agent不传输历史工具调用的文字

This commit is contained in:
朱潮 2025-12-05 13:15:33 +08:00
parent 2c1b41741f
commit e1dbcad755

View File

@ -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: