From 29f7f8b81ecec67cba2a1f9aa471f08b88e3636a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E8=85=BE?= <101850389+hangters@users.noreply.github.com> Date: Mon, 29 Jul 2024 19:35:16 +0800 Subject: [PATCH] fix MiniMax chat bug (#1733) ### What problem does this PR solve? #1717 fix MiniMax chat bug ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) Co-authored-by: Zhedong Cen --- rag/llm/chat_model.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/rag/llm/chat_model.py b/rag/llm/chat_model.py index 48dc29ba0..0a88f3027 100644 --- a/rag/llm/chat_model.py +++ b/rag/llm/chat_model.py @@ -564,12 +564,15 @@ class MiniMaxChat(Base): ) for resp in response.text.split("\n\n")[:-1]: resp = json.loads(resp[6:]) - if "delta" in resp["choices"][0]: + text = "" + if "choices" in resp and "delta" in resp["choices"][0]: text = resp["choices"][0]["delta"]["content"] - else: - continue ans += text - total_tokens += num_tokens_from_string(text) + total_tokens = ( + total_tokens + num_tokens_from_string(text) + if "usage" not in resp + else resp["usage"]["total_tokens"] + ) yield ans except Exception as e: