bugfix(Azure): fix index out of range error due to Azure Openai reponses an empty chunk at first (#820)

Co-authored-by: 一帆 <zhang.f@digitalcnzz.com>
This commit is contained in:
Fan 2023-11-28 14:27:39 +08:00 committed by GitHub
parent d9cc1020c1
commit 0b02451fb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
@ -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
@ -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
@ -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