处理 tool_call字符串

This commit is contained in:
朱潮 2025-10-27 21:40:42 +08:00
parent 91b75990bb
commit dd8d9f5680

View File

@ -42,6 +42,7 @@ from modified_assistant import update_agent_llm
from task_queue.manager import queue_manager from task_queue.manager import queue_manager
from task_queue.integration_tasks import process_files_async, cleanup_project_async from task_queue.integration_tasks import process_files_async, cleanup_project_async
from task_queue.task_status import task_status_store from task_queue.task_status import task_status_store
import re
os.environ["TOKENIZERS_PARALLELISM"] = "false" os.environ["TOKENIZERS_PARALLELISM"] = "false"
@ -56,13 +57,24 @@ def get_content_from_messages(messages: List[dict], tool_response: bool = True)
ANSWER_S = '[ANSWER]' ANSWER_S = '[ANSWER]'
for msg in messages: for msg in messages:
if msg['role'] == ASSISTANT: if msg['role'] == ASSISTANT:
if msg.get('reasoning_content'): if msg.get('reasoning_content'):
assert isinstance(msg['reasoning_content'], str), 'Now only supports text messages' assert isinstance(msg['reasoning_content'], str), 'Now only supports text messages'
content.append(f'{THOUGHT_S}\n{msg["reasoning_content"]}') content.append(f'{THOUGHT_S}\n{msg["reasoning_content"]}')
if msg.get('content'): if msg.get('content'):
assert isinstance(msg['content'], str), 'Now only supports text messages' assert isinstance(msg['content'], str), 'Now only supports text messages'
content.append(f'{ANSWER_S}\n{msg["content"]}') # 过滤掉流式输出中的不完整 tool_call 文本
content_text = msg["content"]
# 使用正则表达式替换不完整的 tool_call 模式为空字符串
# 匹配并替换不完整的 tool_call 模式
content_text = re.sub(r'<t?o?o?l?_?c?a?l?l?$', '', content_text)
# 只有在处理后内容不为空时才添加
if content_text.strip():
content.append(f'{ANSWER_S}\n{content_text}')
if msg.get('function_call'): if msg.get('function_call'):
content.append(f'{TOOL_CALL_S} {msg["function_call"]["name"]}\n{msg["function_call"]["arguments"]}') content.append(f'{TOOL_CALL_S} {msg["function_call"]["name"]}\n{msg["function_call"]["arguments"]}')
elif msg['role'] == FUNCTION: elif msg['role'] == FUNCTION:
@ -70,6 +82,7 @@ def get_content_from_messages(messages: List[dict], tool_response: bool = True)
content.append(f'{TOOL_RESULT_S} {msg["name"]}\n{msg["content"]}') content.append(f'{TOOL_RESULT_S} {msg["name"]}\n{msg["content"]}')
else: else:
raise TypeError raise TypeError
print(content)
if content: if content:
full_text = '\n'.join(content) full_text = '\n'.join(content)