2025-03-24 23:00:14 +08:00
|
|
|
|
import json
|
2025-07-02 02:58:13 +08:00
|
|
|
|
import asyncio
|
2025-07-24 00:46:15 +08:00
|
|
|
|
import inspect
|
|
|
|
|
|
import types
|
2025-05-23 15:30:14 +08:00
|
|
|
|
from collections.abc import Callable
|
|
|
|
|
|
from typing import Annotated, Any
|
2025-03-24 23:00:14 +08:00
|
|
|
|
|
2025-05-23 15:30:14 +08:00
|
|
|
|
from pydantic import BaseModel, Field
|
2025-07-02 02:58:13 +08:00
|
|
|
|
from langchain_core.tools import StructuredTool, tool
|
|
|
|
|
|
from langchain_tavily import TavilySearch
|
2025-05-23 15:30:14 +08:00
|
|
|
|
|
|
|
|
|
|
from src import config, graph_base, knowledge_base
|
2025-07-02 02:58:13 +08:00
|
|
|
|
from src.utils import logger
|
2025-03-24 23:00:14 +08:00
|
|
|
|
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-07-24 00:46:15 +08:00
|
|
|
|
# 工具注册表 - 移到前面以避免NameError
|
|
|
|
|
|
_TOOLS_REGISTRY = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-04-05 17:27:52 +08:00
|
|
|
|
class KnowledgeRetrieverModel(BaseModel):
|
2025-06-16 23:07:28 +08:00
|
|
|
|
query_text: str = Field(
|
2025-05-23 15:30:14 +08:00
|
|
|
|
description=(
|
|
|
|
|
|
"查询的关键词,查询的时候,应该尽量以可能帮助回答这个问题的关键词进行查询,"
|
|
|
|
|
|
"不要直接使用用户的原始输入去查询。"
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2025-04-05 17:27:52 +08:00
|
|
|
|
|
2025-07-24 00:46:15 +08:00
|
|
|
|
def get_runnable_tools():
|
|
|
|
|
|
"""获取所有可运行的工具(给大模型使用)"""
|
2025-04-05 17:27:52 +08:00
|
|
|
|
tools = _TOOLS_REGISTRY.copy()
|
2025-04-11 11:54:45 +08:00
|
|
|
|
|
|
|
|
|
|
# 获取所有知识库
|
2025-04-05 17:27:52 +08:00
|
|
|
|
for db_Id, retrieve_info in knowledge_base.get_retrievers().items():
|
2025-07-24 00:46:15 +08:00
|
|
|
|
_id = f"retrieve_{db_Id[:8]}" # Deepseek does not support non-alphanumeric characters in tool names
|
2025-04-11 11:54:45 +08:00
|
|
|
|
description = (
|
|
|
|
|
|
f"使用 {retrieve_info['name']} 知识库进行检索。\n"
|
|
|
|
|
|
f"下面是这个知识库的描述:\n{retrieve_info['description']}"
|
|
|
|
|
|
)
|
2025-06-27 01:49:31 +08:00
|
|
|
|
|
|
|
|
|
|
# 创建异步工具,确保正确处理异步检索器
|
2025-07-21 18:18:47 +08:00
|
|
|
|
async def async_retriever_wrapper(query_text: str, db_id=db_Id, retriever_info=retrieve_info):
|
2025-06-27 01:49:31 +08:00
|
|
|
|
"""异步检索器包装函数"""
|
2025-07-21 18:18:47 +08:00
|
|
|
|
retriever = retriever_info["retriever"]
|
2025-06-27 01:49:31 +08:00
|
|
|
|
try:
|
|
|
|
|
|
if asyncio.iscoroutinefunction(retriever):
|
|
|
|
|
|
result = await retriever(query_text)
|
|
|
|
|
|
else:
|
|
|
|
|
|
result = retriever(query_text)
|
|
|
|
|
|
return result
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"Error in retriever {db_id}: {e}")
|
|
|
|
|
|
return f"检索失败: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
# 使用 StructuredTool.from_function 创建异步工具
|
2025-07-24 00:46:15 +08:00
|
|
|
|
tools[_id] = StructuredTool.from_function(
|
2025-06-27 01:49:31 +08:00
|
|
|
|
coroutine=async_retriever_wrapper, # 指定为协程
|
2025-07-24 00:46:15 +08:00
|
|
|
|
name=_id,
|
2025-04-11 11:54:45 +08:00
|
|
|
|
description=description,
|
2025-07-24 00:46:15 +08:00
|
|
|
|
args_schema=KnowledgeRetrieverModel,
|
|
|
|
|
|
metadata=retrieve_info
|
2025-06-27 01:49:31 +08:00
|
|
|
|
)
|
2025-04-05 17:27:52 +08:00
|
|
|
|
|
|
|
|
|
|
return tools
|
|
|
|
|
|
|
2025-07-24 00:46:15 +08:00
|
|
|
|
def get_all_tools_info():
|
|
|
|
|
|
"""获取所有工具的信息(用于前端展示)"""
|
|
|
|
|
|
tools_info = {}
|
|
|
|
|
|
|
|
|
|
|
|
tools = get_runnable_tools()
|
|
|
|
|
|
|
|
|
|
|
|
# 获取注册的工具信息
|
|
|
|
|
|
for _id, tool_obj in tools.items():
|
|
|
|
|
|
|
|
|
|
|
|
metadata = getattr(tool_obj, 'metadata', {}) or {}
|
|
|
|
|
|
info = {
|
|
|
|
|
|
"id": _id,
|
|
|
|
|
|
"name": metadata.get('name', _id),
|
|
|
|
|
|
"description": metadata.get('description') or getattr(tool_obj, 'description', ''),
|
|
|
|
|
|
'metadata': metadata,
|
|
|
|
|
|
"args": []
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# 获取工具参数信息
|
|
|
|
|
|
if hasattr(tool_obj, 'args_schema') and tool_obj.args_schema:
|
|
|
|
|
|
schema = tool_obj.args_schema.schema()
|
|
|
|
|
|
if 'properties' in schema:
|
|
|
|
|
|
for arg_name, arg_info in schema['properties'].items():
|
|
|
|
|
|
info["args"].append({
|
|
|
|
|
|
"name": arg_name,
|
|
|
|
|
|
"type": arg_info.get('type', ''),
|
|
|
|
|
|
"description": arg_info.get('description', '')
|
|
|
|
|
|
})
|
|
|
|
|
|
tools_info[info['id']] = info
|
|
|
|
|
|
|
|
|
|
|
|
return tools_info
|
|
|
|
|
|
|
2025-03-24 23:00:14 +08:00
|
|
|
|
class BaseToolOutput:
|
|
|
|
|
|
"""
|
|
|
|
|
|
LLM 要求 Tool 的输出为 str,但 Tool 用在别处时希望它正常返回结构化数据。
|
|
|
|
|
|
只需要将 Tool 返回值用该类封装,能同时满足两者的需要。
|
|
|
|
|
|
基类简单的将返回值字符串化,或指定 format="json" 将其转为 json。
|
|
|
|
|
|
用户也可以继承该类定义自己的转换方法。
|
|
|
|
|
|
"""
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-03-24 23:00:14 +08:00
|
|
|
|
def __init__(
|
|
|
|
|
|
self,
|
|
|
|
|
|
data: Any,
|
2025-07-02 02:58:13 +08:00
|
|
|
|
format: str | Callable | None = None,
|
2025-03-24 23:00:14 +08:00
|
|
|
|
data_alias: str = "",
|
|
|
|
|
|
**extras: Any,
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
self.data = data
|
|
|
|
|
|
self.format = format
|
|
|
|
|
|
self.extras = extras
|
|
|
|
|
|
if data_alias:
|
|
|
|
|
|
setattr(self, data_alias, property(lambda obj: obj.data))
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-03-24 23:00:14 +08:00
|
|
|
|
def __str__(self) -> str:
|
|
|
|
|
|
if self.format == "json":
|
|
|
|
|
|
return json.dumps(self.data, ensure_ascii=False, indent=2)
|
|
|
|
|
|
elif callable(self.format):
|
|
|
|
|
|
return self.format(self)
|
|
|
|
|
|
else:
|
|
|
|
|
|
return str(self.data)
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-03-31 22:20:29 +08:00
|
|
|
|
@tool
|
2025-04-05 17:27:52 +08:00
|
|
|
|
def calculator(a: float, b: float, operation: str) -> float:
|
2025-05-13 19:49:54 +08:00
|
|
|
|
"""Calculate two numbers. operation: add, subtract, multiply, divide"""
|
2025-04-05 17:27:52 +08:00
|
|
|
|
if operation == "add":
|
|
|
|
|
|
return a + b
|
|
|
|
|
|
elif operation == "subtract":
|
|
|
|
|
|
return a - b
|
|
|
|
|
|
elif operation == "multiply":
|
|
|
|
|
|
return a * b
|
|
|
|
|
|
elif operation == "divide":
|
|
|
|
|
|
return a / b
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise ValueError(f"Invalid operation: {operation}, only support add, subtract, multiply, divide")
|
2025-03-31 22:20:29 +08:00
|
|
|
|
|
|
|
|
|
|
@tool
|
2025-05-20 20:49:50 +08:00
|
|
|
|
def query_knowledge_graph(query: Annotated[str, "The keyword to query knowledge graph."]):
|
|
|
|
|
|
"""Use this to query knowledge graph."""
|
2025-04-05 17:27:52 +08:00
|
|
|
|
return graph_base.query_node(query, hops=2)
|
2025-03-31 22:20:29 +08:00
|
|
|
|
|
2025-07-24 00:46:15 +08:00
|
|
|
|
# 更新工具注册表
|
|
|
|
|
|
_TOOLS_REGISTRY.update({
|
2025-05-20 20:49:50 +08:00
|
|
|
|
"Calculator": calculator,
|
|
|
|
|
|
"QueryKnowledgeGraph": query_knowledge_graph,
|
2025-07-24 00:46:15 +08:00
|
|
|
|
})
|
2025-04-08 00:35:29 +08:00
|
|
|
|
|
|
|
|
|
|
if config.enable_web_search:
|
2025-06-18 19:07:29 +08:00
|
|
|
|
_TOOLS_REGISTRY["WebSearchWithTavily"] = TavilySearch(max_results=10)
|