liunux4odoo 42aa900566
优化工具定义;添加 openai 兼容的统一 chat 接口 (#3570)
- 修复:
    - Qwen Agent 的 OutputParser 不再抛出异常,遇到非 COT 文本直接返回
    - CallbackHandler 正确处理工具调用信息

- 重写 tool 定义方式:
    - 添加 regist_tool 简化 tool 定义:
        - 可以指定一个用户友好的名称
        - 自动将函数的 __doc__ 作为 tool.description
	- 支持用 Field 定义参数,不再需要额外定义 ModelSchema
        - 添加 BaseToolOutput 封装 tool	返回结果,以便同时获取原始值、给LLM的字符串值
        - 支持工具热加载(有待测试)

- 增加 openai 兼容的统一 chat 接口,通过 tools/tool_choice/extra_body 不同参数组合支持:
    - Agent 对话
    - 指定工具调用(如知识库RAG)
    - LLM 对话

- 根据后端功能更新 webui
2024-03-29 11:55:32 +08:00

19 lines
533 B
Python

from chatchat.server.pydantic_v1 import Field
from .tools_registry import regist_tool, BaseToolOutput
@regist_tool(title="数学计算器")
def calculate(text: str = Field(description="a math expression")) -> float:
'''
Useful to answer questions about simple calculations.
translate user question to a math expression that can be evaluated by numexpr.
'''
import numexpr
try:
ret = str(numexpr.evaluate(text))
except Exception as e:
ret = f"wrong: {e}"
return BaseToolOutput(ret)