process_message只保留find、get工具
This commit is contained in:
parent
93d13f3d22
commit
4a909cd846
@ -144,7 +144,15 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li
|
||||
|
||||
这是 get_content_from_messages 的逆运算,将包含 [TOOL_RESPONSE] 的消息重新组装回
|
||||
msg['role'] == 'function' 和 msg.get('function_call') 的格式。
|
||||
|
||||
Args:
|
||||
messages: 消息列表
|
||||
language: 可选的语言参数
|
||||
include_function_name: 需要包含的function_name关键词列表,默认包含['find', 'get']
|
||||
"""
|
||||
# 设置默认的排除function_name列表
|
||||
include_function_name = ['find', 'get']
|
||||
|
||||
processed_messages = []
|
||||
|
||||
# 收集所有ASSISTANT消息的索引
|
||||
@ -173,38 +181,36 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li
|
||||
continue
|
||||
|
||||
# 不往后传输 历史工具调用的文字
|
||||
if current_tag == "ANSWER":
|
||||
filtered_content += f"[ANSWER]\n{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:]
|
||||
if current_tag == "TOOL_RESPONSE":
|
||||
if is_recent_message:
|
||||
# 最近10条ASSISTANT消息:保留完整TOOL_RESPONSE信息(使用简略模式)
|
||||
if len(text) <= 1000:
|
||||
filtered_content += f"[TOOL_RESPONSE] {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}字..."
|
||||
# 计算省略的字数
|
||||
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"
|
||||
# 拼接内容
|
||||
truncated_text = f"{first_part}\n{omitted_text}\n{middle_part}\n{omitted_text}\n{last_part}"
|
||||
filtered_content += f"[TOOL_RESPONSE] {truncated_text}\n"
|
||||
# 10条以上的消息:不保留TOOL_RESPONSE数据(完全跳过)
|
||||
elif current_tag == "TOOL_CALL":
|
||||
if is_recent_message:
|
||||
# 最近10条ASSISTANT消息:保留TOOL_CALL信息
|
||||
filtered_content += f"[TOOL_CALL] {text}\n"
|
||||
# 10条以上的消息:不保留TOOL_CALL数据(完全跳过)
|
||||
elif current_tag == "ANSWER":
|
||||
# 所有ASSISTANT消息都保留ANSWER数据
|
||||
filtered_content += f"[ANSWER] {text}\n"
|
||||
elif current_tag != "THINK" and current_tag != "PREAMBLE":
|
||||
filtered_content += text + "\n"
|
||||
else: # 标签
|
||||
current_tag = parts[i]
|
||||
|
||||
@ -237,48 +243,61 @@ def process_messages(messages: List[Dict], language: Optional[str] = None) -> Li
|
||||
if not text:
|
||||
continue
|
||||
# 不往后传输 历史工具调用的文字
|
||||
if current_tag == "ANSWER":
|
||||
|
||||
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 ""
|
||||
|
||||
# 过滤掉包含指定关键词的function_name
|
||||
should_include = False
|
||||
if function_name:
|
||||
for exclude_name in include_function_name:
|
||||
if exclude_name in function_name:
|
||||
should_include = True
|
||||
break
|
||||
|
||||
if should_include:
|
||||
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 ""
|
||||
|
||||
# 过滤掉包含指定关键词的function_name
|
||||
should_include = False
|
||||
if function_name:
|
||||
for exclude_name in include_function_name:
|
||||
if exclude_name in function_name:
|
||||
should_include = True
|
||||
break
|
||||
|
||||
if should_include:
|
||||
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
|
||||
})
|
||||
|
||||
# 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:
|
||||
# 非 assistant 消息或不包含 [TOOL_RESPONSE] 的消息直接添加
|
||||
final_messages.append(msg)
|
||||
|
||||
print(final_messages)
|
||||
return final_messages
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user