示例 - OpenAI Agents SDK
来源 (中文翻译版): https://openai.github.io/openai-agents-python/examples/ 抓取时间: 2026-07-21 16:18:56
示例
查看仓库示例部分中 SDK 的各种示例实现。这些示例按几个类别组织,展示了不同的模式和功能。
类别
- agent_patterns: 此类别中的示例展示常见的智能体设计模式,例如
- 确定性工作流
- 智能体作为工具
- 带有流式事件的智能体作为工具(
examples/agent_patterns/agents_as_tools_streaming.py) - 带有结构化输入参数的智能体作为工具(
examples/agent_patterns/agents_as_tools_structured.py) - 并行智能体执行
- 条件工具使用
- 强制使用不同行为的工具(
examples/agent_patterns/forcing_tool_use.py) - 输入/输出护栏
- 大语言模型作为裁判
- 路由
- 流式护栏
- 带有工具审批和状态序列化的人在回路(
examples/agent_patterns/human_in_the_loop.py) - 带流式的人在回路(
examples/agent_patterns/human_in_the_loop_stream.py) - 审批流程的自定义拒绝消息(
examples/agent_patterns/human_in_the_loop_custom_rejection.py)
- basic: 这些示例展示 SDK 的基础功能,例如
- Hello world 示例(默认模型、GPT-5、开源权重模型)
- 智能体生命周期管理
- Run hooks 和 agent hooks 生命周期示例(
examples/basic/lifecycle_example.py) - 动态系统提示
- 基本工具使用(
examples/basic/tools.py) - 工具输入/输出护栏(
examples/basic/tool_guardrails.py) - 图像工具输出(
examples/basic/image_tool_output.py) - 流式输出(文本、项目、函数调用参数)
- 带有跨轮次共享会话助手的 Responses websocket 传输(
examples/basic/stream_ws.py) - 提示模板
- 文件处理(本地和远程,图像和 PDF)
- 使用情况跟踪
- Runner 管理的重试设置(
examples/basic/retry.py) - 通过第三方适配器的 Runner 管理的重试(
examples/basic/retry_litellm.py) - 非严格输出类型
- 先前响应 ID 的使用
- customer_service: 航空公司的客户服务系统示例。
- financial_research_agent: 一个金融研究智能体,展示使用智能体和工具进行金融数据分析的结构化研究工作流。
- handoffs: 带有消息过滤的智能体切换的实际示例,包括:
- 消息过滤器示例(
examples/handoffs/message_filter.py) - 带流式的消息过滤器(
examples/handoffs/message_filter_streaming.py)
- 消息过滤器示例(
- hosted_mcp: 展示如何将托管 MCP(模型上下文协议)与 OpenAI Responses API 一起使用的示例,包括:
- 无需审批的简单托管 MCP(
examples/hosted_mcp/simple.py) - MCP 连接器,如 Google Calendar(
examples/hosted_mcp/connectors.py) - 带有基于中断审批的人在回路(
examples/hosted_mcp/human_in_the_loop.py) - MCP 工具调用的审批回调(
examples/hosted_mcp/on_approval.py)
- 无需审批的简单托管 MCP(
- mcp: 学习如何使用 MCP(模型上下文协议)构建智能体,包括:
- 文件系统示例
- Git 示例
- MCP 提示服务器示例
- SSE(服务器发送事件)示例
- SSE 远程服务器连接(
examples/mcp/sse_remote_example) - 可流式 HTTP 示例
- 可流式 HTTP 远程连接(
examples/mcp/streamable_http_remote_example) - 用于可流式 HTTP 的自定义 HTTP 客户端工厂(
examples/mcp/streamablehttp_custom_client_example) - 使用
MCPUtil.get_all_function_tools预取所有 MCP 工具(examples/mcp/get_all_mcp_tools_example) - 带有 FastAPI 的 MCPServerManager(
examples/mcp/manager_example) - MCP 工具过滤(
examples/mcp/tool_filter_example)
- memory: 智能体不同内存实现的示例,包括:
- SQLite 会话存储
- 高级 SQLite 会话存储
- Redis 会话存储
- SQLAlchemy 会话存储
- Dapr 状态存储会话存储
- 加密会话存储
- OpenAI Conversations 会话存储
- Responses 压缩会话存储
- 使用
ModelSettings(store=False)的无状态 Responses 压缩(examples/memory/compaction_session_stateless_example.py) - 文件支持的会话存储(
examples/memory/file_session.py) - 带有人在回路的文件支持会话(
examples/memory/file_hitl_example.py) - 带有人在回路的 SQLite 内存会话(
examples/memory/memory_session_hitl_example.py) - 带有人在回路的 OpenAI Conversations 会话(
examples/memory/openai_session_hitl_example.py) - 跨会话的 HITL 审批/拒绝场景(
examples/memory/hitl_session_scenario.py)
- model_providers: 探索如何将非 OpenAI 模型与 SDK 一起使用,包括自定义提供程序和第三方适配器。
- realtime: 展示如何使用 SDK 构建实时体验的示例,包括:
- 带有结构化文本和图像消息的 Web 应用程序模式
- 命令行音频循环和播放处理
- 通过 WebSocket 的 Twilio Media Streams 集成
- 使用 Realtime Calls API 附加流的 Twilio SIP 集成
- reasoning_content: 展示如何使用推理内容的示例,包括:
- 使用 Runner API 的推理内容,流式和非流式(
examples/reasoning_content/runner_example.py) - 通过 OpenRouter 使用 OSS 模型的推理内容(
examples/reasoning_content/gpt_oss_stream.py) - 基本推理内容示例(
examples/reasoning_content/main.py)
- 使用 Runner API 的推理内容,流式和非流式(
- research_bot: 简单的深度研究克隆,展示复杂的多智能体研究工作流。
- sandbox: 在隔离工作区中运行智能体的示例,包括:
- 基本沙箱智能体设置(
examples/sandbox/basic.py) - Unix-local 和 Docker 沙箱生命周期示例
- 沙箱支持的切换(
examples/sandbox/handoffs.py) - 沙箱内存和快照恢复(
examples/sandbox/memory.py) - 作为工具暴露的沙箱智能体(
examples/sandbox/sandbox_agents_as_tools.py)
- 基本沙箱智能体设置(
- tools: 学习如何实现 OpenAI 托管工具和实验性 Codex 工具,例如:
- Web 搜索和带过滤器的 Web 搜索
- 文件搜索
- 代码解释器
- 带有文件编辑和审批的应用补丁工具(
examples/tools/apply_patch.py) - 带有审批回调的 Shell 工具执行(
examples/tools/shell.py) - 带有基于中断的人在回路审批的 Shell 工具(
examples/tools/shell_human_in_the_loop.py) - 带内联技能的托管容器 shell(
examples/tools/container_shell_inline_skill.py) - 带技能引用的托管容器 shell(
examples/tools/container_shell_skill_reference.py) - 带本地技能的本地 shell(
examples/tools/local_shell_skill.py) - 带命名空间和延迟工具的工具搜索(
examples/tools/tool_search.py) - 计算机使用
- 图像生成
- 实验性 Codex 工具工作流(
examples/tools/codex.py) - 实验性 Codex 同线程工作流(
examples/tools/codex_same_thread.py)
- voice: 查看语音智能体的示例,使用我们的 TTS 和 STT 模型,包括流式语音示例。