From 2c4a839cef2a22054a6cfa375721bcb198257c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=BD=AE?= Date: Mon, 10 Nov 2025 17:54:35 +0800 Subject: [PATCH] process_messages --- fastapi_app.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/fastapi_app.py b/fastapi_app.py index 33e3ed9..aa376a5 100644 --- a/fastapi_app.py +++ b/fastapi_app.py @@ -847,10 +847,23 @@ def process_messages(messages: List[Message], language: Optional[str] = None) -> continue if current_tag == "TOOL_RESPONSE": - # 统计 [TOOL_RESPONSE] 后面的文字长度,超过1000字符就丢弃 - if len(text) <= 1000: + # 统计 [TOOL_RESPONSE] 后面的文字长度,超过500字就截取 + if len(text) <= 500: filtered_content += f"[TOOL_RESPONSE]\n{text}\n" - # 如果超过1000字符,直接丢弃这块内容 + 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" elif current_tag == "TOOL_CALL": filtered_content += f"[TOOL_CALL]\n{text}\n" elif current_tag == "ANSWER":