Tools
MCPToolApprovalFunction
module-attribute
MCPToolApprovalFunction = Callable[[MCPToolApprovalRequest], MaybeAwaitable[MCPToolApprovalFunctionResult]]
用于批准或拒绝工具调用的函数。
LocalShellExecutor
module-attribute
LocalShellExecutor = Callable[[LocalShellCommandRequest], MaybeAwaitable[str]]
用于在 shell 上执行命令的函数。
Tool
module-attribute
Tool = Union[FunctionTool, FileSearchTool, WebSearchTool, ComputerTool, HostedMCPTool, LocalShellTool, ImageGenerationTool, CodeInterpreterTool]
可在 agent 中使用的工具。
FunctionToolResult
dataclass
Source code in agents/tool.py
FunctionTool
dataclass
包装函数的工具。在大多数情况下,你应该使用 function_tool
辅助函数来创建 FunctionTool,
因为它们可以让你轻松地包装一个 Python 函数。
Source code in agents/tool.py
on_invoke_tool
instance-attribute
on_invoke_tool: Callable[[RunContextWrapper[Any], str], Awaitable[Any]]
用于根据给定上下文和参数调用工具的函数。传递的参数为: 1. 工具运行上下文。 2. 来自 LLM 的参数,作为 JSON 字符串。
你必须返回工具输出的字符串表示,或者可以调用 str()
的对象。
如果发生错误,你可以抛出异常(这会导致运行失败),或者返回字符串错误信息(会反馈给 LLM)。
FileSearchTool
dataclass
托管工具,允许 LLM 搜索向量存储。目前仅支持 OpenAI 模型,使用 Responses API。
Source code in agents/tool.py
include_search_results
class-attribute
instance-attribute
是否在 LLM 产生的输出中包含搜索结果。
ranking_options
class-attribute
instance-attribute
搜索的排序选项。
WebSearchTool
dataclass
托管工具,允许 LLM 搜索互联网。目前仅支持 OpenAI 模型,使用 Responses API。
Source code in agents/tool.py
ComputerTool
dataclass
托管工具,允许 LLM 控制计算机。
Source code in agents/tool.py
MCPToolApprovalRequest
dataclass
工具调用审批请求。
Source code in agents/tool.py
MCPToolApprovalFunctionResult
Bases: TypedDict
MCP 工具审批函数的结果。
Source code in agents/tool.py
HostedMCPTool
dataclass
允许 LLM 使用远程 MCP 服务器的工具。LLM 会自动列出并调用工具,无需你的代码往返。
如果你想通过 stdio 在本地、VPC 或其他非公网环境运行 MCP 服务器,或者你更喜欢本地运行工具调用,
可以使用 agents.mcp
中的服务器,并将 Agent(mcp_servers=[...])
传递给 agent。
Source code in agents/tool.py
on_approval_request
class-attribute
instance-attribute
on_approval_request: MCPToolApprovalFunction | None = None
可选函数,当 MCP 工具请求审批时会被调用。如果未提供,你需要手动在输入中添加批准/拒绝并再次调用
Runner.run(...)
。
CodeInterpreterTool
dataclass
允许 LLM 在沙箱环境中执行代码的工具。
Source code in agents/tool.py
ImageGenerationTool
dataclass
允许 LLM 生成图片的工具。
Source code in agents/tool.py
LocalShellCommandRequest
dataclass
在 shell 上执行命令的请求。
Source code in agents/tool.py
LocalShellTool
dataclass
允许 LLM 在 shell 上执行命令的工具。
Source code in agents/tool.py
default_tool_error_function
default_tool_error_function(ctx: RunContextWrapper[Any], error: Exception) -> str
function_tool
function_tool(func: ToolFunction[...], *, name_override: str | None = None, description_override: str | None = None, docstring_style: DocstringStyle | None = None, use_docstring_info: bool = True, failure_error_function: ToolErrorFunction | None = None, strict_mode: bool = True) -> FunctionTool
function_tool(*, name_override: str | None = None, description_override: str | None = None, docstring_style: DocstringStyle | None = None, use_docstring_info: bool = True, failure_error_function: ToolErrorFunction | None = None, strict_mode: bool = True) -> Callable[[ToolFunction[...]], FunctionTool]
function_tool(func: ToolFunction[...] | None = None, *, name_override: str | None = None, description_override: str | None = None, docstring_style: DocstringStyle | None = None, use_docstring_info: bool = True, failure_error_function: ToolErrorFunction | None = default_tool_error_function, strict_mode: bool = True) -> FunctionTool | Callable[[ToolFunction[...]], FunctionTool]
装饰器:将一个函数包装为 FunctionTool。默认情况下,我们会: 1. 解析函数签名,为工具参数生成 JSON schema。 2. 使用函数的 docstring 作为工具描述。 3. 使用函数的 docstring 作为参数描述。 docstring 风格会自动检测,但你可以手动指定。
如果函数的第一个参数是 RunContextWrapper
,它 必须 与使用该工具的 agent 的上下文类型一致。
参数说明
func: 要包装的函数。 name_override: 如果提供,使用该名称作为工具名,替代函数名。 description_override: 如果提供,使用该描述作为工具描述,替代函数 docstring。 docstring_style: 如果提供,使用该风格解析 docstring。如果未提供,将自动检测风格。 use_docstring_info: 如果为 True,使用函数 docstring 填充工具描述和参数描述。 failure_error_function: 如果提供,当工具调用失败时使用该函数生成错误信息。错误信息会反馈给 LLM。如果传 None,则不会发送错误信息,而是抛出异常。 strict_mode: 是否启用严格模式生成工具的 JSON schema。强烈建议设置为 True,这样更容易获得正确的 JSON 输入。如果为 False,则允许非严格 JSON schema。例如,带默认值的参数会变为可选,允许额外属性等。详见:https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses#supported-schemas
Source code in agents/tool.py
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
|