API 参考

工具调用相关的 API 参考文档。

tool() 参数

namerequiredstring
工具的唯一标识符。模型通过名称来选择调用哪个工具。
descriptionrequiredstring
工具的功能描述。帮助模型理解工具的用途和何时应该调用它。
schemarequiredz.ZodObject
参数的 Zod Schema。既用于向模型描述参数结构,也用于验证模型生成的参数。
executerequired(args: z.infer<T>) => ToolResult | Promise<ToolResult>
工具执行函数。接收经过 Schema 验证的参数,返回任意类型的执行结果。
strictboolean
false
启用此工具的严格模式。同一请求中的所有工具必须具有相同的 strict 值——不允许混用。也可以使用 createModel({ strict: true }) 全局启用严格模式。SDK 会自动切换到 Beta 端点并向 API 传递 strict: true。服务端会校验 JSON Schema,确保模型严格遵循定义的参数结构。
requiredboolean
false
标记为必需工具。启用后模型将被强制调用此工具。
timeoutnumber
60000
执行超时时间(毫秒)。超过此时间未完成的工具调用将被终止。
retriesnumber
0
失败时的最大重试次数。每次重试都会受到超时限制。
compactboolean | ToolCompactConfig
undefined
启用结果压缩。设为 true 时,超过阈值的工具结果会通过 LLM 压缩以节省上下文窗口空间。传入对象 { threshold?: number, model?: Model } 可自定义设置。

Tool 类型

ToolReturnType<typeof tool>
tool() 函数的返回类型。包含原始配置属性和经过处理的 parameters(JSON Schema)与 execute(包装后的执行函数)。
StrictToolStrictTool
设置了 strict: true 的工具。此工具定义的所有属性必须设置 strict: true
NonStrictToolNonStrictTool
strict: false 或未定义的工具。未启用严格模式时的默认工具类型。
ConsistentToolsStrictTool[] | NonStrictTool[]
generateText 和智能体方法使用的联合类型。确保数组中的所有工具具有一致的严格模式设置——要么全部严格,要么全部非严格。

工具结果类型

ToolSuccess{ success: true, data: T }
工具执行成功时的结果格式。data 字段包含 execute 函数的返回值。
ToolFailure{ success: false, error: string }
工具执行失败时的结果格式。error 字段包含错误描述信息。

ToolChoice 类型

autostring
模型自主决定是否调用工具(默认行为)。
nonestring
模型不会调用任何工具。
requiredstring
模型必须调用至少一个工具。
{ type: 'function', function: { name: string } }object
模型必须调用指定的工具。当只有一个工具设置 required: true 时自动使用此模式。