File tree Expand file tree Collapse file tree 1 file changed +11
-0
lines changed
Expand file tree Collapse file tree 1 file changed +11
-0
lines changed Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments