英文文档原文详见 OpenAI Agents SDKhttps://openai.github.io/openai-agents-python/
本文是OpenAI-agents-sdk-python使用翻译软件翻译后的中文文档/教程。分多个帖子发布,帖子的目录如下:
(2)OpenAI agents sdk, agents,运行agents,结果,流,工具,交接
(3) OpenAi agents sdk, 跟踪,上下文管理,护栏
(4) Openai agents sdk, 编排多个代理,模型,配置SDK
(5)(6)..等等,后面的都放到openai agents sdk的这个专栏https://blog.csdn.net/wtsolutions/category_12916751.html里面了,大家可以到专栏里面看到所有的目录,欢迎订阅这个专栏。
Tools
工具 module-attribute
Tool = Union[
FunctionTool,
FileSearchTool,
WebSearchTool,
ComputerTool,
]
可在代理中使用的工具。
FunctionTool 数据类
包装函数的工具。在大多数情况下,您应该使用帮助程序来 创建一个 FunctionTool,因为它们让你可以轻松地包装一个 Python 函数。function_tool
src/agents/tool.py
name 实例属性
name: str
工具的名称,如 LLM 所示。通常是函数的名称。
描述 instance-attribute
description: str
工具的描述,如 LLM 所示。
params_json_schema instance-attribute
params_json_schema: dict[str, Any]
工具参数的 JSON 架构。
on_invoke_tool instance-attribute
on_invoke_tool: Callable[
[RunContextWrapper[Any], str], Awaitable[str]
]
使用给定上下文和参数调用工具的函数。传递的参数 是: 1. 工具运行上下文。 2. 来自 LLM 的参数,以 JSON 字符串形式。
您必须返回工具输出的字符串表示。如果出现错误,您可以执行以下任一作 引发 Exception (这将导致运行失败) 或返回字符串错误消息 (该 将被发送回 LLM)。
strict_json_schema 类属性
instance-attribute
strict_json_schema: bool = True
JSON 架构是否处于严格模式。我们强烈建议将此设置为 True, 因为它增加了正确 JSON 输入的可能性。
FileSearchTool 数据类
一个托管工具,允许 LLM 在向量存储中进行搜索。目前仅支持 OpenAI 模型,使用响应 API。
源码src/agents/tool.py
vector_store_ids instance-attribute
vector_store_ids: list[str]
要搜索的向量存储的 ID。
max_num_results 类属性
instance-attribute
max_num_results: int | None = None
要返回的最大结果数。
include_search_results 类属性
实例属性
include_search_results: bool = False
是否将搜索结果包含在 LLM 生成的输出中。
ranking_options 类属性
instance-attribute
ranking_options: RankingOptions | None = None
搜索的排名选项。
filters 类-属性
instance-attribute
filters: Filters | None = None
要根据文件属性应用的过滤器。
WebSearchTool 数据类
ComputerTool 数据类
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,
) -> 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,
) -> 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,
) -> (
FunctionTool
| Callable[[ToolFunction[...]], FunctionTool]
)
Decorator 从函数创建 FunctionTool。默认情况下,我们将: 1. 解析函数签名,为工具的参数创建 JSON 架构。 2. 使用函数的文档字符串填充工具的描述。 3. 使用函数的文档字符串填充参数描述。 系统会自动检测文档字符串样式,但您可以覆盖它。
如果函数将 a 作为第一个参数,则它必须与 使用该工具的代理程序的 context 类型。RunContextWrapper
参数:
名字 | 类型 | 描述 | 违约 |
---|---|---|---|
func | ToolFunction[...] | None |
要包装的函数。 | None |
name_override | str | None |
如果提供,请为工具使用此名称,而不是函数的名称。 | None |
description_override | str | None |
如果提供,请对工具使用此描述,而不是 函数的 docString 中。 | None |
docstring_style | DocstringStyle | None |
如果提供,请将此样式用于工具的文档字符串。如果未提供,则 我们将尝试自动检测样式。 | None |
use_docstring_info | bool |
如果为 True,则使用函数的文档字符串填充工具的 description 和 argument descriptions。 | True |
failure_error_function | ToolErrorFunction | None |
如果提供,请使用此函数在 工具调用失败。错误消息将发送到 LLM。如果传递 None,则 no 将发送错误消息,而是引发 Exception。 | default_tool_error_function |
Results
RunResultBase 数据类
基地:ABC
src/agents/result.py
input 实例属性
input: str | list[TResponseInputItem]
原始输入项,即调用 run() 之前的项。这可能是一个 mutated version (如果存在改变输入的 Handoff 输入过滤器)。
final_output instance-attribute
final_output: Any
最后一个代理的输出。
input_guardrail_results instance-attribute
input_guardrail_results: list[InputGuardrailResult]
输入消息的护栏结果。
output_guardrail_results instance-attribute
output_guardrail_results: list[OutputGuardrailResult]
代理的最终输出的 Guardrail 结果。
final_output_as
final_output_as(
cls: type[T], raise_if_incorrect_type: bool = False
) -> T
to_input_list
to_input_list() -> list[TResponseInputItem]
RunResult 数据类
RunResultStreaming 数据类
代理在流式处理模式下运行的结果。您可以使用该方法 在生成语义事件时接收语义事件。stream_events
流式处理方法将引发: - 如果代理超过max_turns限制,则出现 MaxTurnsExceeded 异常。 - 如果护栏跳闸,则出现 GuardrailTripwireTriggered 异常。
源码src/agents/result.py
current_turn instance-attribute
current_turn: int
当前轮次编号。
max_turns instance-attribute
max_turns: int
代理可以运行的最大轮次。
final_output instance-attribute
final_output: Any
代理的最终输出。在代理完成运行之前,此字段为 None。
is_complete 类属性
instance-attribute
is_complete: bool = False
代理是否已完成运行。
stream_events async
stream_events() -> AsyncIterator[StreamEvent]
在生成新项时流式传输新项的增量。我们使用的是 OpenAI 响应 API,所以这些是语义事件:每个事件都有一个字段 描述事件的类型以及该事件的数据。type
这将引发: - 如果代理超过max_turns限制,则出现 MaxTurnsExceeded 异常。 - 如果护栏跳闸,则出现 GuardrailTripwireTriggered 异常。