工具
MCPToolApprovalFunction module-attribute
MCPToolApprovalFunction = Callable[
[MCPToolApprovalRequest],
MaybeAwaitable[MCPToolApprovalFunctionResult],
]
一个用于批准或拒绝工具调用的函数。
LocalShellExecutor module-attribute
LocalShellExecutor = Callable[
[LocalShellCommandRequest], MaybeAwaitable[str]
]
一个在 shell 上执行命令的函数。
ShellExecutor module-attribute
ShellExecutor = Callable[
[ShellCommandRequest],
MaybeAwaitable[Union[str, ShellResult]],
]
执行 shell 命令序列,并返回文本或结构化输出。
Tool module-attribute
Tool = Union[
FunctionTool,
FileSearchTool,
WebSearchTool,
ComputerTool[Any],
HostedMCPTool,
ShellTool,
ApplyPatchTool,
LocalShellTool,
ImageGenerationTool,
CodeInterpreterTool,
]
可以在代理中使用的工具。
ToolOutputText
ToolOutputTextDict
ToolOutputImage
基础: BaseModel
表示应作为图像发送到模型的工具输出。
您可以提供一个 image_url(URL 或数据 URL)或一个用于先前上传内容的 file_id。可选的 detail 可以控制视觉细节。
源代码在 src/agents/tool.py
check_at_least_one_required_field
check_at_least_one_required_field() -> ToolOutputImage
验证是否提供了 image_url 或 file_id 中的至少一个。
源代码在 src/agents/tool.py
ToolOutputImageDict
基础: TypedDict
用于图像工具输出的 TypedDict 变体。
源代码在 src/agents/tool.py
ToolOutputFileContent
基础: BaseModel
表示应作为文件发送到模型的工具输出。
提供 file_data(base64)、file_url 或 file_id 中的一个。在使用 file_data 时,您还可以提供一个可选的 filename 来提示文件名。
源代码在 src/agents/tool.py
check_at_least_one_required_field
check_at_least_one_required_field() -> (
ToolOutputFileContent
)
验证是否提供了 file_data、file_url 或 file_id 中的至少一个。
源代码在 src/agents/tool.py
ToolOutputFileContentDict
基础: TypedDict
用于文件内容工具输出的 TypedDict 变体。
源代码在 src/agents/tool.py
ComputerCreate
ComputerDispose
基类:Protocol[ComputerT_contra]
清理为运行上下文初始化的计算机。
源代码在 src/agents/tool.py
ComputerProvider dataclass
基类:Generic[ComputerT]
配置用于每次运行计算机生命周期管理的创建/销毁钩子。
源代码在 src/agents/tool.py
FunctionToolResult dataclass
源代码在 src/agents/tool.py
FunctionTool dataclass
包装函数的工具。在大多数情况下,您应该使用 function_tool 助手来创建 FunctionTool,因为它们让您可以轻松地包装 Python 函数。
源代码在 src/agents/tool.py
on_invoke_tool instance-attribute
on_invoke_tool: Callable[
[ToolContext[Any], str], Awaitable[Any]
]
一个函数,它使用给定的上下文和参数调用该工具。传递的参数是:1. 工具运行上下文。2. 来自 LLM 的参数,作为 JSON 字符串。
您必须返回结构化工具输出类型之一(例如 ToolOutputText、ToolOutputImage、ToolOutputFileContent)或工具输出的字符串表示形式,或者它们的列表,或者我们可以调用 str() 的内容。如果发生错误,您可以选择引发异常(这将导致运行失败)或返回字符串错误消息(这将发送回 LLM)。
strict_json_schema 类属性 实例属性
JSON 模式是否处于严格模式。我们强烈建议将其设置为 True,因为它会增加生成正确 JSON 输入的可能性。
is_enabled class-attribute instance-attribute
is_enabled: (
bool
| Callable[
[RunContextWrapper[Any], AgentBase],
MaybeAwaitable[bool],
]
) = True
工具是否启用。一个布尔值或一个接受运行上下文和代理并返回工具是否启用的 Callable。您可以使用此功能根据您的上下文/状态动态启用/禁用工具。
tool_input_guardrails class-attribute instance-attribute
tool_input_guardrails: (
list[ToolInputGuardrail[Any]] | None
) = None
可选的输入防护栏列表,在调用此工具之前运行。
tool_output_guardrails class-attribute instance-attribute
tool_output_guardrails: (
list[ToolOutputGuardrail[Any]] | None
) = None
可选的输出防护栏列表,在调用此工具之后运行。
FileSearchTool dataclass
一个托管工具,允许 LLM 搜索向量存储。目前仅支持使用 OpenAI 模型和 Responses API。
源代码在 src/agents/tool.py
include_search_results class-attribute instance-attribute
是否将搜索结果包含在 LLM 生成的输出中。
ranking_options class-attribute instance-attribute
搜索排名选项。
WebSearchTool dataclass
一个托管工具,允许 LLM 搜索网络。目前仅支持使用 OpenAI 模型和 Responses API。
源代码在 src/agents/tool.py
user_location class-attribute instance-attribute
搜索的可选位置。允许您自定义结果,使其与位置相关。
ComputerTool dataclass
基类:Generic[ComputerT]
一个托管工具,允许 LLM 控制计算机。
源代码在 src/agents/tool.py
on_safety_check class-attribute instance-attribute
on_safety_check: (
Callable[
[ComputerToolSafetyCheckData], MaybeAwaitable[bool]
]
| None
) = None
可选的回调函数,用于确认计算机工具的安全检查。
ComputerToolSafetyCheckData dataclass
计算机工具安全检查的信息。
源代码在 src/agents/tool.py
MCPToolApprovalRequest dataclass
批准工具调用的请求。
源代码在 src/agents/tool.py
MCPToolApprovalFunctionResult
基础: TypedDict
MCP 工具批准函数的结果。
源代码在 src/agents/tool.py
HostedMCPTool dataclass
一个允许 LLM 使用远程 MCP 服务器的工具。LLM 将自动列出和调用工具,无需往返您的代码。如果您想通过 stdio 在本地运行 MCP 服务器,在 VPC 或其他非公开可访问的环境中,或者您只是更喜欢在本地运行工具调用,那么您可以改为使用 agents.mcp 中的服务器并将 Agent(mcp_servers=[...]) 传递给代理。
源代码在 src/agents/tool.py
on_approval_request class-attribute instance-attribute
on_approval_request: MCPToolApprovalFunction | None = None
一个可选的函数,如果请求批准 MCP 工具,将调用该函数。如果未提供,则需要手动将批准/拒绝添加到输入并调用 Runner.run(...) 再次。
CodeInterpreterTool dataclass
一个允许 LLM 在沙盒环境中执行代码的工具。
源代码在 src/agents/tool.py
ImageGenerationTool dataclass
一个允许 LLM 生成图像的工具。
源代码在 src/agents/tool.py
LocalShellCommandRequest dataclass
一个在 shell 上执行命令的请求。
源代码在 src/agents/tool.py
LocalShellTool dataclass
一个允许 LLM 在 shell 上执行命令的工具。
有关详细信息,请参阅:https://platform.openai.com/docs/guides/tools-local-shell
源代码在 src/agents/tool.py
ShellCallOutcome dataclass
ShellCommandOutput dataclass
单个 shell 命令执行的结构化输出。
源代码在 src/agents/tool.py
ShellResult dataclass
ShellActionRequest dataclass
ShellCallData dataclass
ShellCommandRequest dataclass
ShellTool dataclass
下一代 shell 工具。LocalShellTool 将被弃用,转而使用此工具。
源代码在 src/agents/tool.py
ApplyPatchTool dataclass
托管 apply_patch 工具。允许模型通过统一的 diff 请求文件修改。
源代码在 src/agents/tool.py
resolve_computer async
resolve_computer(
*,
tool: ComputerTool[Any],
run_context: RunContextWrapper[Any],
) -> ComputerLike
解析给定运行上下文的计算机,如果需要则初始化它。
源代码在 src/agents/tool.py
dispose_resolved_computers async
dispose_resolved_computers(
*, run_context: RunContextWrapper[Any]
) -> None
销毁为提供的运行上下文创建的任何计算机实例。
源代码在 src/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,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], AgentBase],
MaybeAwaitable[bool],
] = True,
tool_input_guardrails: list[ToolInputGuardrail[Any]]
| None = None,
tool_output_guardrails: list[ToolOutputGuardrail[Any]]
| None = None,
) -> 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,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], AgentBase],
MaybeAwaitable[bool],
] = True,
tool_input_guardrails: list[ToolInputGuardrail[Any]]
| None = None,
tool_output_guardrails: list[ToolOutputGuardrail[Any]]
| None = None,
) -> 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,
is_enabled: bool
| Callable[
[RunContextWrapper[Any], AgentBase],
MaybeAwaitable[bool],
] = True,
tool_input_guardrails: list[ToolInputGuardrail[Any]]
| None = None,
tool_output_guardrails: list[ToolOutputGuardrail[Any]]
| None = None,
) -> (
FunctionTool
| Callable[[ToolFunction[...]], FunctionTool]
)
装饰器,用于从函数创建 FunctionTool。默认情况下,我们将:1. 解析函数签名以创建工具参数的 JSON schema。 2. 使用函数的文档字符串填充工具的描述。 3. 使用函数的文档字符串填充参数描述。 文档字符串的风格会自动检测,但您可以覆盖它。
如果函数将 RunContextWrapper 作为第一个参数,它必须与使用该工具的代理的上下文类型匹配。
参数
| 名称 | 类型 | 描述 | 默认 |
|---|---|---|---|
func
|
ToolFunction[...] | None
|
要包装的函数。 |
None
|
name_override
|
str | None
|
如果提供,则使用此名称作为工具的名称,而不是函数的名称。 |
None
|
description_override
|
str | None
|
如果提供,则使用此描述作为工具的描述,而不是函数的文档字符串。 |
None
|
docstring_style
|
DocstringStyle | None
|
如果提供,则使用此风格作为工具的文档字符串。 如果未提供,我们将尝试自动检测风格。 |
None
|
use_docstring_info
|
bool
|
如果为 True,则使用函数的文档字符串填充工具的描述和参数描述。 |
True
|
failure_error_function
|
ToolErrorFunction | None
|
如果提供,则使用此函数生成工具调用失败时的错误消息。 错误消息将发送给 LLM。 如果传递 None,则不会发送错误消息,而是会引发异常。 |
default_tool_error_function
|
strict_mode
|
bool
|
是否为工具的 JSON schema 启用严格模式。 我们强烈建议将其设置为 True,因为它增加了正确 JSON 输入的可能性。 如果为 False,则允许非严格的 JSON schema。 例如,如果参数具有默认值,它将是可选的,允许其他属性等。 更多信息请参见:https://platform.openai.com/docs/guides/structured-outputs?api-mode=responses#supported-schemas |
True
|
is_enabled
|
bool | Callable[[RunContextWrapper[Any], AgentBase], MaybeAwaitable[bool]]
|
工具是否启用。 可以是 bool 或一个可调用对象,它接收运行上下文和代理,并返回工具是否启用。 禁用的工具在运行时对 LLM 隐藏。 |
True
|
tool_input_guardrails
|
list[ToolInputGuardrail[Any]] | None
|
在调用工具之前运行的 guardrail 的可选列表。 |
None
|
tool_output_guardrails
|
list[ToolOutputGuardrail[Any]] | None
|
在工具返回后运行的 guardrail 的可选列表。 |
None
|
源代码在 src/agents/tool.py
714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 | |