Skip to content

Commit 0b02451

Browse files
zfanswer一帆
andauthored
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>
1 parent d9cc102 commit 0b02451

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

pilot/model/proxy/llms/chatgpt.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@ def chatgpt_generate_stream(
172172
res = client.chat.completions.create(messages=history, **payloads)
173173
text = ""
174174
for r in res:
175+
# logger.info(str(r))
176+
# Azure Openai reponse may have empty choices body in the first chunk
177+
# to avoid index out of range error
178+
if not r.get("choices"):
179+
continue
175180
if r.choices[0].delta.content is not None:
176181
content = r.choices[0].delta.content
177182
text += content
@@ -186,6 +191,8 @@ def chatgpt_generate_stream(
186191

187192
text = ""
188193
for r in res:
194+
if not r.get("choices"):
195+
continue
189196
if r["choices"][0]["delta"].get("content") is not None:
190197
content = r["choices"][0]["delta"]["content"]
191198
text += content
@@ -220,6 +227,8 @@ async def async_chatgpt_generate_stream(
220227
res = await client.chat.completions.create(messages=history, **payloads)
221228
text = ""
222229
for r in res:
230+
if not r.get("choices"):
231+
continue
223232
if r.choices[0].delta.content is not None:
224233
content = r.choices[0].delta.content
225234
text += content
@@ -233,6 +242,8 @@ async def async_chatgpt_generate_stream(
233242

234243
text = ""
235244
async for r in res:
245+
if not r.get("choices"):
246+
continue
236247
if r["choices"][0]["delta"].get("content") is not None:
237248
content = r["choices"][0]["delta"]["content"]
238249
text += content

0 commit comments

Comments
 (0)