Skip to content

Commit

Permalink
fix(openai): multiple streamed tool calls (#922)
Browse files Browse the repository at this point in the history
  • Loading branch information
hassiebp authored Sep 12, 2024
1 parent 6744bb3 commit c14a10e
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions langfuse/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,16 +421,30 @@ def _extract_streamed_openai_response(resource, chunks):
)

if not curr:
completion["tool_calls"] = {
"name": getattr(tool_call_chunk, "name", ""),
"arguments": getattr(tool_call_chunk, "arguments", ""),
}
completion["tool_calls"] = [
{
"name": getattr(tool_call_chunk, "name", ""),
"arguments": getattr(tool_call_chunk, "arguments", ""),
}
]

elif getattr(tool_call_chunk, "name", None) is not None:
curr.append(
{
"name": getattr(tool_call_chunk, "name", None),
"arguments": getattr(
tool_call_chunk, "arguments", None
),
}
)

else:
curr["name"] = curr["name"] or getattr(
curr[-1]["name"] = curr[-1]["name"] or getattr(
tool_call_chunk, "name", None
)
curr["arguments"] += getattr(tool_call_chunk, "arguments", None)
curr[-1]["arguments"] += getattr(
tool_call_chunk, "arguments", None
)

if resource.type == "completion":
completion += choice.get("text", None)
Expand All @@ -449,7 +463,10 @@ def get_response_for_chat():
completion["tool_calls"]
and {
"role": "assistant",
"tool_calls": [{"function": completion["tool_calls"]}],
# "tool_calls": [{"function": completion["tool_calls"]}],
"tool_calls": [
{"function": data} for data in completion["tool_calls"]
],
}
)
or None
Expand Down

0 comments on commit c14a10e

Please sign in to comment.