Deep Read

02 带有Tool工具的Agent


TOOL的定义和结构

Tools的整体结构

TOOL_SPECS = [
    {...},
    {...},
    {...},
    {...},
]

这是一个列表,里面每个 {...} 代表一个工具,可以理解为这是一个工具清单,里面每一项都是一个工具的元数据。

TOOLS 可以理解为 给模型看,让模型知道有哪些工具,知道每个工具需要什么参数,TOOLS 不负责真正执行。真正执行靠这个:TOOL_HANDLERS

单个工具的定义

{
    "name": "read_file",
    "description": "Read the contents of a file.",
    "input_schema": {
        "type": "object",
        "properties": {
            "file_path": {
                "type": "string",
                "description": "Path to the file (relative to working directory).",
            },
        },
        "required": ["file_path"],
    },
}
  • name:工具名字, 意思是告诉模型:这个工具叫 read_file
  • description:工具说明, 这是给模型看的。模型会根据 description 判断:什么时候应该用这个工具
  • input_schema:参数规则, 它告诉模型:调用这个工具时,需要传什么参数, 参数是什么类型, 哪些参数必须
"input_schema": {
    "type": "object",
    "properties": {...},
    "required": [...]
}
  • properties:参数列表, read_file 工具有一个参数:file_path, 类型是字符串. 调用时类似: read_file(file_path="main.py")
"properties": {
    "file_path": {
        "type": "string",
        "description": "Path to the file..."
    }
}
  • required:必填参数, "required": ["file_path"] , 调用 read_file 时,必须提供 file_path

TOOLS 工具的调用

# 调度表: 工具名 -> 处理函数  
TOOL_HANDLERS: dict[str, Any] = {  
    "bash": tool_bash,  
    "read_file": tool_read_file,  
    "write_file": tool_write_file,  
    "edit_file": tool_edit_file,  
}