better:心流升级,大大减少了复读情况,并且灵活调用工具来实现知识和记忆检索
This commit is contained in:
102
src/do_tool/tool_can_use/README.md
Normal file
102
src/do_tool/tool_can_use/README.md
Normal file
@@ -0,0 +1,102 @@
|
||||
# 工具系统使用指南
|
||||
|
||||
## 概述
|
||||
|
||||
`tool_can_use` 是一个插件式工具系统,允许轻松扩展和注册新工具。每个工具作为独立的文件存在于该目录下,系统会自动发现和注册这些工具。
|
||||
|
||||
## 工具结构
|
||||
|
||||
每个工具应该继承 `BaseTool` 基类并实现必要的属性和方法:
|
||||
|
||||
```python
|
||||
from src.do_tool.tool_can_use.base_tool import BaseTool, register_tool
|
||||
|
||||
class MyNewTool(BaseTool):
|
||||
# 工具名称,必须唯一
|
||||
name = "my_new_tool"
|
||||
|
||||
# 工具描述,告诉LLM这个工具的用途
|
||||
description = "这是一个新工具,用于..."
|
||||
|
||||
# 工具参数定义,遵循JSONSchema格式
|
||||
parameters = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"param1": {
|
||||
"type": "string",
|
||||
"description": "参数1的描述"
|
||||
},
|
||||
"param2": {
|
||||
"type": "integer",
|
||||
"description": "参数2的描述"
|
||||
}
|
||||
},
|
||||
"required": ["param1"] # 必需的参数列表
|
||||
}
|
||||
|
||||
async def execute(self, function_args, message_txt=""):
|
||||
"""执行工具逻辑
|
||||
|
||||
Args:
|
||||
function_args: 工具调用参数
|
||||
message_txt: 原始消息文本
|
||||
|
||||
Returns:
|
||||
Dict: 包含执行结果的字典,必须包含name和content字段
|
||||
"""
|
||||
# 实现工具逻辑
|
||||
result = f"工具执行结果: {function_args.get('param1')}"
|
||||
|
||||
return {
|
||||
"name": self.name,
|
||||
"content": result
|
||||
}
|
||||
|
||||
# 注册工具
|
||||
register_tool(MyNewTool)
|
||||
```
|
||||
|
||||
## 自动注册机制
|
||||
|
||||
工具系统通过以下步骤自动注册工具:
|
||||
|
||||
1. 在`__init__.py`中,`discover_tools()`函数会自动遍历当前目录中的所有Python文件
|
||||
2. 对于每个文件,系统会寻找继承自`BaseTool`的类
|
||||
3. 这些类会被自动注册到工具注册表中
|
||||
|
||||
只要确保在每个工具文件的末尾调用`register_tool(YourToolClass)`,工具就会被自动注册。
|
||||
|
||||
## 添加新工具步骤
|
||||
|
||||
1. 在`tool_can_use`目录下创建新的Python文件(如`my_new_tool.py`)
|
||||
2. 导入`BaseTool`和`register_tool`
|
||||
3. 创建继承自`BaseTool`的工具类
|
||||
4. 实现必要的属性(`name`, `description`, `parameters`)
|
||||
5. 实现`execute`方法
|
||||
6. 使用`register_tool`注册工具
|
||||
|
||||
## 与ToolUser整合
|
||||
|
||||
`ToolUser`类已经更新为使用这个新的工具系统,它会:
|
||||
|
||||
1. 自动获取所有已注册工具的定义
|
||||
2. 基于工具名称找到对应的工具实例
|
||||
3. 调用工具的`execute`方法
|
||||
|
||||
## 使用示例
|
||||
|
||||
```python
|
||||
from src.do_tool.tool_use import ToolUser
|
||||
|
||||
# 创建工具用户
|
||||
tool_user = ToolUser()
|
||||
|
||||
# 使用工具
|
||||
result = await tool_user.use_tool(message_txt="查询关于Python的知识", sender_name="用户", chat_stream=chat_stream)
|
||||
|
||||
# 处理结果
|
||||
if result["used_tools"]:
|
||||
print("工具使用结果:", result["collected_info"])
|
||||
else:
|
||||
print("未使用工具")
|
||||
```
|
||||
11
src/do_tool/tool_can_use/__init__.py
Normal file
11
src/do_tool/tool_can_use/__init__.py
Normal file
@@ -0,0 +1,11 @@
|
||||
from src.do_tool.tool_can_use.base_tool import (
|
||||
BaseTool,
|
||||
register_tool,
|
||||
discover_tools,
|
||||
get_all_tool_definitions,
|
||||
get_tool_instance,
|
||||
TOOL_REGISTRY
|
||||
)
|
||||
|
||||
# 自动发现并注册工具
|
||||
discover_tools()
|
||||
119
src/do_tool/tool_can_use/base_tool.py
Normal file
119
src/do_tool/tool_can_use/base_tool.py
Normal file
@@ -0,0 +1,119 @@
|
||||
from typing import Dict, List, Any, Optional, Union, Type
|
||||
import inspect
|
||||
import importlib
|
||||
import pkgutil
|
||||
import os
|
||||
from src.common.logger import get_module_logger
|
||||
|
||||
logger = get_module_logger("base_tool")
|
||||
|
||||
# 工具注册表
|
||||
TOOL_REGISTRY = {}
|
||||
|
||||
class BaseTool:
|
||||
"""所有工具的基类"""
|
||||
# 工具名称,子类必须重写
|
||||
name = None
|
||||
# 工具描述,子类必须重写
|
||||
description = None
|
||||
# 工具参数定义,子类必须重写
|
||||
parameters = None
|
||||
|
||||
@classmethod
|
||||
def get_tool_definition(cls) -> Dict[str, Any]:
|
||||
"""获取工具定义,用于LLM工具调用
|
||||
|
||||
Returns:
|
||||
Dict: 工具定义字典
|
||||
"""
|
||||
if not cls.name or not cls.description or not cls.parameters:
|
||||
raise NotImplementedError(f"工具类 {cls.__name__} 必须定义 name, description 和 parameters 属性")
|
||||
|
||||
return {
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": cls.name,
|
||||
"description": cls.description,
|
||||
"parameters": cls.parameters
|
||||
}
|
||||
}
|
||||
|
||||
async def execute(self, function_args: Dict[str, Any], message_txt: str = "") -> Dict[str, Any]:
|
||||
"""执行工具函数
|
||||
|
||||
Args:
|
||||
function_args: 工具调用参数
|
||||
message_txt: 原始消息文本
|
||||
|
||||
Returns:
|
||||
Dict: 工具执行结果
|
||||
"""
|
||||
raise NotImplementedError("子类必须实现execute方法")
|
||||
|
||||
|
||||
def register_tool(tool_class: Type[BaseTool]):
|
||||
"""注册工具到全局注册表
|
||||
|
||||
Args:
|
||||
tool_class: 工具类
|
||||
"""
|
||||
if not issubclass(tool_class, BaseTool):
|
||||
raise TypeError(f"{tool_class.__name__} 不是 BaseTool 的子类")
|
||||
|
||||
tool_name = tool_class.name
|
||||
if not tool_name:
|
||||
raise ValueError(f"工具类 {tool_class.__name__} 没有定义 name 属性")
|
||||
|
||||
TOOL_REGISTRY[tool_name] = tool_class
|
||||
logger.info(f"已注册工具: {tool_name}")
|
||||
|
||||
|
||||
def discover_tools():
|
||||
"""自动发现并注册tool_can_use目录下的所有工具"""
|
||||
# 获取当前目录路径
|
||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
package_name = os.path.basename(current_dir)
|
||||
parent_dir = os.path.dirname(current_dir)
|
||||
|
||||
# 导入当前包
|
||||
package = importlib.import_module(f"src.do_tool.{package_name}")
|
||||
|
||||
# 遍历包中的所有模块
|
||||
for _, module_name, is_pkg in pkgutil.iter_modules([current_dir]):
|
||||
# 跳过当前模块和__pycache__
|
||||
if module_name == "base_tool" or module_name.startswith("__"):
|
||||
continue
|
||||
|
||||
# 导入模块
|
||||
module = importlib.import_module(f"src.do_tool.{package_name}.{module_name}")
|
||||
|
||||
# 查找模块中的工具类
|
||||
for name, obj in inspect.getmembers(module):
|
||||
if inspect.isclass(obj) and issubclass(obj, BaseTool) and obj != BaseTool:
|
||||
register_tool(obj)
|
||||
|
||||
logger.info(f"工具发现完成,共注册 {len(TOOL_REGISTRY)} 个工具")
|
||||
|
||||
|
||||
def get_all_tool_definitions() -> List[Dict[str, Any]]:
|
||||
"""获取所有已注册工具的定义
|
||||
|
||||
Returns:
|
||||
List[Dict]: 工具定义列表
|
||||
"""
|
||||
return [tool_class().get_tool_definition() for tool_class in TOOL_REGISTRY.values()]
|
||||
|
||||
|
||||
def get_tool_instance(tool_name: str) -> Optional[BaseTool]:
|
||||
"""获取指定名称的工具实例
|
||||
|
||||
Args:
|
||||
tool_name: 工具名称
|
||||
|
||||
Returns:
|
||||
Optional[BaseTool]: 工具实例,如果找不到则返回None
|
||||
"""
|
||||
tool_class = TOOL_REGISTRY.get(tool_name)
|
||||
if not tool_class:
|
||||
return None
|
||||
return tool_class()
|
||||
63
src/do_tool/tool_can_use/get_current_task.py
Normal file
63
src/do_tool/tool_can_use/get_current_task.py
Normal file
@@ -0,0 +1,63 @@
|
||||
from src.do_tool.tool_can_use.base_tool import BaseTool, register_tool
|
||||
from src.plugins.schedule.schedule_generator import bot_schedule
|
||||
from src.common.logger import get_module_logger
|
||||
from typing import Dict, Any
|
||||
|
||||
logger = get_module_logger("get_current_task_tool")
|
||||
|
||||
class GetCurrentTaskTool(BaseTool):
|
||||
"""获取当前正在做的事情/最近的任务工具"""
|
||||
name = "get_current_task"
|
||||
description = "获取当前正在做的事情/最近的任务"
|
||||
parameters = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"num": {
|
||||
"type": "integer",
|
||||
"description": "要获取的任务数量"
|
||||
},
|
||||
"time_info": {
|
||||
"type": "boolean",
|
||||
"description": "是否包含时间信息"
|
||||
}
|
||||
},
|
||||
"required": []
|
||||
}
|
||||
|
||||
async def execute(self, function_args: Dict[str, Any], message_txt: str = "") -> Dict[str, Any]:
|
||||
"""执行获取当前任务
|
||||
|
||||
Args:
|
||||
function_args: 工具参数
|
||||
message_txt: 原始消息文本,此工具不使用
|
||||
|
||||
Returns:
|
||||
Dict: 工具执行结果
|
||||
"""
|
||||
try:
|
||||
# 获取参数,如果没有提供则使用默认值
|
||||
num = function_args.get("num", 1)
|
||||
time_info = function_args.get("time_info", False)
|
||||
|
||||
# 调用日程系统获取当前任务
|
||||
current_task = bot_schedule.get_current_num_task(num=num, time_info=time_info)
|
||||
|
||||
# 格式化返回结果
|
||||
if current_task:
|
||||
task_info = current_task
|
||||
else:
|
||||
task_info = "当前没有正在进行的任务"
|
||||
|
||||
return {
|
||||
"name": "get_current_task",
|
||||
"content": f"当前任务信息: {task_info}"
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"获取当前任务工具执行失败: {str(e)}")
|
||||
return {
|
||||
"name": "get_current_task",
|
||||
"content": f"获取当前任务失败: {str(e)}"
|
||||
}
|
||||
|
||||
# 注册工具
|
||||
register_tool(GetCurrentTaskTool)
|
||||
147
src/do_tool/tool_can_use/get_knowledge.py
Normal file
147
src/do_tool/tool_can_use/get_knowledge.py
Normal file
@@ -0,0 +1,147 @@
|
||||
from src.do_tool.tool_can_use.base_tool import BaseTool, register_tool
|
||||
from src.plugins.chat.utils import get_embedding
|
||||
from src.common.database import db
|
||||
from src.common.logger import get_module_logger
|
||||
from typing import Dict, Any, Union, List
|
||||
|
||||
logger = get_module_logger("get_knowledge_tool")
|
||||
|
||||
class SearchKnowledgeTool(BaseTool):
|
||||
"""从知识库中搜索相关信息的工具"""
|
||||
name = "search_knowledge"
|
||||
description = "从知识库中搜索相关信息"
|
||||
parameters = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"query": {
|
||||
"type": "string",
|
||||
"description": "搜索查询关键词"
|
||||
},
|
||||
"threshold": {
|
||||
"type": "number",
|
||||
"description": "相似度阈值,0.0到1.0之间"
|
||||
}
|
||||
},
|
||||
"required": ["query"]
|
||||
}
|
||||
|
||||
async def execute(self, function_args: Dict[str, Any], message_txt: str = "") -> Dict[str, Any]:
|
||||
"""执行知识库搜索
|
||||
|
||||
Args:
|
||||
function_args: 工具参数
|
||||
message_txt: 原始消息文本
|
||||
|
||||
Returns:
|
||||
Dict: 工具执行结果
|
||||
"""
|
||||
try:
|
||||
query = function_args.get("query", message_txt)
|
||||
threshold = function_args.get("threshold", 0.4)
|
||||
|
||||
# 调用知识库搜索
|
||||
embedding = await get_embedding(query, request_type="info_retrieval")
|
||||
if embedding:
|
||||
knowledge_info = self.get_info_from_db(embedding, limit=3, threshold=threshold)
|
||||
if knowledge_info:
|
||||
content = f"你知道这些知识: {knowledge_info}"
|
||||
else:
|
||||
content = f"你不太了解有关{query}的知识"
|
||||
return {
|
||||
"name": "search_knowledge",
|
||||
"content": content
|
||||
}
|
||||
return {
|
||||
"name": "search_knowledge",
|
||||
"content": f"无法获取关于'{query}'的嵌入向量"
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"知识库搜索工具执行失败: {str(e)}")
|
||||
return {
|
||||
"name": "search_knowledge",
|
||||
"content": f"知识库搜索失败: {str(e)}"
|
||||
}
|
||||
|
||||
def get_info_from_db(
|
||||
self, query_embedding: list, limit: int = 1, threshold: float = 0.5, return_raw: bool = False
|
||||
) -> Union[str, list]:
|
||||
"""从数据库中获取相关信息
|
||||
|
||||
Args:
|
||||
query_embedding: 查询的嵌入向量
|
||||
limit: 最大返回结果数
|
||||
threshold: 相似度阈值
|
||||
return_raw: 是否返回原始结果
|
||||
|
||||
Returns:
|
||||
Union[str, list]: 格式化的信息字符串或原始结果列表
|
||||
"""
|
||||
if not query_embedding:
|
||||
return "" if not return_raw else []
|
||||
|
||||
# 使用余弦相似度计算
|
||||
pipeline = [
|
||||
{
|
||||
"$addFields": {
|
||||
"dotProduct": {
|
||||
"$reduce": {
|
||||
"input": {"$range": [0, {"$size": "$embedding"}]},
|
||||
"initialValue": 0,
|
||||
"in": {
|
||||
"$add": [
|
||||
"$$value",
|
||||
{
|
||||
"$multiply": [
|
||||
{"$arrayElemAt": ["$embedding", "$$this"]},
|
||||
{"$arrayElemAt": [query_embedding, "$$this"]},
|
||||
]
|
||||
},
|
||||
]
|
||||
},
|
||||
}
|
||||
},
|
||||
"magnitude1": {
|
||||
"$sqrt": {
|
||||
"$reduce": {
|
||||
"input": "$embedding",
|
||||
"initialValue": 0,
|
||||
"in": {"$add": ["$$value", {"$multiply": ["$$this", "$$this"]}]},
|
||||
}
|
||||
}
|
||||
},
|
||||
"magnitude2": {
|
||||
"$sqrt": {
|
||||
"$reduce": {
|
||||
"input": query_embedding,
|
||||
"initialValue": 0,
|
||||
"in": {"$add": ["$$value", {"$multiply": ["$$this", "$$this"]}]},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
{"$addFields": {"similarity": {"$divide": ["$dotProduct", {"$multiply": ["$magnitude1", "$magnitude2"]}]}}},
|
||||
{
|
||||
"$match": {
|
||||
"similarity": {"$gte": threshold} # 只保留相似度大于等于阈值的结果
|
||||
}
|
||||
},
|
||||
{"$sort": {"similarity": -1}},
|
||||
{"$limit": limit},
|
||||
{"$project": {"content": 1, "similarity": 1}},
|
||||
]
|
||||
|
||||
results = list(db.knowledges.aggregate(pipeline))
|
||||
logger.debug(f"知识库查询结果数量: {len(results)}")
|
||||
|
||||
if not results:
|
||||
return "" if not return_raw else []
|
||||
|
||||
if return_raw:
|
||||
return results
|
||||
else:
|
||||
# 返回所有找到的内容,用换行分隔
|
||||
return "\n".join(str(result["content"]) for result in results)
|
||||
|
||||
# 注册工具
|
||||
register_tool(SearchKnowledgeTool)
|
||||
72
src/do_tool/tool_can_use/get_memory.py
Normal file
72
src/do_tool/tool_can_use/get_memory.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from src.do_tool.tool_can_use.base_tool import BaseTool, register_tool
|
||||
from src.plugins.memory_system.Hippocampus import HippocampusManager
|
||||
from src.common.logger import get_module_logger
|
||||
from typing import Dict, Any
|
||||
|
||||
logger = get_module_logger("get_memory_tool")
|
||||
|
||||
class GetMemoryTool(BaseTool):
|
||||
"""从记忆系统中获取相关记忆的工具"""
|
||||
name = "get_memory"
|
||||
description = "从记忆系统中获取相关记忆"
|
||||
parameters = {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"text": {
|
||||
"type": "string",
|
||||
"description": "要查询的相关文本"
|
||||
},
|
||||
"max_memory_num": {
|
||||
"type": "integer",
|
||||
"description": "最大返回记忆数量"
|
||||
}
|
||||
},
|
||||
"required": ["text"]
|
||||
}
|
||||
|
||||
async def execute(self, function_args: Dict[str, Any], message_txt: str = "") -> Dict[str, Any]:
|
||||
"""执行记忆获取
|
||||
|
||||
Args:
|
||||
function_args: 工具参数
|
||||
message_txt: 原始消息文本
|
||||
|
||||
Returns:
|
||||
Dict: 工具执行结果
|
||||
"""
|
||||
try:
|
||||
text = function_args.get("text", message_txt)
|
||||
max_memory_num = function_args.get("max_memory_num", 2)
|
||||
|
||||
# 调用记忆系统
|
||||
related_memory = await HippocampusManager.get_instance().get_memory_from_text(
|
||||
text=text,
|
||||
max_memory_num=max_memory_num,
|
||||
max_memory_length=2,
|
||||
max_depth=3,
|
||||
fast_retrieval=False
|
||||
)
|
||||
|
||||
memory_info = ""
|
||||
if related_memory:
|
||||
for memory in related_memory:
|
||||
memory_info += memory[1] + "\n"
|
||||
|
||||
if memory_info:
|
||||
content = f"你记得这些事情: {memory_info}"
|
||||
else:
|
||||
content = f"你不太记得有关{text}的记忆,你对此不太了解"
|
||||
|
||||
return {
|
||||
"name": "get_memory",
|
||||
"content": content
|
||||
}
|
||||
except Exception as e:
|
||||
logger.error(f"记忆获取工具执行失败: {str(e)}")
|
||||
return {
|
||||
"name": "get_memory",
|
||||
"content": f"记忆获取失败: {str(e)}"
|
||||
}
|
||||
|
||||
# 注册工具
|
||||
register_tool(GetMemoryTool)
|
||||
172
src/do_tool/tool_use.py
Normal file
172
src/do_tool/tool_use.py
Normal file
@@ -0,0 +1,172 @@
|
||||
from src.plugins.models.utils_model import LLM_request
|
||||
from src.plugins.config.config import global_config
|
||||
from src.plugins.chat.chat_stream import ChatStream
|
||||
from src.common.database import db
|
||||
import time
|
||||
import json
|
||||
from src.common.logger import get_module_logger
|
||||
from typing import Union
|
||||
from src.do_tool.tool_can_use import get_all_tool_definitions, get_tool_instance
|
||||
|
||||
logger = get_module_logger("tool_use")
|
||||
|
||||
|
||||
class ToolUser:
|
||||
def __init__(self):
|
||||
self.llm_model_tool = LLM_request(
|
||||
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):
|
||||
"""构建工具使用的提示词
|
||||
|
||||
Args:
|
||||
message_txt: 用户消息文本
|
||||
sender_name: 发送者名称
|
||||
chat_stream: 聊天流对象
|
||||
|
||||
Returns:
|
||||
str: 构建好的提示词
|
||||
"""
|
||||
new_messages = list(
|
||||
db.messages.find({"chat_id": chat_stream.stream_id, "time": {"$gt": time.time()}})
|
||||
.sort("time", 1)
|
||||
.limit(15)
|
||||
)
|
||||
new_messages_str = ""
|
||||
for msg in new_messages:
|
||||
if "detailed_plain_text" in msg:
|
||||
new_messages_str += f"{msg['detailed_plain_text']}"
|
||||
|
||||
# 这些信息应该从调用者传入,而不是从self获取
|
||||
bot_name = global_config.BOT_NICKNAME
|
||||
prompt = ""
|
||||
prompt += "你正在思考如何回复群里的消息。\n"
|
||||
prompt += f"你注意到{sender_name}刚刚说:{message_txt}\n"
|
||||
prompt += f"注意你就是{bot_name},{bot_name}指的就是你。"
|
||||
prompt += "你现在需要对群里的聊天内容进行回复,现在请你思考,你是否需要额外的信息,或者一些工具来帮你回复,比如回忆或者搜寻已有的知识,或者了解你现在正在做什么,请输出你需要的工具,或者你需要的额外信息。"
|
||||
|
||||
return prompt
|
||||
|
||||
def _define_tools(self):
|
||||
"""获取所有已注册工具的定义
|
||||
|
||||
Returns:
|
||||
list: 工具定义列表
|
||||
"""
|
||||
return get_all_tool_definitions()
|
||||
|
||||
async def _execute_tool_call(self, tool_call, message_txt:str):
|
||||
"""执行特定的工具调用
|
||||
|
||||
Args:
|
||||
tool_call: 工具调用对象
|
||||
message_txt: 原始消息文本
|
||||
|
||||
Returns:
|
||||
dict: 工具调用结果
|
||||
"""
|
||||
try:
|
||||
function_name = tool_call["function"]["name"]
|
||||
function_args = json.loads(tool_call["function"]["arguments"])
|
||||
|
||||
# 获取对应工具实例
|
||||
tool_instance = get_tool_instance(function_name)
|
||||
if not tool_instance:
|
||||
logger.warning(f"未知工具名称: {function_name}")
|
||||
return None
|
||||
|
||||
# 执行工具
|
||||
result = await tool_instance.execute(function_args, message_txt)
|
||||
if result:
|
||||
return {
|
||||
"tool_call_id": tool_call["id"],
|
||||
"role": "tool",
|
||||
"name": function_name,
|
||||
"content": result["content"]
|
||||
}
|
||||
return None
|
||||
except Exception as e:
|
||||
logger.error(f"执行工具调用时发生错误: {str(e)}")
|
||||
return None
|
||||
|
||||
async def use_tool(self, message_txt:str, sender_name:str, chat_stream:ChatStream):
|
||||
"""使用工具辅助思考,判断是否需要额外信息
|
||||
|
||||
Args:
|
||||
message_txt: 用户消息文本
|
||||
sender_name: 发送者名称
|
||||
chat_stream: 聊天流对象
|
||||
|
||||
Returns:
|
||||
dict: 工具使用结果
|
||||
"""
|
||||
try:
|
||||
# 构建提示词
|
||||
prompt = await self._build_tool_prompt(message_txt, sender_name, chat_stream)
|
||||
|
||||
# 定义可用工具
|
||||
tools = self._define_tools()
|
||||
|
||||
# 使用llm_model_tool发送带工具定义的请求
|
||||
payload = {
|
||||
"model": self.llm_model_tool.model_name,
|
||||
"messages": [{"role": "user", "content": prompt}],
|
||||
"max_tokens": global_config.max_response_length,
|
||||
"tools": tools,
|
||||
"temperature": 0.2
|
||||
}
|
||||
|
||||
logger.debug(f"发送工具调用请求,模型: {self.llm_model_tool.model_name}")
|
||||
# 发送请求获取模型是否需要调用工具
|
||||
response = await self.llm_model_tool._execute_request(
|
||||
endpoint="/chat/completions",
|
||||
payload=payload,
|
||||
prompt=prompt
|
||||
)
|
||||
|
||||
# 根据返回值数量判断是否有工具调用
|
||||
if len(response) == 3:
|
||||
content, reasoning_content, tool_calls = response
|
||||
logger.info(f"工具思考: {tool_calls}")
|
||||
|
||||
# 检查响应中工具调用是否有效
|
||||
if not tool_calls:
|
||||
logger.info("模型返回了空的tool_calls列表")
|
||||
return {"used_tools": False}
|
||||
|
||||
logger.info(f"模型请求调用{len(tool_calls)}个工具")
|
||||
tool_results = []
|
||||
collected_info = ""
|
||||
|
||||
# 执行所有工具调用
|
||||
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"
|
||||
|
||||
# 如果有工具结果,直接返回收集的信息
|
||||
if collected_info:
|
||||
logger.info(f"工具调用收集到信息: {collected_info}")
|
||||
return {
|
||||
"used_tools": True,
|
||||
"collected_info": collected_info,
|
||||
}
|
||||
else:
|
||||
# 没有工具调用
|
||||
content, reasoning_content = response
|
||||
logger.info("模型没有请求调用任何工具")
|
||||
|
||||
# 如果没有工具调用或处理失败,直接返回原始思考
|
||||
return {
|
||||
"used_tools": False,
|
||||
}
|
||||
|
||||
except Exception as e:
|
||||
logger.error(f"工具调用过程中出错: {str(e)}")
|
||||
return {
|
||||
"used_tools": False,
|
||||
"error": str(e),
|
||||
}
|
||||
Reference in New Issue
Block a user