From 0b02451fb387dae9c1345e5e23413cd1114bec14 Mon Sep 17 00:00:00 2001 From: Fan Date: Tue, 28 Nov 2023 14:27:39 +0800 Subject: [PATCH] bugfix(Azure): fix index out of range error due to Azure Openai reponses an empty chunk at first (#820) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 一帆 --- pilot/model/proxy/llms/chatgpt.py | 11 +++++++++++ 1 file changed, 11 insertions(+) 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