Skip to content

Commit

Permalink
bugfix(Azure): fix index out of range error due to Azure Openai repon…
Browse files Browse the repository at this point in the history
…ses an empty chunk at first (eosphoros-ai#820)

Co-authored-by: 一帆 <zhang.f@digitalcnzz.com>
  • Loading branch information
zfanswer and 一帆 committed Nov 28, 2023
1 parent d9cc102 commit 0b02451
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 0b02451

Please sign in to comment.