From 940072592f12ae142ba36065e2f3e5876b81fb4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E5=A5=87?= <49386598+alen08266@users.noreply.github.com> Date: Thu, 13 Mar 2025 18:59:59 +0800 Subject: [PATCH] Fix: chat_completion answer data incorrect (#6041) ### What problem does this PR solve? fix chat_completion answer data incorrect ### Type of change - [x] Bug Fix (non-breaking change which fixes an issue) Co-authored-by: renqi --- api/apps/sdk/session.py | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/api/apps/sdk/session.py b/api/apps/sdk/session.py index d29666adf..fb3f26d00 100644 --- a/api/apps/sdk/session.py +++ b/api/apps/sdk/session.py @@ -259,7 +259,7 @@ def chat_completion_openai_like(tenant_id, chat_id): # The choices field on the last chunk will always be an empty array []. def streamed_response_generator(chat_id, dia, msg): token_used = 0 - should_split_index = 0 + answer_cache = "" response = { "id": f"chatcmpl-{chat_id}", "choices": [ @@ -285,13 +285,9 @@ def chat_completion_openai_like(tenant_id, chat_id): try: for ans in chat(dia, msg, True): answer = ans["answer"] - incremental = answer[should_split_index:] + incremental = answer.replace(answer_cache, "", 1) + answer_cache = answer.rstrip("") token_used += len(incremental) - if incremental.endswith(""): - response_data_len = len(incremental.rstrip("")) - else: - response_data_len = len(incremental) - should_split_index += response_data_len response["choices"][0]["delta"]["content"] = incremental yield f"data:{json.dumps(response, ensure_ascii=False)}\n\n" except Exception as e: