2025-07-02 02:58:13 +08:00
|
|
|
|
import asyncio
|
2025-08-28 13:16:19 +08:00
|
|
|
|
import traceback
|
2025-05-23 15:30:14 +08:00
|
|
|
|
from typing import Annotated, Any
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-10-23 01:39:01 +08:00
|
|
|
|
from langchain.tools import tool
|
|
|
|
|
|
from langchain_core.tools import StructuredTool
|
2025-07-02 02:58:13 +08:00
|
|
|
|
from langchain_tavily import TavilySearch
|
2025-10-27 15:32:12 +08:00
|
|
|
|
from langgraph.types import interrupt
|
2025-09-01 22:37:03 +08:00
|
|
|
|
from pydantic import BaseModel, Field
|
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-11-01 21:34:16 +08:00
|
|
|
|
|
2025-10-27 15:32:12 +08:00
|
|
|
|
# TODO[修改建议]:前端需要通过interrupt进行交互,点击是或否来批准执行
|
|
|
|
|
|
# 返回中断点:
|
|
|
|
|
|
# is_approved : bool = True 或者 False
|
|
|
|
|
|
# resume_command = Command(resume=is_approved)
|
|
|
|
|
|
# stream = graph.stream(resume_command, config=config, stream_mode="messages")
|
|
|
|
|
|
# graph.invoke(resume_command, config=config)
|
|
|
|
|
|
@tool(name_or_callable="人工审批工具", description="请求人工审批工具,用于在执行重要操作前获得人类确认。")
|
|
|
|
|
|
def get_approved_user_goal(
|
|
|
|
|
|
operation_description: str,
|
2025-11-01 21:34:16 +08:00
|
|
|
|
) -> dict:
|
2025-10-27 15:32:12 +08:00
|
|
|
|
"""
|
|
|
|
|
|
请求人工审批,在执行重要操作前获得人类确认。
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
operation_description: 需要审批的操作描述,例如 "调用知识库工具"
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
dict: 包含审批结果的字典,格式为 {"approved": bool, "message": str}
|
|
|
|
|
|
"""
|
|
|
|
|
|
# 构建详细的中断信息
|
|
|
|
|
|
interrupt_info = {
|
2025-10-31 14:16:07 +08:00
|
|
|
|
"question": "是否批准以下操作?",
|
2025-10-27 15:32:12 +08:00
|
|
|
|
"operation": operation_description,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
# 触发人工审批
|
|
|
|
|
|
is_approved = interrupt(interrupt_info)
|
|
|
|
|
|
|
|
|
|
|
|
# 返回审批结果
|
|
|
|
|
|
if is_approved:
|
|
|
|
|
|
result = {
|
|
|
|
|
|
"approved": True,
|
|
|
|
|
|
"message": f"✅ 操作已批准:{operation_description}",
|
|
|
|
|
|
}
|
|
|
|
|
|
print(f"✅ 人工审批通过: {operation_description}")
|
|
|
|
|
|
else:
|
|
|
|
|
|
result = {
|
|
|
|
|
|
"approved": False,
|
|
|
|
|
|
"message": f"❌ 操作被拒绝:{operation_description}",
|
|
|
|
|
|
}
|
|
|
|
|
|
print(f"❌ 人工审批被拒绝: {operation_description}")
|
|
|
|
|
|
|
|
|
|
|
|
return result
|
2025-03-24 19:07:51 +08:00
|
|
|
|
|
2025-11-01 21:34:16 +08:00
|
|
|
|
|
2025-10-24 00:11:52 +08:00
|
|
|
|
@tool(name_or_callable="查询知识图谱", description="使用这个工具可以查询知识图谱中包含的三元组信息。")
|
2025-08-31 00:34:26 +08:00
|
|
|
|
def query_knowledge_graph(query: Annotated[str, "The keyword to query knowledge graph."]) -> Any:
|
|
|
|
|
|
"""Use this to query knowledge graph, which include some food domain knowledge."""
|
|
|
|
|
|
try:
|
|
|
|
|
|
logger.debug(f"Querying knowledge graph with: {query}")
|
2025-09-01 22:37:03 +08:00
|
|
|
|
result = graph_base.query_node(query, hops=2, return_format="triples")
|
|
|
|
|
|
logger.debug(
|
2025-09-02 01:08:42 +08:00
|
|
|
|
f"Knowledge graph query returned "
|
|
|
|
|
|
f"{len(result.get('triples', [])) if isinstance(result, dict) else 'N/A'} triples"
|
2025-09-01 22:37:03 +08:00
|
|
|
|
)
|
2025-08-31 00:34:26 +08:00
|
|
|
|
return result
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"Knowledge graph query error: {e}, {traceback.format_exc()}")
|
|
|
|
|
|
return f"知识图谱查询失败: {str(e)}"
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-09-01 03:38:46 +08:00
|
|
|
|
def get_static_tools() -> list:
|
2025-08-31 00:34:26 +08:00
|
|
|
|
"""注册静态工具"""
|
2025-11-01 21:34:16 +08:00
|
|
|
|
static_tools = [query_knowledge_graph, get_approved_user_goal]
|
2025-08-31 00:34:26 +08:00
|
|
|
|
|
|
|
|
|
|
# 检查是否启用网页搜索
|
|
|
|
|
|
if config.enable_web_search:
|
2025-10-24 00:11:52 +08:00
|
|
|
|
search = TavilySearch(max_results=10)
|
|
|
|
|
|
search.metadata = {"name": "Tavily 网页搜索"}
|
|
|
|
|
|
static_tools.append(search)
|
2025-08-31 00:34:26 +08:00
|
|
|
|
|
|
|
|
|
|
return static_tools
|
|
|
|
|
|
|
|
|
|
|
|
|
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-09-01 22:37:03 +08:00
|
|
|
|
"查询的关键词,查询的时候,应该尽量以可能帮助回答这个问题的关键词进行查询,不要直接使用用户的原始输入去查询。"
|
2025-05-23 15:30:14 +08:00
|
|
|
|
)
|
|
|
|
|
|
)
|
2025-04-05 17:27:52 +08:00
|
|
|
|
|
2025-08-24 20:12:15 +08:00
|
|
|
|
|
2025-09-01 03:38:46 +08:00
|
|
|
|
def get_kb_based_tools() -> list:
|
2025-08-24 20:12:15 +08:00
|
|
|
|
"""获取所有知识库基于的工具"""
|
|
|
|
|
|
# 获取所有知识库
|
2025-08-29 20:54:49 +08:00
|
|
|
|
kb_tools = []
|
2025-08-24 20:12:15 +08:00
|
|
|
|
retrievers = knowledge_base.get_retrievers()
|
2025-08-31 00:34:26 +08:00
|
|
|
|
|
|
|
|
|
|
def _create_retriever_wrapper(db_id: str, retriever_info: dict[str, Any]):
|
|
|
|
|
|
"""创建检索器包装函数的工厂函数,避免闭包变量捕获问题"""
|
2025-09-01 22:37:03 +08:00
|
|
|
|
|
2025-08-31 00:34:26 +08:00
|
|
|
|
async def async_retriever_wrapper(query_text: str) -> Any:
|
|
|
|
|
|
"""异步检索器包装函数"""
|
|
|
|
|
|
retriever = retriever_info["retriever"]
|
|
|
|
|
|
try:
|
|
|
|
|
|
logger.debug(f"Retrieving from database {db_id} with query: {query_text}")
|
|
|
|
|
|
if asyncio.iscoroutinefunction(retriever):
|
|
|
|
|
|
result = await retriever(query_text)
|
|
|
|
|
|
else:
|
|
|
|
|
|
result = retriever(query_text)
|
|
|
|
|
|
logger.debug(f"Retrieved {len(result) if isinstance(result, list) else 'N/A'} results from {db_id}")
|
|
|
|
|
|
return result
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"Error in retriever {db_id}: {e}")
|
|
|
|
|
|
return f"检索失败: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
return async_retriever_wrapper
|
|
|
|
|
|
|
2025-08-24 20:12:15 +08:00
|
|
|
|
for db_id, retrieve_info in retrievers.items():
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 构建工具描述
|
2025-09-02 01:08:42 +08:00
|
|
|
|
description = (
|
|
|
|
|
|
f"使用 {retrieve_info['name']} 知识库进行检索。\n"
|
|
|
|
|
|
f"下面是这个知识库的描述:\n{retrieve_info['description'] or '没有描述。'} "
|
|
|
|
|
|
)
|
2025-08-24 20:12:15 +08:00
|
|
|
|
|
|
|
|
|
|
# 使用工厂函数创建检索器包装函数,避免闭包问题
|
|
|
|
|
|
retriever_wrapper = _create_retriever_wrapper(db_id, retrieve_info)
|
|
|
|
|
|
|
2025-10-24 00:11:52 +08:00
|
|
|
|
safename = retrieve_info["name"].replace(" ", "_")[:20]
|
|
|
|
|
|
|
2025-08-24 20:12:15 +08:00
|
|
|
|
# 使用 StructuredTool.from_function 创建异步工具
|
|
|
|
|
|
tool = StructuredTool.from_function(
|
|
|
|
|
|
coroutine=retriever_wrapper,
|
2025-10-24 00:11:52 +08:00
|
|
|
|
name=safename,
|
2025-08-24 20:12:15 +08:00
|
|
|
|
description=description,
|
|
|
|
|
|
args_schema=KnowledgeRetrieverModel,
|
2025-09-01 22:37:03 +08:00
|
|
|
|
metadata=retrieve_info["metadata"] | {"tag": ["knowledgebase"]},
|
2025-08-24 20:12:15 +08:00
|
|
|
|
)
|
|
|
|
|
|
|
2025-08-29 20:54:49 +08:00
|
|
|
|
kb_tools.append(tool)
|
2025-08-28 13:16:19 +08:00
|
|
|
|
# logger.debug(f"Successfully created tool {tool_id} for database {db_id}")
|
2025-08-24 20:12:15 +08:00
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-08-28 13:16:19 +08:00
|
|
|
|
logger.error(f"Failed to create tool for database {db_id}: {e}, \n{traceback.format_exc()}")
|
2025-08-24 20:12:15 +08:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
return kb_tools
|
|
|
|
|
|
|
2025-08-31 00:34:26 +08:00
|
|
|
|
|
2025-09-01 03:38:46 +08:00
|
|
|
|
def get_buildin_tools() -> list:
|
2025-08-31 00:34:26 +08:00
|
|
|
|
"""获取所有可运行的工具(给大模型使用)"""
|
|
|
|
|
|
tools = []
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 获取所有知识库基于的工具
|
|
|
|
|
|
tools.extend(get_kb_based_tools())
|
|
|
|
|
|
tools.extend(get_static_tools())
|
|
|
|
|
|
|
2025-09-16 15:23:18 +08:00
|
|
|
|
from src.agents.common.toolkits.mysql.tools import get_mysql_tools
|
|
|
|
|
|
|
|
|
|
|
|
tools.extend(get_mysql_tools())
|
|
|
|
|
|
|
2025-08-31 00:34:26 +08:00
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"Failed to get knowledge base retrievers: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
return tools
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-09-01 03:38:46 +08:00
|
|
|
|
def gen_tool_info(tools) -> list[dict[str, Any]]:
|
2025-07-24 00:46:15 +08:00
|
|
|
|
"""获取所有工具的信息(用于前端展示)"""
|
2025-08-29 20:54:49 +08:00
|
|
|
|
tools_info = []
|
2025-07-24 00:46:15 +08:00
|
|
|
|
|
2025-08-24 20:12:15 +08:00
|
|
|
|
try:
|
|
|
|
|
|
# 获取注册的工具信息
|
2025-08-29 20:54:49 +08:00
|
|
|
|
for tool_obj in tools:
|
2025-08-24 20:12:15 +08:00
|
|
|
|
try:
|
2025-09-01 22:37:03 +08:00
|
|
|
|
metadata = getattr(tool_obj, "metadata", {}) or {}
|
2025-08-24 20:12:15 +08:00
|
|
|
|
info = {
|
2025-08-29 20:54:49 +08:00
|
|
|
|
"id": tool_obj.name,
|
2025-09-01 22:37:03 +08:00
|
|
|
|
"name": metadata.get("name", tool_obj.name),
|
2025-08-29 20:54:49 +08:00
|
|
|
|
"description": tool_obj.description,
|
2025-09-01 22:37:03 +08:00
|
|
|
|
"metadata": metadata,
|
2025-09-01 03:38:46 +08:00
|
|
|
|
"args": [],
|
|
|
|
|
|
# "is_async": is_async # Include async information
|
2025-08-24 20:12:15 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
if hasattr(tool_obj, "args_schema") and tool_obj.args_schema:
|
2025-10-25 22:49:20 +08:00
|
|
|
|
if isinstance(tool_obj.args_schema, dict):
|
|
|
|
|
|
schema = tool_obj.args_schema
|
|
|
|
|
|
else:
|
|
|
|
|
|
schema = tool_obj.args_schema.schema()
|
|
|
|
|
|
|
2025-09-01 22:37:03 +08:00
|
|
|
|
for arg_name, arg_info in schema.get("properties", {}).items():
|
|
|
|
|
|
info["args"].append(
|
|
|
|
|
|
{
|
|
|
|
|
|
"name": arg_name,
|
|
|
|
|
|
"type": arg_info.get("type", ""),
|
|
|
|
|
|
"description": arg_info.get("description", ""),
|
|
|
|
|
|
}
|
|
|
|
|
|
)
|
2025-08-29 20:54:49 +08:00
|
|
|
|
|
|
|
|
|
|
tools_info.append(info)
|
2025-08-31 00:34:26 +08:00
|
|
|
|
# logger.debug(f"Successfully processed tool info for {tool_obj.name}")
|
2025-07-24 00:46:15 +08:00
|
|
|
|
|
2025-08-24 20:12:15 +08:00
|
|
|
|
except Exception as e:
|
2025-09-01 22:37:03 +08:00
|
|
|
|
logger.error(
|
2025-10-25 22:49:20 +08:00
|
|
|
|
f"Failed to process tool {getattr(tool_obj, 'name', 'unknown')}: {e}\n{traceback.format_exc()}. "
|
|
|
|
|
|
f"Details: {dict(tool_obj.__dict__)}"
|
2025-09-01 22:37:03 +08:00
|
|
|
|
)
|
2025-08-24 20:12:15 +08:00
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
2025-09-01 03:38:46 +08:00
|
|
|
|
logger.error(f"Failed to get tools info: {e}\n{traceback.format_exc()}")
|
|
|
|
|
|
return []
|
2025-08-24 20:12:15 +08:00
|
|
|
|
|
|
|
|
|
|
logger.info(f"Successfully extracted info for {len(tools_info)} tools")
|
|
|
|
|
|
return tools_info
|