mirror of
https://github.com/RYDE-WORK/Langchain-Chatchat.git
synced 2026-01-19 21:37:20 +08:00
- 重构 api.py:
- 按模块划分为不同的 router
- 添加 openai 兼容的转发接口,项目默认使用该接口以实现模型负载均衡
- 添加 /tools 接口,可以获取/调用编写的 agent tools
- 移除所有 EmbeddingFuncAdapter,统一改用 get_Embeddings
- 待办:
- /chat/chat 接口改为 openai 兼容
- 添加 /chat/kb_chat 接口,openai 兼容
- 改变 ntlk/knowledge_base/logs 等数据目录位置
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
from __future__ import annotations
|
|
|
|
from typing import List
|
|
|
|
from fastapi import APIRouter, Request, Body
|
|
|
|
from configs import logger
|
|
from server.utils import BaseResponse
|
|
|
|
|
|
tool_router = APIRouter(prefix="/tools", tags=["Toolkits"])
|
|
|
|
|
|
@tool_router.get("/", response_model=BaseResponse)
|
|
async def list_tools():
|
|
import importlib
|
|
from server.agent.tools_factory import tools_registry
|
|
importlib.reload(tools_registry)
|
|
|
|
data = {t.name: {"name": t.name, "description": t.description, "args": t.args} for t in tools_registry.all_tools}
|
|
return {"data": data}
|
|
|
|
|
|
@tool_router.post("/call", response_model=BaseResponse)
|
|
async def call_tool(
|
|
name: str = Body(examples=["calculate"]),
|
|
kwargs: dict = Body({}, examples=[{"a":1,"b":2,"operator":"+"}]),
|
|
):
|
|
import importlib
|
|
from server.agent.tools_factory import tools_registry
|
|
importlib.reload(tools_registry)
|
|
|
|
tool_names = {t.name: t for t in tools_registry.all_tools}
|
|
if tool := tool_names.get(name):
|
|
try:
|
|
result = await tool.ainvoke(kwargs)
|
|
return {"data": result}
|
|
except Exception:
|
|
msg = f"failed to call tool '{name}'"
|
|
logger.error(msg, exc_info=True)
|
|
return {"code": 500, "msg": msg}
|
|
else:
|
|
return {"code": 500, "msg": f"no tool named '{name}'"}
|