更新时间:2024-08-29 GMT+08:00
分享

实例化Tool

Tool分为StaticTool(静态工具)和DynamicTool(动态工具)两类,静态工具需要开发者事先定义好,即在编译期定义与实例化;动态工具开发者可以在系统运行时动态构建,即在运行态定义与实例化。

StaticTool(静态工具)

静态工具可以通过继承Tool的方式新增,在_run接口中实现工具的功能,例如:

from typing import Type
from pangukitsappdev.tool.tool import Tool
from pydantic import BaseModel, Field


class AddTool(Tool):
    class AddParam(BaseModel):
        a: int = Field(description="加法运算的数字")
        b: int = Field(description="加法运算的数字")

    name = "add"
    description = "加法运算"
    args_schema: Type[BaseModel] = AddParam
    principle = "请在需要做两个整型的加法运算时调用此工具"
    input_desc = "加法输入"
    output_desc = "加法运算的结果"

    def _run(self, a: int, b: int) -> int:
        return a + b

@Tool说明:

  • name。工具的标识,建议为英文且与实际工具含义匹配,在同一个Agent中唯一。
  • description。工具的描述,建议为中文,尽可能的简短描述工具。
  • principle。何时使用该工具,为重要参数,该描述直接影响LLM对工具使用的判断,尽量描述清楚。如果Agent实际执行效果不符合预期,可以调整。
  • input_desc。工具的入参描述 ,为重要参数,该描述直接影响LLM对入参的提取,尽量描述清楚。如果Agent实际执行效果不符合预期,可以调整。
  • output_desc。工具的出参描述,当前对Agent的表现无重要影响。
  • args_schema。工具入参类型,为重要参数,入参继承BaseModel的类型需额外指定,简单类型无需指定。
  • return_type。指定工具返回类型,为可选参数,如_run方法未指定返回类型时必选。
  • 如果输入输出参数为复杂类型,则需要通过继承BaseModel定义复杂类型的参数描述,此时input_desc、output_desc可以填空字符串,但仍然建议给出简要的描述。当前版本不支持复杂类型中再嵌套复杂类型,只支持基本类型:str、int、float、bool,建议参数数量不超过5个。

@Field说明:

  • description。参数的描述,为重要参数,该描述直接影响LLM对入参的提取,尽量描述清楚,如果Agent实际执行效果不符合预期,可以调整。

上例中的args_schema为一个复杂的入参,如果工具的入参为一个基本类型,则不需要再额外定一个结构体,例如:

from typing import Type
from pangukitsappdev.tool.tool import Tool
from pydantic import BaseModel, Field


class ReverseTool(Tool):
    name = "reverse"
    description = "字符串翻转"
    principle = "请在需要字符串翻转时调用此工具"
    input_desc = "输入的字符串"
    output_desc = "反转的结果"

    def _run(self, s: str) -> str:
        return s[::-1]

DynamicTool(动态工具)

动态工具可以在业务运行态动态新增或修改:

from pangukitsappdev.tool.tool import Tool
from pydantic import BaseModel, Field, create_model

def add(a: int, b: int) -> int:
    return a + b

# 定义函数方式
add_tool = Tool.from_function(func=add,
                              name="add",
                              description="加法运算",
                              principle="请在需要做两个整型的加法运算时调用此工具",
                              input_desc="加法输入",
                              output_desc="加法运算的结果",
                              args_schema=create_model('AddTool', a=(int, Field(description="加法运算的数字")), b=(int, Field(description="加法运算的数字")))
)
# lambda匿名方式
lambda_add_tool = Tool.from_function(func=lambda a, b: a+b,
                              name="add",
                              description="加法运算",
                              principle="请在需要做两个整型的加法运算时调用此工具",
                              input_desc="加法输入",
                              output_desc="加法运算的结果",
                              args_schema=create_model('AddTool', a=(int, Field(description="加法运算的数字")), b=(int, Field(description="加法运算的数字"))),
                              return_type=int)
  • name、description、principle、input_desc、output_desc和args_schema的定义与说明与静态工具相同。
  • return_type。为可选参数,如果func为未指定返回值类型的callable类型,必须通过return_type指定返回值类型。

相关文档