From e1dbcad7556008f7786e75810549d2cd03473e78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Fri, 5 Dec 2025 13:15:33 +0800 Subject: [PATCH] =?UTF-8?q?agent=E4=B8=8D=E4=BC=A0=E8=BE=93=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E5=B7=A5=E5=85=B7=E8=B0=83=E7=94=A8=E7=9A=84=E6=96=87?= =?UTF-8?q?=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/fastapi_utils.py | 121 ++++++++++++++++++++++------------------- 1 file changed, 65 insertions(+), 56 deletions(-) diff --git a/utils/fastapi_utils.py b/utils/fastapi_utils.py index b6c924e..c69d082 100644 --- a/utils/fastapi_utils.py +++ b/utils/fastapi_utils.py @@ -172,36 +172,39 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li if not text: continue - if current_tag == "TOOL_RESPONSE": - if is_recent_message: - # 最近10条ASSISTANT消息:保留完整TOOL_RESPONSE信息(使用简略模式) - if len(text) <= 500: - filtered_content += f"[TOOL_RESPONSE]\n{text}\n" - else: - # 截取前中后3段内容,每段250字 - first_part = text[:250] - middle_start = len(text) // 2 - 125 - middle_part = text[middle_start:middle_start + 250] - last_part = text[-250:] - - # 计算省略的字数 - omitted_count = len(text) - 750 - omitted_text = f"...此处省略{omitted_count}字..." - - # 拼接内容 - truncated_text = f"{first_part}\n{omitted_text}\n{middle_part}\n{omitted_text}\n{last_part}" - filtered_content += f"[TOOL_RESPONSE]\n{truncated_text}\n" - # 10条以上的消息:不保留TOOL_RESPONSE数据(完全跳过) - elif current_tag == "TOOL_CALL": - if is_recent_message: - # 最近10条ASSISTANT消息:保留TOOL_CALL信息 - filtered_content += f"[TOOL_CALL]\n{text}\n" - # 10条以上的消息:不保留TOOL_CALL数据(完全跳过) - elif current_tag == "ANSWER": - # 所有ASSISTANT消息都保留ANSWER数据 + # 不往后传输 历史工具调用的文字 + if current_tag == "ANSWER": filtered_content += f"[ANSWER]\n{text}\n" - elif current_tag != "THINK" and current_tag != "PREAMBLE": - filtered_content += text + "\n" + # if current_tag == "TOOL_RESPONSE": + # if is_recent_message: + # # 最近10条ASSISTANT消息:保留完整TOOL_RESPONSE信息(使用简略模式) + # if len(text) <= 500: + # filtered_content += f"[TOOL_RESPONSE]\n{text}\n" + # else: + # # 截取前中后3段内容,每段250字 + # first_part = text[:250] + # middle_start = len(text) // 2 - 125 + # middle_part = text[middle_start:middle_start + 250] + # last_part = text[-250:] + + # # 计算省略的字数 + # omitted_count = len(text) - 750 + # omitted_text = f"...此处省略{omitted_count}字..." + + # # 拼接内容 + # truncated_text = f"{first_part}\n{omitted_text}\n{middle_part}\n{omitted_text}\n{last_part}" + # filtered_content += f"[TOOL_RESPONSE]\n{truncated_text}\n" + # # 10条以上的消息:不保留TOOL_RESPONSE数据(完全跳过) + # elif current_tag == "TOOL_CALL": + # if is_recent_message: + # # 最近10条ASSISTANT消息:保留TOOL_CALL信息 + # filtered_content += f"[TOOL_CALL]\n{text}\n" + # # 10条以上的消息:不保留TOOL_CALL数据(完全跳过) + # elif current_tag == "ANSWER": + # # 所有ASSISTANT消息都保留ANSWER数据 + # filtered_content += f"[ANSWER]\n{text}\n" + # elif current_tag != "THINK" and current_tag != "PREAMBLE": + # filtered_content += text + "\n" else: # 标签 current_tag = parts[i] @@ -233,37 +236,43 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li text = parts[i].strip() if not text: continue - - if current_tag == "TOOL_RESPONSE": - # 解析 TOOL_RESPONSE 格式:[TOOL_RESPONSE] function_name\ncontent - lines = text.split('\n', 1) - function_name = lines[0].strip() if lines else "" - response_content = lines[1].strip() if len(lines) > 1 else "" - - final_messages.append({ - "role": FUNCTION, - "name": function_name, - "content": response_content - }) - elif current_tag == "TOOL_CALL": - # 解析 TOOL_CALL 格式:[TOOL_CALL] function_name\narguments - lines = text.split('\n', 1) - function_name = lines[0].strip() if lines else "" - arguments = lines[1].strip() if len(lines) > 1 else "" - - final_messages.append({ - "role": ASSISTANT, - "content": "", - "function_call": { - "name": function_name, - "arguments": arguments - } - }) - elif current_tag != "THINK" and current_tag != "PREAMBLE": + # 不往后传输 历史工具调用的文字 + if current_tag == "ANSWER": final_messages.append({ "role": ASSISTANT, "content": text }) + + # if current_tag == "TOOL_RESPONSE": + # # 解析 TOOL_RESPONSE 格式:[TOOL_RESPONSE] function_name\ncontent + # lines = text.split('\n', 1) + # function_name = lines[0].strip() if lines else "" + # response_content = lines[1].strip() if len(lines) > 1 else "" + + # final_messages.append({ + # "role": FUNCTION, + # "name": function_name, + # "content": response_content + # }) + # elif current_tag == "TOOL_CALL": + # # 解析 TOOL_CALL 格式:[TOOL_CALL] function_name\narguments + # lines = text.split('\n', 1) + # function_name = lines[0].strip() if lines else "" + # arguments = lines[1].strip() if len(lines) > 1 else "" + + # final_messages.append({ + # "role": ASSISTANT, + # "content": "", + # "function_call": { + # "name": function_name, + # "arguments": arguments + # } + # }) + # elif current_tag != "THINK" and current_tag != "PREAMBLE": + # final_messages.append({ + # "role": ASSISTANT, + # "content": text + # }) else: # 标签 current_tag = parts[i] else: