From 403df9de359bc240185508b0996876d23b67f03d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Tue, 16 Dec 2025 09:09:17 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B7=A5=E5=85=B7=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/fastapi_utils.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/utils/fastapi_utils.py b/utils/fastapi_utils.py index 29ea6d1..1fcec31 100644 --- a/utils/fastapi_utils.py +++ b/utils/fastapi_utils.py @@ -251,7 +251,8 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li assistant_content = "" function_calls = [] tool_responses = [] - tool_id = "" + tool_id_counter = 0 # 添加唯一的工具调用计数器 + tool_id_list = [] for i in range(0, len(parts)): if i % 2 == 0: # 文本内容 text = parts[i].strip() @@ -272,8 +273,9 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li if exclude_name in function_name: should_include = True break - - if should_include: + + if should_include and len(tool_id_list)>0: + tool_id = tool_id_list.pop(0) # 将 TOOL_RESPONSE 包装成 tool_result 消息,紧跟对应的 tool_use final_messages.append({ "role": TOOL, @@ -296,7 +298,9 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li break if should_include: - tool_id = f"tool_id_{i}" + tool_id = f"tool_id_{tool_id_counter}" # 使用唯一计数器 + tool_id_list.append(tool_id) + tool_id_counter += 1 # 递增计数器 final_messages.append({ "role": ASSISTANT, "content": "",