agent不传输历史工具调用的文字
This commit is contained in:
parent
2c1b41741f
commit
e1dbcad755
@ -172,36 +172,39 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li
|
|||||||
if not text:
|
if not text:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if current_tag == "TOOL_RESPONSE":
|
# 不往后传输 历史工具调用的文字
|
||||||
if is_recent_message:
|
if current_tag == "ANSWER":
|
||||||
# 最近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"
|
filtered_content += f"[ANSWER]\n{text}\n"
|
||||||
elif current_tag != "THINK" and current_tag != "PREAMBLE":
|
# if current_tag == "TOOL_RESPONSE":
|
||||||
filtered_content += text + "\n"
|
# 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: # 标签
|
else: # 标签
|
||||||
current_tag = parts[i]
|
current_tag = parts[i]
|
||||||
|
|
||||||
@ -233,37 +236,43 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li
|
|||||||
text = parts[i].strip()
|
text = parts[i].strip()
|
||||||
if not text:
|
if not text:
|
||||||
continue
|
continue
|
||||||
|
# 不往后传输 历史工具调用的文字
|
||||||
if current_tag == "TOOL_RESPONSE":
|
if current_tag == "ANSWER":
|
||||||
# 解析 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({
|
final_messages.append({
|
||||||
"role": ASSISTANT,
|
"role": ASSISTANT,
|
||||||
"content": text
|
"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: # 标签
|
else: # 标签
|
||||||
current_tag = parts[i]
|
current_tag = parts[i]
|
||||||
else:
|
else:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user