Skip to content

Commit

Permalink
bugfix: fix index out of range error due to Azure Openai reponses an …
Browse files Browse the repository at this point in the history
…empty chunk at first
  • Loading branch information
一帆 committed Nov 22, 2023
1 parent 64165b2 commit 059d8fe
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pilot/model/proxy/llms/chatgpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ def chatgpt_generate_stream(
res = client.chat.completions.create(messages=history, **payloads)
text = ""
for r in res:
# logger.info(str(r))
# Azure Openai reponse may have empty choices body in the first chunk
# to avoid index out of range error
if not r.get('choices'):
continue
if r.choices[0].delta.content is not None:
content = r.choices[0].delta.content
text += content
Expand All @@ -186,6 +191,8 @@ def chatgpt_generate_stream(

text = ""
for r in res:
if not r.get('choices'):
continue
if r["choices"][0]["delta"].get("content") is not None:
content = r["choices"][0]["delta"]["content"]
text += content
Expand Down Expand Up @@ -220,6 +227,8 @@ async def async_chatgpt_generate_stream(
res = await client.chat.completions.create(messages=history, **payloads)
text = ""
for r in res:
if not r.get('choices'):
continue
if r.choices[0].delta.content is not None:
content = r.choices[0].delta.content
text += content
Expand All @@ -233,6 +242,8 @@ async def async_chatgpt_generate_stream(

text = ""
async for r in res:
if not r.get('choices'):
continue
if r["choices"][0]["delta"].get("content") is not None:
content = r["choices"][0]["delta"]["content"]
text += content
Expand Down

0 comments on commit 059d8fe

Please sign in to comment.