liunux4odoo 5c650a8dc3
优化目录结构 (#4058)
* 优化目录结构

* 修改一些测试问题

---------

Co-authored-by: glide-the <2533736852@qq.com>
2024-05-22 13:11:45 +08:00

44 lines
1.2 KiB
Python

from __future__ import annotations
from typing import List
from fastapi import APIRouter, Request, Body
from chatchat.server.utils import BaseResponse, get_tool, get_tool_config
import logging
logger = logging.getLogger()
tool_router = APIRouter(prefix="/tools", tags=["Toolkits"])
@tool_router.get("", response_model=BaseResponse)
async def list_tools():
tools = get_tool()
data = {t.name: {
"name": t.name,
"title": t.title,
"description": t.description,
"args": t.args,
"config": get_tool_config(t.name),
} for t in tools.values()}
return {"data": data}
@tool_router.post("/call", response_model=BaseResponse)
async def call_tool(
name: str = Body(examples=["calculate"]),
tool_input: dict = Body({}, examples=[{"text": "3+5/2"}]),
):
if tool := get_tool(name):
try:
result = await tool.ainvoke(tool_input)
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}'"}