From 62cab2f3a9af68e657631f0137a9746d8b3bb81f Mon Sep 17 00:00:00 2001 From: 2829798842 <2829798842@qq.com> Date: Wed, 28 May 2025 19:20:29 +0800 Subject: [PATCH] Update tool_processor.py --- .../info_processors/tool_processor.py | 66 ++++++++----------- 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/src/chat/focus_chat/info_processors/tool_processor.py b/src/chat/focus_chat/info_processors/tool_processor.py index 2d52a04a..e82dbb85 100644 --- a/src/chat/focus_chat/info_processors/tool_processor.py +++ b/src/chat/focus_chat/info_processors/tool_processor.py @@ -12,7 +12,7 @@ from .base_processor import BaseProcessor from typing import List, Optional, Dict from src.chat.heart_flow.observation.observation import Observation from src.chat.focus_chat.info.structured_info import StructuredInfo -from src.chat.heart_flow.observation.structure_observation import StructureObservation +from src.chat.focus_chat.info.info_base import InfoBase logger = get_logger("processor") @@ -22,21 +22,21 @@ def init_prompt(): # 添加工具执行器提示词 tool_executor_prompt = """ -你是一个专门执行工具的助手。你的名字是{bot_name}。现在是{time_now}。 +You are a specialized tool execution assistant. Your name is {bot_name}. The current time is {time_now}. -你当前的额外信息: +Your current additional information: {memory_str} -群里正在进行的聊天内容: +Current chat content in the group: {chat_observe_info} -请仔细分析聊天内容,考虑以下几点: -1. 内容中是否包含需要查询信息的问题 -2. 是否需要执行特定操作 -3. 是否有明确的工具使用指令 -4. 考虑用户与你的关系以及当前的对话氛围 +Please carefully analyze the chat content and consider the following points: +1. Does the content contain questions that require information queries? +2. Are there specific operations that need to be executed? +3. Are there explicit tool usage instructions? +4. Consider your relationship with the user and the current conversation atmosphere. -如果需要使用工具,请直接调用相应的工具函数。如果不需要使用工具,请简单输出"无需使用工具"。 +If you need to use tools, please directly call the corresponding tool functions. If no tools are needed, simply output "No tools needed". """ Prompt(tool_executor_prompt, "tool_executor_prompt") @@ -51,13 +51,13 @@ class ToolProcessor(BaseProcessor): self.llm_model = LLMRequest( model=global_config.model.focus_tool_use, max_tokens=500, - request_type="focus_tool", + request_type="tool_execution", ) self.structured_info = [] async def process_info( self, observations: Optional[List[Observation]] = None, running_memorys: Optional[List[Dict]] = None, *infos - ) -> List[dict]: + ) -> List[InfoBase]: """处理信息对象 Args: @@ -67,36 +67,24 @@ class ToolProcessor(BaseProcessor): list: 处理后的结构化信息列表 """ - working_infos = [] + result_infos = [] if observations: for observation in observations: if isinstance(observation, ChattingObservation): - result, used_tools, prompt = await self.execute_tools(observation, running_memorys) - - # 更新WorkingObservation中的结构化信息 - logger.debug(f"工具调用结果: {result}") - - for observation in observations: - if isinstance(observation, StructureObservation): - for structured_info in result: - # logger.debug(f"{self.log_prefix} 更新WorkingObservation中的结构化信息: {structured_info}") - observation.add_structured_info(structured_info) - - working_infos = observation.get_observe_info() - logger.debug(f"{self.log_prefix} 获取更新后WorkingObservation中的结构化信息: {working_infos}") - - structured_info = StructuredInfo() - if working_infos: - for working_info in working_infos: - # print(f"working_info: {working_info}") - # print(f"working_info.get('type'): {working_info.get('type')}") - # print(f"working_info.get('content'): {working_info.get('content')}") - structured_info.set_info(key=working_info.get("type"), value=working_info.get("content")) - # info = structured_info.get_processed_info() - # print(f"info: {info}") - - return [structured_info] + # 执行工具调用 + tool_results, used_tools, _ = await self.execute_tools(observation, running_memorys) + + # 为每个工具调用结果创建StructuredInfo对象并返回 + for tool_result in tool_results: + structured_info = StructuredInfo() + + structured_info.data[tool_result.get("type")] = tool_result.get("content") + result_infos.append(structured_info) + logger.info(f"{self.log_prefix} 工具调用成功: {tool_result.get('type')} - {tool_result.get('content')}") + + logger.debug(f"result_infos: {result_infos}") + return result_infos async def execute_tools(self, observation: ChattingObservation, running_memorys: Optional[List[Dict]] = None): """ @@ -162,7 +150,7 @@ class ToolProcessor(BaseProcessor): ) # 调用LLM,专注于工具使用 - logger.debug(f"开始执行工具调用{prompt}") + # logger.debug(f"开始执行工具调用{prompt}") response, _, tool_calls = await self.llm_model.generate_response_tool_async(prompt=prompt, tools=tools) logger.debug(f"获取到工具原始输出:\n{tool_calls}")