diff --git a/pilot/model/proxy/llms/chatgpt.py b/pilot/model/proxy/llms/chatgpt.py index 1da815bfa..d0adaf606 100644 --- a/pilot/model/proxy/llms/chatgpt.py +++ b/pilot/model/proxy/llms/chatgpt.py @@ -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