From ac26e862acb31231bcfa6893bc5ea73d0c346ef0 Mon Sep 17 00:00:00 2001 From: 2829798842 <2829798842@qq.com> Date: Tue, 27 May 2025 20:33:56 +0800 Subject: [PATCH] Update tool_processor.py --- .../info_processors/tool_processor.py | 54 +++++++++---------- 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/src/chat/focus_chat/info_processors/tool_processor.py b/src/chat/focus_chat/info_processors/tool_processor.py index 39ac8dc6..18e9246b 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") @@ -57,7 +57,7 @@ class ToolProcessor(BaseProcessor): async def process_info( self, observations: Optional[List[Observation]] = None, running_memorys: Optional[List[Dict]] = None, *infos - ) -> List[dict]: + ) -> List[InfoBase]: """处理信息对象 Args: @@ -67,29 +67,23 @@ 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中的结构化信息 - 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: - structured_info.set_info(working_info.get("type"), working_info.get("content")) - - return [structured_info] + # 执行工具调用 + tool_results, used_tools, prompt = await self.execute_tools(observation, running_memorys) + + # 为每个工具调用结果创建StructuredInfo对象并返回 + for tool_result in tool_results: + structured_info = StructuredInfo() + structured_info.set_info(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): """