From 26ecc9e3497c162b246b2f8c6cb6451a9e68a997 Mon Sep 17 00:00:00 2001 From: Sync-Packages Action Date: Fri, 19 Jul 2024 11:37:26 +0000 Subject: [PATCH] Sync: devchat[main](5da5b4d7) Merge pull request #410 from devchat-ai/fix_workflow_openai_error --- site-packages/devchat-0.3.0.dist-info/RECORD | 2 +- site-packages/devchat/llm/openai.py | 43 +++++++++++--------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/site-packages/devchat-0.3.0.dist-info/RECORD b/site-packages/devchat-0.3.0.dist-info/RECORD index a53ce7d..279164e 100644 --- a/site-packages/devchat-0.3.0.dist-info/RECORD +++ b/site-packages/devchat-0.3.0.dist-info/RECORD @@ -123,7 +123,7 @@ devchat/llm/__pycache__/pipeline.cpython-38.pyc,, devchat/llm/__pycache__/text_confirm.cpython-38.pyc,, devchat/llm/__pycache__/tools_call.cpython-38.pyc,, devchat/llm/chat.py,sha256=3xTtBQdaoZosxXxqiDaFzka9phgF2SgFu1hvaDv8JEw,3279 -devchat/llm/openai.py,sha256=6o551AdNRdFkD5HGPJ4POsW4NlTkdn7caKEAUy3HNTs,6353 +devchat/llm/openai.py,sha256=izaocQiDBGCpJUgK18-XEA7Z2u9PLYvWez9Xwaoc3G0,6557 devchat/llm/pipeline.py,sha256=u0NyaiIRNB4xCcbxYxZ3E3Im2dHLIcA3fZZ6QiUcpf0,2120 devchat/llm/text_confirm.py,sha256=sdt7AUFDcsOZ0fLfS0vtjdS2_8xhkTF6aF8Sn05OlI0,1462 devchat/llm/tools_call.py,sha256=OBObtFAzuqEJPq7Ro9hR4oirrcMtxGchlMQl8vL1CBc,8038 diff --git a/site-packages/devchat/llm/openai.py b/site-packages/devchat/llm/openai.py index fb9a95d..d8ffca7 100644 --- a/site-packages/devchat/llm/openai.py +++ b/site-packages/devchat/llm/openai.py @@ -74,10 +74,11 @@ def chat_completion_stream_raw(**kwargs): def stream_out_chunk(chunks): for chunk in chunks: chunk_dict = chunk.dict() - delta = chunk_dict["choices"][0]["delta"] - if delta.get("content", None): - print(delta["content"], end="", flush=True) - yield chunk + if len(chunk_dict["choices"]) > 0: + delta = chunk_dict["choices"][0]["delta"] + if delta.get("content", None): + print(delta["content"], end="", flush=True) + yield chunk def retry_timeout(chunks): @@ -97,11 +98,12 @@ def chunks_content(chunks): content = None for chunk in chunks: chunk_dict = chunk.dict() - delta = chunk_dict["choices"][0]["delta"] - if delta.get("content", None): - if content is None: - content = "" - content += delta["content"] + if len(chunk_dict["choices"]) > 0: + delta = chunk_dict["choices"][0]["delta"] + if delta.get("content", None): + if content is None: + content = "" + content += delta["content"] return content @@ -110,17 +112,18 @@ def chunks_call(chunks): for chunk in chunks: chunk = chunk.dict() - delta = chunk["choices"][0]["delta"] - if "tool_calls" in delta and delta["tool_calls"]: - tool_call = delta["tool_calls"][0]["function"] - if delta["tool_calls"][0].get("index", None) is not None: - index = delta["tool_calls"][0]["index"] - if index >= len(tool_calls): - tool_calls.append({"name": None, "arguments": ""}) - if tool_call.get("name", None): - tool_calls[-1]["name"] = tool_call["name"] - if tool_call.get("arguments", None): - tool_calls[-1]["arguments"] += tool_call["arguments"] + if len(chunk["choices"]) > 0: + delta = chunk["choices"][0]["delta"] + if "tool_calls" in delta and delta["tool_calls"]: + tool_call = delta["tool_calls"][0]["function"] + if delta["tool_calls"][0].get("index", None) is not None: + index = delta["tool_calls"][0]["index"] + if index >= len(tool_calls): + tool_calls.append({"name": None, "arguments": ""}) + if tool_call.get("name", None): + tool_calls[-1]["name"] = tool_call["name"] + if tool_call.get("arguments", None): + tool_calls[-1]["arguments"] += tool_call["arguments"] return tool_calls