Skip to content

Commit

Permalink
Error during async streaming of OpenAIAgent & multi tool calls (#9207)
Browse files Browse the repository at this point in the history
* fix: invalid chunk when streaming

* edit condition order

---------

Co-authored-by: lpineau <lpineau@onestock-retail.com>
  • Loading branch information
mathematisse and lpineau authored Nov 29, 2023
1 parent 895c15f commit 6efaaeb
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions llama_index/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,16 +506,27 @@ async def gen() -> ChatResponseAsyncGen:
tool_calls: List[ChoiceDeltaToolCall] = []

is_function = False
first_chat_chunk = True
async for response in await self._aclient.chat.completions.create(
messages=message_dicts,
stream=True,
**self._get_model_kwargs(**kwargs),
):
response = cast(ChatCompletionChunk, response)
if len(response.choices) > 0:
# check if the first chunk has neither content nor tool_calls
# this happens when 1106 models end up calling multiple tools
if (
first_chat_chunk
and response.choices[0].delta.content is None
and response.choices[0].delta.tool_calls is None
):
first_chat_chunk = False
continue
delta = response.choices[0].delta
else:
delta = ChoiceDelta()
first_chat_chunk = False

# check if this chunk is the start of a function call
if delta.tool_calls:
Expand Down

0 comments on commit 6efaaeb

Please sign in to comment.