2025-03-24 23:00:14 +08:00
|
|
|
|
import json
|
|
|
|
|
|
import re
|
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-04-02 00:00:04 +08:00
|
|
|
|
from langchain_community.tools.tavily_search import TavilySearchResults
|
2025-05-23 15:30:14 +08:00
|
|
|
|
from langchain_core.tools import BaseTool, StructuredTool, tool
|
|
|
|
|
|
from pydantic import BaseModel, Field
|
|
|
|
|
|
|
|
|
|
|
|
from src import config, graph_base, knowledge_base
|
2025-03-24 23:00:14 +08:00
|
|
|
|
|
2025-04-05 17:27:52 +08:00
|
|
|
|
|
2025-03-24 23:00:14 +08:00
|
|
|
|
# refs https://github.com/chatchat-space/LangGraph-Chatchat chatchat-server/chatchat/server/agent/tools_factory/tools_registry.py
|
|
|
|
|
|
def regist_tool(
|
|
|
|
|
|
*args: Any,
|
|
|
|
|
|
title: str = "",
|
|
|
|
|
|
description: str = "",
|
|
|
|
|
|
return_direct: bool = False,
|
2025-05-23 15:30:14 +08:00
|
|
|
|
args_schema: type[BaseModel] | None = None,
|
2025-03-24 23:00:14 +08:00
|
|
|
|
infer_schema: bool = True,
|
2025-05-23 15:30:14 +08:00
|
|
|
|
) -> Callable | BaseTool:
|
2025-03-24 23:00:14 +08:00
|
|
|
|
"""
|
|
|
|
|
|
wrapper of langchain tool decorator
|
|
|
|
|
|
add tool to registry automatically
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_tool(t: BaseTool):
|
|
|
|
|
|
nonlocal description, title
|
|
|
|
|
|
|
|
|
|
|
|
_TOOLS_REGISTRY[t.name] = t
|
|
|
|
|
|
|
|
|
|
|
|
# change default description
|
|
|
|
|
|
if not description:
|
|
|
|
|
|
if t.func is not None:
|
|
|
|
|
|
description = t.func.__doc__
|
|
|
|
|
|
elif t.coroutine is not None:
|
|
|
|
|
|
description = t.coroutine.__doc__
|
|
|
|
|
|
t.description = " ".join(re.split(r"\n+\s*", description))
|
|
|
|
|
|
# set a default title for human
|
|
|
|
|
|
if not title:
|
|
|
|
|
|
title = "".join([x.capitalize() for x in t.name.split("_")])
|
|
|
|
|
|
setattr(t, "_title", title)
|
|
|
|
|
|
|
|
|
|
|
|
def wrapper(def_func: Callable) -> BaseTool:
|
|
|
|
|
|
partial_ = tool(
|
|
|
|
|
|
*args,
|
|
|
|
|
|
return_direct=return_direct,
|
|
|
|
|
|
args_schema=args_schema,
|
|
|
|
|
|
infer_schema=infer_schema,
|
|
|
|
|
|
)
|
|
|
|
|
|
t = partial_(def_func)
|
|
|
|
|
|
_parse_tool(t)
|
|
|
|
|
|
return t
|
|
|
|
|
|
|
|
|
|
|
|
if len(args) == 0:
|
|
|
|
|
|
return wrapper
|
|
|
|
|
|
else:
|
|
|
|
|
|
t = tool(
|
|
|
|
|
|
*args,
|
|
|
|
|
|
return_direct=return_direct,
|
|
|
|
|
|
args_schema=args_schema,
|
|
|
|
|
|
infer_schema=infer_schema,
|
|
|
|
|
|
)
|
|
|
|
|
|
_parse_tool(t)
|
|
|
|
|
|
return t
|
|
|
|
|
|
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-04-05 17:27:52 +08:00
|
|
|
|
class KnowledgeRetrieverModel(BaseModel):
|
2025-05-23 15:30:14 +08:00
|
|
|
|
query: str = Field(
|
|
|
|
|
|
description=(
|
|
|
|
|
|
"查询的关键词,查询的时候,应该尽量以可能帮助回答这个问题的关键词进行查询,"
|
|
|
|
|
|
"不要直接使用用户的原始输入去查询。"
|
|
|
|
|
|
)
|
|
|
|
|
|
)
|
2025-04-05 17:27:52 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_tools():
|
|
|
|
|
|
"""获取所有工具"""
|
|
|
|
|
|
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():
|
|
|
|
|
|
name = f"retrieve_{retrieve_info['name']}"
|
2025-04-11 11:54:45 +08:00
|
|
|
|
description = (
|
|
|
|
|
|
f"使用 {retrieve_info['name']} 知识库进行检索。\n"
|
|
|
|
|
|
f"下面是这个知识库的描述:\n{retrieve_info['description']}"
|
|
|
|
|
|
)
|
2025-04-05 17:27:52 +08:00
|
|
|
|
tools[name] = StructuredTool.from_function(
|
|
|
|
|
|
retrieve_info["retriever"],
|
|
|
|
|
|
name=name,
|
2025-04-11 11:54:45 +08:00
|
|
|
|
description=description,
|
2025-04-05 17:27:52 +08:00
|
|
|
|
args_schema=KnowledgeRetrieverModel)
|
|
|
|
|
|
|
|
|
|
|
|
return tools
|
|
|
|
|
|
|
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,
|
|
|
|
|
|
format: str | Callable = None,
|
|
|
|
|
|
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-04-02 00:00:04 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_TOOLS_REGISTRY = {
|
2025-05-20 20:49:50 +08:00
|
|
|
|
"Calculator": calculator,
|
|
|
|
|
|
"QueryKnowledgeGraph": query_knowledge_graph,
|
2025-04-02 00:00:04 +08:00
|
|
|
|
}
|
2025-04-08 00:35:29 +08:00
|
|
|
|
|
|
|
|
|
|
if config.enable_web_search:
|
2025-05-20 20:49:50 +08:00
|
|
|
|
_TOOLS_REGISTRY["WebSearchWithTavily"] = TavilySearchResults(max_results=10)
|