保留tool_reponse
This commit is contained in:
parent
987d34c4c8
commit
0478163b2d
@ -827,19 +827,46 @@ async def fetch_bot_config(bot_id: str) -> Dict[str, Any]:
|
||||
|
||||
|
||||
def process_messages(messages: List[Message], language: Optional[str] = None) -> List[Dict[str, str]]:
|
||||
"""处理消息列表,包括[ANSWER]分割和语言指令添加"""
|
||||
"""处理消息列表,包括[TOOL_CALL]|[TOOL_RESPONSE]|[ANSWER]分割和语言指令添加"""
|
||||
processed_messages = []
|
||||
|
||||
# 处理每条消息
|
||||
for msg in messages:
|
||||
if msg.role == "assistant":
|
||||
# 对assistant消息进行[ANSWER]分割处理,只保留最后一段
|
||||
content_parts = msg.content.split("[ANSWER]")
|
||||
if content_parts:
|
||||
# 取最后一段非空文本
|
||||
last_part = content_parts[-1].strip()
|
||||
processed_messages.append({"role": msg.role, "content": last_part})
|
||||
# 使用正则表达式按照 [TOOL_CALL]|[TOOL_RESPONSE]|[ANSWER] 进行切割
|
||||
parts = re.split(r'\[(TOOL_CALL|TOOL_RESPONSE|ANSWER)\]', msg.content)
|
||||
|
||||
# 重新组装内容,过滤掉过长的 [TOOL_RESPONSE] 后的内容
|
||||
filtered_content = ""
|
||||
current_tag = None
|
||||
|
||||
for i in range(0, len(parts)):
|
||||
if i % 2 == 0: # 文本内容
|
||||
text = parts[i].strip()
|
||||
if not text:
|
||||
continue
|
||||
|
||||
if current_tag == "TOOL_RESPONSE":
|
||||
# 统计 [TOOL_RESPONSE] 后面的文字长度,超过1000字符就丢弃
|
||||
if len(text) <= 1000:
|
||||
filtered_content += f"[TOOL_RESPONSE]\n{text}\n"
|
||||
# 如果超过1000字符,直接丢弃这块内容
|
||||
elif current_tag == "TOOL_CALL":
|
||||
filtered_content += f"[TOOL_CALL]\n{text}\n"
|
||||
elif current_tag == "ANSWER":
|
||||
filtered_content += f"[ANSWER]\n{text}\n"
|
||||
else:
|
||||
# 第一个标签之前的内容
|
||||
filtered_content += text + "\n"
|
||||
else: # 标签
|
||||
current_tag = parts[i]
|
||||
|
||||
# 取最终处理后的内容,去除首尾空白
|
||||
final_content = filtered_content.strip()
|
||||
if final_content:
|
||||
processed_messages.append({"role": msg.role, "content": final_content})
|
||||
else:
|
||||
# 如果处理后为空,使用原内容
|
||||
processed_messages.append({"role": msg.role, "content": msg.content})
|
||||
else:
|
||||
processed_messages.append({"role": msg.role, "content": msg.content})
|
||||
|
||||
Loading…
Reference in New Issue
Block a user