refac:tool去处理器化
This commit is contained in:
@@ -28,6 +28,7 @@ from datetime import datetime
|
||||
import re
|
||||
from src.chat.knowledge.knowledge_lib import qa_manager
|
||||
from src.chat.focus_chat.memory_activator import MemoryActivator
|
||||
from src.tools.tool_executor import ToolExecutor
|
||||
|
||||
logger = get_logger("replyer")
|
||||
|
||||
@@ -42,7 +43,7 @@ def init_prompt():
|
||||
Prompt(
|
||||
"""
|
||||
{expression_habits_block}
|
||||
{structured_info_block}
|
||||
{tool_info_block}
|
||||
{memory_block}
|
||||
{relation_info_block}
|
||||
{extra_info_block}
|
||||
@@ -67,7 +68,7 @@ def init_prompt():
|
||||
Prompt(
|
||||
"""
|
||||
{expression_habits_block}
|
||||
{structured_info_block}
|
||||
{tool_info_block}
|
||||
{memory_block}
|
||||
{relation_info_block}
|
||||
{extra_info_block}
|
||||
@@ -156,12 +157,20 @@ class DefaultReplyer:
|
||||
fallback_config = global_config.model.replyer_1.copy()
|
||||
fallback_config.setdefault("weight", 1.0)
|
||||
self.express_model_configs = [fallback_config]
|
||||
|
||||
self.heart_fc_sender = HeartFCSender()
|
||||
self.memory_activator = MemoryActivator()
|
||||
|
||||
|
||||
self.chat_stream = chat_stream
|
||||
self.is_group_chat, self.chat_target_info = get_chat_type_and_target_info(self.chat_stream.stream_id)
|
||||
|
||||
self.heart_fc_sender = HeartFCSender()
|
||||
self.memory_activator = MemoryActivator()
|
||||
self.tool_executor = ToolExecutor(
|
||||
chat_id=self.chat_stream.stream_id,
|
||||
enable_cache=True,
|
||||
cache_ttl=3
|
||||
)
|
||||
|
||||
|
||||
|
||||
|
||||
def _select_weighted_model_config(self) -> Dict[str, Any]:
|
||||
"""使用加权随机选择来挑选一个模型配置"""
|
||||
@@ -394,6 +403,54 @@ class DefaultReplyer:
|
||||
|
||||
return memory_block
|
||||
|
||||
async def build_tool_info(self, reply_data=None, chat_history=None):
|
||||
"""构建工具信息块
|
||||
|
||||
Args:
|
||||
reply_data: 回复数据,包含要回复的消息内容
|
||||
chat_history: 聊天历史
|
||||
|
||||
Returns:
|
||||
str: 工具信息字符串
|
||||
"""
|
||||
if not reply_data:
|
||||
return ""
|
||||
|
||||
reply_to = reply_data.get("reply_to", "")
|
||||
sender, text = self._parse_reply_target(reply_to)
|
||||
|
||||
if not text:
|
||||
return ""
|
||||
|
||||
try:
|
||||
# 使用工具执行器获取信息
|
||||
tool_results = await self.tool_executor.execute_from_chat_message(
|
||||
sender = sender,
|
||||
target_message=text,
|
||||
chat_history=chat_history,
|
||||
return_details=False
|
||||
)
|
||||
|
||||
if tool_results:
|
||||
tool_info_str = "以下是你通过工具获取到的实时信息:\n"
|
||||
for tool_result in tool_results:
|
||||
tool_name = tool_result.get("tool_name", "unknown")
|
||||
content = tool_result.get("content", "")
|
||||
result_type = tool_result.get("type", "info")
|
||||
|
||||
tool_info_str += f"- 【{tool_name}】{result_type}: {content}\n"
|
||||
|
||||
tool_info_str += "以上是你获取到的实时信息,请在回复时参考这些信息。"
|
||||
logger.info(f"{self.log_prefix} 获取到 {len(tool_results)} 个工具结果")
|
||||
return tool_info_str
|
||||
else:
|
||||
logger.debug(f"{self.log_prefix} 未获取到任何工具结果")
|
||||
return ""
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"{self.log_prefix} 工具信息获取失败: {e}")
|
||||
return ""
|
||||
|
||||
def _parse_reply_target(self, target_message: str) -> tuple:
|
||||
sender = ""
|
||||
target = ""
|
||||
@@ -502,11 +559,12 @@ class DefaultReplyer:
|
||||
show_actions=True,
|
||||
)
|
||||
|
||||
# 并行执行三个构建任务
|
||||
expression_habits_block, relation_info, memory_block = await asyncio.gather(
|
||||
# 并行执行四个构建任务
|
||||
expression_habits_block, relation_info, memory_block, tool_info = await asyncio.gather(
|
||||
self.build_expression_habits(chat_talking_prompt_half, target),
|
||||
self.build_relation_info(reply_data, chat_talking_prompt_half),
|
||||
self.build_memory_block(chat_talking_prompt_half, target),
|
||||
self.build_tool_info(reply_data, chat_talking_prompt_half),
|
||||
)
|
||||
|
||||
keywords_reaction_prompt = await self.build_keywords_reaction_prompt(target)
|
||||
@@ -518,6 +576,11 @@ class DefaultReplyer:
|
||||
else:
|
||||
structured_info_block = ""
|
||||
|
||||
if tool_info:
|
||||
tool_info_block = f"{tool_info}"
|
||||
else:
|
||||
tool_info_block = ""
|
||||
|
||||
if extra_info_block:
|
||||
extra_info_block = f"以下是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策\n{extra_info_block}\n以上是你在回复时需要参考的信息,现在请你阅读以下内容,进行决策"
|
||||
else:
|
||||
@@ -590,6 +653,7 @@ class DefaultReplyer:
|
||||
chat_info=chat_talking_prompt,
|
||||
memory_block=memory_block,
|
||||
structured_info_block=structured_info_block,
|
||||
tool_info_block=tool_info_block,
|
||||
extra_info_block=extra_info_block,
|
||||
relation_info_block=relation_info,
|
||||
time_block=time_block,
|
||||
@@ -620,6 +684,7 @@ class DefaultReplyer:
|
||||
chat_info=chat_talking_prompt,
|
||||
memory_block=memory_block,
|
||||
structured_info_block=structured_info_block,
|
||||
tool_info_block=tool_info_block,
|
||||
relation_info_block=relation_info,
|
||||
extra_info_block=extra_info_block,
|
||||
time_block=time_block,
|
||||
|
||||
Reference in New Issue
Block a user