feat: 现在使用工具调用来更新关系和心情而不是固定更新
This commit is contained in:
@@ -21,7 +21,7 @@ class ToolUser:
|
||||
model=global_config.llm_heartflow, temperature=0.2, max_tokens=1000, request_type="tool_use"
|
||||
)
|
||||
|
||||
async def _build_tool_prompt(self, message_txt: str, sender_name: str, chat_stream: ChatStream):
|
||||
async def _build_tool_prompt(self, message_txt: str, sender_name: str, chat_stream: ChatStream, reply_message:str = ""):
|
||||
"""构建工具使用的提示词
|
||||
|
||||
Args:
|
||||
@@ -45,9 +45,11 @@ class ToolUser:
|
||||
prompt = ""
|
||||
prompt += "你正在思考如何回复群里的消息。\n"
|
||||
prompt += f"你注意到{sender_name}刚刚说:{message_txt}\n"
|
||||
if reply_message:
|
||||
prompt += f"你刚刚回复的内容是:{reply_message}\n"
|
||||
prompt += f"注意你就是{bot_name},{bot_name}指的就是你。"
|
||||
|
||||
prompt += "你现在需要对群里的聊天内容进行回复,现在请你思考,你是否需要额外的信息,或者一些工具来帮你回复,不要使用危险功能(比如文件操作或者系统操作爬虫),比如回忆或者搜寻已有的知识,或者了解你现在正在做什么,请输出你需要的工具,或者你需要的额外信息。"
|
||||
prompt += "你现在需要对群里的聊天内容进行回复,现在选择工具来对消息和你的回复进行处理,你是否需要额外的信息,或者进行一些动作,比如回忆或者搜寻已有的知识,改变关系和情感,或者了解你现在正在做什么,请输出你需要的工具,或者你需要的额外信息。"
|
||||
return prompt
|
||||
|
||||
def _define_tools(self):
|
||||
@@ -81,10 +83,26 @@ class ToolUser:
|
||||
# 执行工具
|
||||
result = await tool_instance.execute(function_args, message_txt)
|
||||
if result:
|
||||
# 根据工具名称确定类型标签
|
||||
tool_type = ""
|
||||
if "memory" in function_name.lower():
|
||||
tool_type = "memory"
|
||||
elif "schedule" in function_name.lower() or "task" in function_name.lower():
|
||||
tool_type = "schedule"
|
||||
elif "knowledge" in function_name.lower():
|
||||
tool_type = "knowledge"
|
||||
elif "change_relationship" in function_name.lower():
|
||||
tool_type = "change_relationship"
|
||||
elif "change_mood" in function_name.lower():
|
||||
tool_type = "change_mood"
|
||||
else:
|
||||
tool_type = "other"
|
||||
|
||||
return {
|
||||
"tool_call_id": tool_call["id"],
|
||||
"role": "tool",
|
||||
"name": function_name,
|
||||
"type": tool_type,
|
||||
"content": result["content"],
|
||||
}
|
||||
return None
|
||||
@@ -101,7 +119,7 @@ class ToolUser:
|
||||
chat_stream: 聊天流对象
|
||||
|
||||
Returns:
|
||||
dict: 工具使用结果
|
||||
dict: 工具使用结果,包含结构化的信息
|
||||
"""
|
||||
try:
|
||||
# 构建提示词
|
||||
@@ -109,6 +127,7 @@ class ToolUser:
|
||||
|
||||
# 定义可用工具
|
||||
tools = self._define_tools()
|
||||
logger.trace(f"工具定义: {tools}")
|
||||
|
||||
# 使用llm_model_tool发送带工具定义的请求
|
||||
payload = {
|
||||
@@ -119,7 +138,7 @@ class ToolUser:
|
||||
"temperature": 0.2,
|
||||
}
|
||||
|
||||
logger.debug(f"发送工具调用请求,模型: {self.llm_model_tool.model_name}")
|
||||
logger.trace(f"发送工具调用请求,模型: {self.llm_model_tool.model_name}")
|
||||
# 发送请求获取模型是否需要调用工具
|
||||
response = await self.llm_model_tool._execute_request(
|
||||
endpoint="/chat/completions", payload=payload, prompt=prompt
|
||||
@@ -128,36 +147,50 @@ class ToolUser:
|
||||
# 根据返回值数量判断是否有工具调用
|
||||
if len(response) == 3:
|
||||
content, reasoning_content, tool_calls = response
|
||||
logger.info(f"工具思考: {tool_calls}")
|
||||
# logger.info(f"工具思考: {tool_calls}")
|
||||
# logger.debug(f"工具思考: {content}")
|
||||
|
||||
# 检查响应中工具调用是否有效
|
||||
if not tool_calls:
|
||||
logger.info("模型返回了空的tool_calls列表")
|
||||
logger.debug("模型返回了空的tool_calls列表")
|
||||
return {"used_tools": False}
|
||||
|
||||
logger.info(f"模型请求调用{len(tool_calls)}个工具")
|
||||
tool_calls_str = ""
|
||||
for tool_call in tool_calls:
|
||||
tool_calls_str += f"{tool_call['function']['name']}\n"
|
||||
logger.info(f"模型请求调用{len(tool_calls)}个工具: {tool_calls_str}")
|
||||
tool_results = []
|
||||
collected_info = ""
|
||||
structured_info = {
|
||||
"memory": [],
|
||||
"schedule": [],
|
||||
"knowledge": [],
|
||||
"change_relationship": [],
|
||||
"change_mood": [],
|
||||
"other": []
|
||||
}
|
||||
|
||||
# 执行所有工具调用
|
||||
for tool_call in tool_calls:
|
||||
result = await self._execute_tool_call(tool_call, message_txt)
|
||||
if result:
|
||||
tool_results.append(result)
|
||||
# 将工具结果添加到收集的信息中
|
||||
collected_info += f"\n{result['name']}返回结果: {result['content']}\n"
|
||||
# 将工具结果添加到对应类型的列表中
|
||||
structured_info[result["type"]].append({
|
||||
"name": result["name"],
|
||||
"content": result["content"]
|
||||
})
|
||||
|
||||
# 如果有工具结果,直接返回收集的信息
|
||||
if collected_info:
|
||||
logger.info(f"工具调用收集到信息: {collected_info}")
|
||||
# 如果有工具结果,返回结构化的信息
|
||||
if any(structured_info.values()):
|
||||
logger.info(f"工具调用收集到结构化信息: {json.dumps(structured_info, ensure_ascii=False)}")
|
||||
return {
|
||||
"used_tools": True,
|
||||
"collected_info": collected_info,
|
||||
"structured_info": structured_info
|
||||
}
|
||||
else:
|
||||
# 没有工具调用
|
||||
content, reasoning_content = response
|
||||
logger.info("模型没有请求调用任何工具")
|
||||
logger.debug("模型没有请求调用任何工具")
|
||||
|
||||
# 如果没有工具调用或处理失败,直接返回原始思考
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user