From 502208e36b77fbdc0b0247e2f82ecb6378499ad2 Mon Sep 17 00:00:00 2001 From: Kazuhiro Sera Date: Mon, 20 Jul 2026 08:35:11 +0900 Subject: [PATCH] docs: fix runnable Python examples --- docs/agents.md | 8 ++++---- docs/context.md | 2 +- docs/human_in_the_loop.md | 2 +- docs/ja/agents.md | 10 +++++----- docs/ja/context.md | 2 +- docs/ja/human_in_the_loop.md | 2 +- docs/ja/models/index.md | 7 ++++++- docs/ja/sessions/advanced_sqlite_session.md | 2 +- docs/ja/tools.md | 9 +++++++-- docs/ja/voice/quickstart.md | 2 -- docs/ko/agents.md | 10 +++++----- docs/ko/context.md | 2 +- docs/ko/human_in_the_loop.md | 2 +- docs/ko/models/index.md | 7 ++++++- docs/ko/sessions/advanced_sqlite_session.md | 2 +- docs/ko/tools.md | 9 +++++++-- docs/ko/voice/quickstart.md | 2 -- docs/models/index.md | 7 ++++++- docs/sessions/advanced_sqlite_session.md | 2 +- docs/tools.md | 9 +++++++-- docs/voice/quickstart.md | 2 -- docs/zh/agents.md | 10 +++++----- docs/zh/context.md | 2 +- docs/zh/human_in_the_loop.md | 2 +- docs/zh/models/index.md | 7 ++++++- docs/zh/sessions/advanced_sqlite_session.md | 2 +- docs/zh/tools.md | 9 +++++++-- docs/zh/voice/quickstart.md | 2 -- 28 files changed, 83 insertions(+), 51 deletions(-) diff --git a/docs/agents.md b/docs/agents.md index ca07a3e0df..aff4c297b9 100644 --- a/docs/agents.md +++ b/docs/agents.md @@ -328,7 +328,7 @@ Supplying a list of tools doesn't always mean the LLM will use a tool. You can f When you are using OpenAI Responses tool search, named tool choices are more limited: you cannot target bare namespace names or deferred-only tools with `tool_choice`, and `tool_choice="tool_search"` does not target [`ToolSearchTool`][agents.tool.ToolSearchTool]. In those cases, prefer `auto` or `required`. See [Hosted tool search](tools.md#hosted-tool-search) for the Responses-specific constraints. ```python -from agents import Agent, Runner, function_tool, ModelSettings +from agents import Agent, function_tool, ModelSettings @function_tool def get_weather(city: str) -> str: @@ -351,7 +351,7 @@ The `tool_use_behavior` parameter in the `Agent` configuration controls how tool - `"stop_on_first_tool"`: The output of the first tool call is used as the final response, without further LLM processing. ```python -from agents import Agent, Runner, function_tool, ModelSettings +from agents import Agent, function_tool @function_tool def get_weather(city: str) -> str: @@ -369,7 +369,7 @@ agent = Agent( - `StopAtTools(stop_at_tool_names=[...])`: Stops if any specified tool is called, using its output as the final response. ```python -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool from agents.agent import StopAtTools @function_tool @@ -393,7 +393,7 @@ agent = Agent( - `ToolsToFinalOutputFunction`: A custom function that processes tool results and decides whether to stop or continue with the LLM. ```python -from agents import Agent, Runner, function_tool, FunctionToolResult, RunContextWrapper +from agents import Agent, function_tool, FunctionToolResult, RunContextWrapper from agents.agent import ToolsToFinalOutputResult from typing import List, Any diff --git a/docs/context.md b/docs/context.md index 47ba2bddb8..dfdd32484e 100644 --- a/docs/context.md +++ b/docs/context.md @@ -97,7 +97,7 @@ For this, you can use the [`ToolContext`][agents.tool_context.ToolContext] class ```python from typing import Annotated from pydantic import BaseModel, Field -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool from agents.tool_context import ToolContext class WeatherContext(BaseModel): diff --git a/docs/human_in_the_loop.md b/docs/human_in_the_loop.md index 907b545e37..76cd9154c3 100644 --- a/docs/human_in_the_loop.md +++ b/docs/human_in_the_loop.md @@ -13,7 +13,7 @@ This page focuses on the manual approval flow via `interruptions`. If your app c Set `needs_approval` to `True` to always require approval or provide an async function that decides per call. The callable receives the run context, parsed tool parameters, and the tool call ID. ```python -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool @function_tool(needs_approval=True) diff --git a/docs/ja/agents.md b/docs/ja/agents.md index 95292924b7..8ce787be4e 100644 --- a/docs/ja/agents.md +++ b/docs/ja/agents.md @@ -49,7 +49,7 @@ SDK は、OpenAI モデルに対してデフォルトで Responses API を使用 | `reset_tool_choice` | いいえ | ツール使用のループを回避するため、ツール呼び出し後に `tool_choice` をリセットします(デフォルト: `True`)。[ツール使用の強制](#forcing-tool-use)を参照してください。 | ```python -from agents import Agent, ModelSettings, function_tool +from agents import Agent, function_tool @function_tool def get_weather(city: str) -> str: @@ -332,7 +332,7 @@ robot_agent = pirate_agent.clone( OpenAI Responses のツール検索を使用する場合、名前付きツール選択肢にはより多くの制限があります。`tool_choice` では、単独の名前空間名や遅延専用ツールを指定できず、`tool_choice="tool_search"` で [`ToolSearchTool`][agents.tool.ToolSearchTool] を指定することもできません。このような場合は、`auto` または `required` を使用してください。Responses 固有の制約については、[ホステッドツール検索](tools.md#hosted-tool-search)を参照してください。 ```python -from agents import Agent, Runner, function_tool, ModelSettings +from agents import Agent, function_tool, ModelSettings @function_tool def get_weather(city: str) -> str: @@ -355,7 +355,7 @@ agent = Agent( - `"stop_on_first_tool"`: 最初のツール呼び出しの出力を、LLM による追加処理を行わずに最終応答として使用します。 ```python -from agents import Agent, Runner, function_tool, ModelSettings +from agents import Agent, function_tool @function_tool def get_weather(city: str) -> str: @@ -373,7 +373,7 @@ agent = Agent( - `StopAtTools(stop_at_tool_names=[...])`: 指定したツールのいずれかが呼び出された場合、その出力を最終応答として使用して停止します。 ```python -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool from agents.agent import StopAtTools @function_tool @@ -397,7 +397,7 @@ agent = Agent( - `ToolsToFinalOutputFunction`: ツールの実行結果を処理し、停止するか LLM で処理を続行するかを決定するカスタム関数です。 ```python -from agents import Agent, Runner, function_tool, FunctionToolResult, RunContextWrapper +from agents import Agent, function_tool, FunctionToolResult, RunContextWrapper from agents.agent import ToolsToFinalOutputResult from typing import List, Any diff --git a/docs/ja/context.md b/docs/ja/context.md index 9152944e89..3185eee160 100644 --- a/docs/ja/context.md +++ b/docs/ja/context.md @@ -101,7 +101,7 @@ if __name__ == "__main__": ```python from typing import Annotated from pydantic import BaseModel, Field -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool from agents.tool_context import ToolContext class WeatherContext(BaseModel): diff --git a/docs/ja/human_in_the_loop.md b/docs/ja/human_in_the_loop.md index e689da43c9..938a6a0987 100644 --- a/docs/ja/human_in_the_loop.md +++ b/docs/ja/human_in_the_loop.md @@ -17,7 +17,7 @@ search: 常に承認を要求するには `needs_approval` を `True` に設定するか、呼び出しごとに判定する async 関数を指定します。この呼び出し可能オブジェクトは、実行コンテキスト、解析済みのツールパラメーター、ツール呼び出し ID を受け取ります。 ```python -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool @function_tool(needs_approval=True) diff --git a/docs/ja/models/index.md b/docs/ja/models/index.md index c1e5df0399..d781836606 100644 --- a/docs/ja/models/index.md +++ b/docs/ja/models/index.md @@ -353,9 +353,10 @@ agent= Agent(name="Helping Agent", instructions="You are a Helping Agent", model SDK は [`OpenAIResponsesModel`][agents.models.openai_responses.OpenAIResponsesModel] と [`OpenAIChatCompletionsModel`][agents.models.openai_chatcompletions.OpenAIChatCompletionsModel] の両方の形式をサポートしていますが、2 つの形式ではサポートされる機能とツールが異なるため、各ワークフローで単一のモデル形式を使用することを推奨します。ワークフローでモデル形式を組み合わせる必要がある場合は、使用するすべての機能が両方で利用できることを確認してください。 ```python -from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel import asyncio +from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel + spanish_agent = Agent( name="Spanish agent", instructions="You only speak Spanish.", @@ -381,6 +382,10 @@ triage_agent = Agent( async def main(): result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?") print(result.final_output) + + +if __name__ == "__main__": + asyncio.run(main()) ``` 1. OpenAI モデルの名前を直接設定します。 diff --git a/docs/ja/sessions/advanced_sqlite_session.md b/docs/ja/sessions/advanced_sqlite_session.md index 2f5cb469ea..6927602fd0 100644 --- a/docs/ja/sessions/advanced_sqlite_session.md +++ b/docs/ja/sessions/advanced_sqlite_session.md @@ -68,7 +68,7 @@ session = AdvancedSQLiteSession( # With persistent storage session = AdvancedSQLiteSession( session_id="user_123", - db_path="path/to/conversations.db", + db_path="conversations.db", create_tables=True ) diff --git a/docs/ja/tools.md b/docs/ja/tools.md index 44a327477a..080394416d 100644 --- a/docs/ja/tools.md +++ b/docs/ja/tools.md @@ -466,7 +466,7 @@ def score_b(score: Annotated[int, Field(..., ge=0, le=100, description="Score fr ```python import asyncio -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool @function_tool(timeout=2.0) @@ -549,9 +549,10 @@ def get_user_profile(user_id: str) -> str: ワークフローによっては、制御をハンドオフする代わりに、中央のエージェントで特化型エージェントのネットワークをオーケストレーションしたい場合があります。これは、エージェントをツールとしてモデル化することで実現できます。 ```python -from agents import Agent, Runner import asyncio +from agents import Agent, Runner + spanish_agent = Agent( name="Spanish agent", instructions="You translate the user's message to Spanish", @@ -583,6 +584,10 @@ orchestrator_agent = Agent( async def main(): result = await Runner.run(orchestrator_agent, input="Say 'Hello, how are you?' in Spanish.") print(result.final_output) + + +if __name__ == "__main__": + asyncio.run(main()) ``` ### ツールエージェントのカスタマイズ diff --git a/docs/ja/voice/quickstart.md b/docs/ja/voice/quickstart.md index 6fa92f062e..9cd40857cb 100644 --- a/docs/ja/voice/quickstart.md +++ b/docs/ja/voice/quickstart.md @@ -51,7 +51,6 @@ graph LR まず、いくつかのエージェントをセットアップします。この SDK でエージェントを構築した経験があれば、馴染みのある作業でしょう。ここでは、2 つのエージェント、1 つのハンドオフ、1 つのツールを用意します。 ```python -import asyncio import random from agents import ( @@ -136,7 +135,6 @@ import sounddevice as sd from agents import ( Agent, function_tool, - set_tracing_disabled, ) from agents.voice import ( AudioInput, diff --git a/docs/ko/agents.md b/docs/ko/agents.md index 47262b9773..ad7c47eb62 100644 --- a/docs/ko/agents.md +++ b/docs/ko/agents.md @@ -49,7 +49,7 @@ SDK는 OpenAI 모델에 기본적으로 Responses API를 사용하지만, 여기 | `reset_tool_choice` | 아니요 | 도구 사용 루프를 방지하기 위해 도구 호출 후 `tool_choice`를 재설정합니다(기본값: `True`). [도구 사용 강제](#forcing-tool-use)를 참조하세요. | ```python -from agents import Agent, ModelSettings, function_tool +from agents import Agent, function_tool @function_tool def get_weather(city: str) -> str: @@ -332,7 +332,7 @@ robot_agent = pirate_agent.clone( OpenAI Responses 도구 검색을 사용할 때는 이름이 지정된 도구 선택에 더 많은 제한이 있습니다. `tool_choice`로 단독 네임스페이스 이름이나 지연 전용 도구를 지정할 수 없으며, `tool_choice="tool_search"`는 [`ToolSearchTool`][agents.tool.ToolSearchTool]을 대상으로 하지 않습니다. 이러한 경우에는 `auto` 또는 `required`를 사용하는 것이 좋습니다. Responses 전용 제약 조건은 [호스티드 툴 검색](tools.md#hosted-tool-search)을 참조하세요. ```python -from agents import Agent, Runner, function_tool, ModelSettings +from agents import Agent, function_tool, ModelSettings @function_tool def get_weather(city: str) -> str: @@ -355,7 +355,7 @@ agent = Agent( - `"stop_on_first_tool"`: 추가적인 LLM 처리 없이 첫 번째 도구 호출의 출력을 최종 응답으로 사용합니다. ```python -from agents import Agent, Runner, function_tool, ModelSettings +from agents import Agent, function_tool @function_tool def get_weather(city: str) -> str: @@ -373,7 +373,7 @@ agent = Agent( - `StopAtTools(stop_at_tool_names=[...])`: 지정된 도구 중 하나가 호출되면 해당 출력을 최종 응답으로 사용하고 중지합니다. ```python -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool from agents.agent import StopAtTools @function_tool @@ -397,7 +397,7 @@ agent = Agent( - `ToolsToFinalOutputFunction`: 도구 결과를 처리하고 LLM을 중지할지 계속 실행할지 결정하는 사용자 지정 함수입니다. ```python -from agents import Agent, Runner, function_tool, FunctionToolResult, RunContextWrapper +from agents import Agent, function_tool, FunctionToolResult, RunContextWrapper from agents.agent import ToolsToFinalOutputResult from typing import List, Any diff --git a/docs/ko/context.md b/docs/ko/context.md index b86aa44c2b..e0d4b6c0ef 100644 --- a/docs/ko/context.md +++ b/docs/ko/context.md @@ -101,7 +101,7 @@ if __name__ == "__main__": ```python from typing import Annotated from pydantic import BaseModel, Field -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool from agents.tool_context import ToolContext class WeatherContext(BaseModel): diff --git a/docs/ko/human_in_the_loop.md b/docs/ko/human_in_the_loop.md index 00771b01c6..c883e64868 100644 --- a/docs/ko/human_in_the_loop.md +++ b/docs/ko/human_in_the_loop.md @@ -17,7 +17,7 @@ search: 항상 승인을 요구하려면 `needs_approval`을 `True`로 설정하거나, 호출마다 결정하는 비동기 함수를 제공합니다. 호출 가능한 함수는 실행 컨텍스트, 파싱된 도구 매개변수, 도구 호출 ID를 받습니다. ```python -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool @function_tool(needs_approval=True) diff --git a/docs/ko/models/index.md b/docs/ko/models/index.md index 888ed371d7..47a7ca30fc 100644 --- a/docs/ko/models/index.md +++ b/docs/ko/models/index.md @@ -353,9 +353,10 @@ agent= Agent(name="Helping Agent", instructions="You are a Helping Agent", model SDK는 [`OpenAIResponsesModel`][agents.models.openai_responses.OpenAIResponsesModel]과 [`OpenAIChatCompletionsModel`][agents.models.openai_chatcompletions.OpenAIChatCompletionsModel] 형식을 모두 지원하지만, 두 형식이 서로 다른 기능과 도구 집합을 지원하므로 각 워크플로에서는 하나의 모델 형식만 사용하는 것이 좋습니다. 워크플로에서 모델 형식을 혼합해야 한다면 사용하는 모든 기능을 양쪽 모두에서 사용할 수 있는지 확인하세요. ```python -from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel import asyncio +from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel + spanish_agent = Agent( name="Spanish agent", instructions="You only speak Spanish.", @@ -381,6 +382,10 @@ triage_agent = Agent( async def main(): result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?") print(result.final_output) + + +if __name__ == "__main__": + asyncio.run(main()) ``` 1. OpenAI 모델의 이름을 직접 설정합니다. diff --git a/docs/ko/sessions/advanced_sqlite_session.md b/docs/ko/sessions/advanced_sqlite_session.md index 3ebe601d76..53029430e1 100644 --- a/docs/ko/sessions/advanced_sqlite_session.md +++ b/docs/ko/sessions/advanced_sqlite_session.md @@ -68,7 +68,7 @@ session = AdvancedSQLiteSession( # With persistent storage session = AdvancedSQLiteSession( session_id="user_123", - db_path="path/to/conversations.db", + db_path="conversations.db", create_tables=True ) diff --git a/docs/ko/tools.md b/docs/ko/tools.md index ea3430ae95..6c947876fe 100644 --- a/docs/ko/tools.md +++ b/docs/ko/tools.md @@ -466,7 +466,7 @@ def score_b(score: Annotated[int, Field(..., ge=0, le=100, description="Score fr ```python import asyncio -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool @function_tool(timeout=2.0) @@ -549,9 +549,10 @@ def get_user_profile(user_id: str) -> str: 일부 워크플로에서는 제어를 핸드오프하는 대신 중앙 에이전트가 전문 에이전트 네트워크를 오케스트레이션하도록 할 수 있습니다. 에이전트를 도구로 모델링하여 이를 구현할 수 있습니다. ```python -from agents import Agent, Runner import asyncio +from agents import Agent, Runner + spanish_agent = Agent( name="Spanish agent", instructions="You translate the user's message to Spanish", @@ -583,6 +584,10 @@ orchestrator_agent = Agent( async def main(): result = await Runner.run(orchestrator_agent, input="Say 'Hello, how are you?' in Spanish.") print(result.final_output) + + +if __name__ == "__main__": + asyncio.run(main()) ``` ### 도구 에이전트 사용자 지정 diff --git a/docs/ko/voice/quickstart.md b/docs/ko/voice/quickstart.md index 85ad0223dd..4d0742e1d7 100644 --- a/docs/ko/voice/quickstart.md +++ b/docs/ko/voice/quickstart.md @@ -51,7 +51,6 @@ graph LR 먼저 몇 가지 에이전트를 설정하겠습니다. 이 SDK로 에이전트를 만들어 본 적이 있다면 익숙할 것입니다. 몇 개의 에이전트와 하나의 핸드오프, 하나의 도구를 사용합니다. ```python -import asyncio import random from agents import ( @@ -136,7 +135,6 @@ import sounddevice as sd from agents import ( Agent, function_tool, - set_tracing_disabled, ) from agents.voice import ( AudioInput, diff --git a/docs/models/index.md b/docs/models/index.md index b2625d973f..bd0db9db4f 100644 --- a/docs/models/index.md +++ b/docs/models/index.md @@ -349,9 +349,10 @@ Within a single workflow, you may want to use different models for each agent. F While our SDK supports both the [`OpenAIResponsesModel`][agents.models.openai_responses.OpenAIResponsesModel] and the [`OpenAIChatCompletionsModel`][agents.models.openai_chatcompletions.OpenAIChatCompletionsModel] shapes, we recommend using a single model shape for each workflow because the two shapes support a different set of features and tools. If your workflow requires mixing and matching model shapes, make sure that all the features you're using are available on both. ```python -from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel import asyncio +from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel + spanish_agent = Agent( name="Spanish agent", instructions="You only speak Spanish.", @@ -377,6 +378,10 @@ triage_agent = Agent( async def main(): result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?") print(result.final_output) + + +if __name__ == "__main__": + asyncio.run(main()) ``` 1. Sets the name of an OpenAI model directly. diff --git a/docs/sessions/advanced_sqlite_session.md b/docs/sessions/advanced_sqlite_session.md index 62155a3fbd..2e50ea9fc0 100644 --- a/docs/sessions/advanced_sqlite_session.md +++ b/docs/sessions/advanced_sqlite_session.md @@ -64,7 +64,7 @@ session = AdvancedSQLiteSession( # With persistent storage session = AdvancedSQLiteSession( session_id="user_123", - db_path="path/to/conversations.db", + db_path="conversations.db", create_tables=True ) diff --git a/docs/tools.md b/docs/tools.md index 92664b731b..42c1ff22b0 100644 --- a/docs/tools.md +++ b/docs/tools.md @@ -462,7 +462,7 @@ You can set per-call timeouts for async function tools with `@function_tool(time ```python import asyncio -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool @function_tool(timeout=2.0) @@ -545,9 +545,10 @@ If you are manually creating a `FunctionTool` object, then you must handle error In some workflows, you may want a central agent to orchestrate a network of specialized agents, instead of handing off control. You can do this by modeling agents as tools. ```python -from agents import Agent, Runner import asyncio +from agents import Agent, Runner + spanish_agent = Agent( name="Spanish agent", instructions="You translate the user's message to Spanish", @@ -579,6 +580,10 @@ orchestrator_agent = Agent( async def main(): result = await Runner.run(orchestrator_agent, input="Say 'Hello, how are you?' in Spanish.") print(result.final_output) + + +if __name__ == "__main__": + asyncio.run(main()) ``` ### Customizing tool-agents diff --git a/docs/voice/quickstart.md b/docs/voice/quickstart.md index cd9126447e..ec0f571a34 100644 --- a/docs/voice/quickstart.md +++ b/docs/voice/quickstart.md @@ -47,7 +47,6 @@ graph LR First, let's set up some Agents. This should feel familiar to you if you've built any agents with this SDK. We'll have a couple of Agents, a handoff, and a tool. ```python -import asyncio import random from agents import ( @@ -132,7 +131,6 @@ import sounddevice as sd from agents import ( Agent, function_tool, - set_tracing_disabled, ) from agents.voice import ( AudioInput, diff --git a/docs/zh/agents.md b/docs/zh/agents.md index 163d5df837..4babc27135 100644 --- a/docs/zh/agents.md +++ b/docs/zh/agents.md @@ -49,7 +49,7 @@ search: | `reset_tool_choice` | 否 | 在工具调用后重置`tool_choice`(默认值:`True`),以避免工具使用循环。请参阅[强制使用工具](#forcing-tool-use)。 | ```python -from agents import Agent, ModelSettings, function_tool +from agents import Agent, function_tool @function_tool def get_weather(city: str) -> str: @@ -332,7 +332,7 @@ robot_agent = pirate_agent.clone( 使用OpenAI Responses工具搜索时,具名工具选择的限制更多:你不能通过`tool_choice`将裸命名空间名称或仅延迟加载的工具设为目标,并且`tool_choice="tool_search"`不会以[`ToolSearchTool`][agents.tool.ToolSearchTool]为目标。在这些情况下,建议使用`auto`或`required`。有关Responses特有的限制,请参阅[托管工具搜索](tools.md#hosted-tool-search)。 ```python -from agents import Agent, Runner, function_tool, ModelSettings +from agents import Agent, function_tool, ModelSettings @function_tool def get_weather(city: str) -> str: @@ -355,7 +355,7 @@ agent = Agent( - `"stop_on_first_tool"`:将首次工具调用的输出用作最终响应,不再由LLM进一步处理。 ```python -from agents import Agent, Runner, function_tool, ModelSettings +from agents import Agent, function_tool @function_tool def get_weather(city: str) -> str: @@ -373,7 +373,7 @@ agent = Agent( - `StopAtTools(stop_at_tool_names=[...])`:如果调用了任何指定工具,则停止运行,并将其输出用作最终响应。 ```python -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool from agents.agent import StopAtTools @function_tool @@ -397,7 +397,7 @@ agent = Agent( - `ToolsToFinalOutputFunction`:处理工具结果并决定是停止还是继续调用LLM的自定义函数。 ```python -from agents import Agent, Runner, function_tool, FunctionToolResult, RunContextWrapper +from agents import Agent, function_tool, FunctionToolResult, RunContextWrapper from agents.agent import ToolsToFinalOutputResult from typing import List, Any diff --git a/docs/zh/context.md b/docs/zh/context.md index 61f20aaa84..9fcfd96b88 100644 --- a/docs/zh/context.md +++ b/docs/zh/context.md @@ -101,7 +101,7 @@ if __name__ == "__main__": ```python from typing import Annotated from pydantic import BaseModel, Field -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool from agents.tool_context import ToolContext class WeatherContext(BaseModel): diff --git a/docs/zh/human_in_the_loop.md b/docs/zh/human_in_the_loop.md index fcca10646e..6e79517367 100644 --- a/docs/zh/human_in_the_loop.md +++ b/docs/zh/human_in_the_loop.md @@ -17,7 +17,7 @@ search: 将 `needs_approval` 设置为 `True` 可始终要求审批,或提供一个异步函数按每次调用做出决定。该可调用对象会接收运行上下文、解析后的工具参数以及工具调用 ID。 ```python -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool @function_tool(needs_approval=True) diff --git a/docs/zh/models/index.md b/docs/zh/models/index.md index 1f26849a0a..a372c08a11 100644 --- a/docs/zh/models/index.md +++ b/docs/zh/models/index.md @@ -353,9 +353,10 @@ agent= Agent(name="Helping Agent", instructions="You are a Helping Agent", model 尽管我们的 SDK 同时支持 [`OpenAIResponsesModel`][agents.models.openai_responses.OpenAIResponsesModel] 和 [`OpenAIChatCompletionsModel`][agents.models.openai_chatcompletions.OpenAIChatCompletionsModel] 形式,但我们建议每个工作流使用单一模型形式,因为这两种形式支持的功能和工具集合不同。如果您的工作流需要混用不同模型形式,请确保您使用的所有功能均受两者支持。 ```python -from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel import asyncio +from agents import Agent, Runner, AsyncOpenAI, OpenAIChatCompletionsModel + spanish_agent = Agent( name="Spanish agent", instructions="You only speak Spanish.", @@ -381,6 +382,10 @@ triage_agent = Agent( async def main(): result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?") print(result.final_output) + + +if __name__ == "__main__": + asyncio.run(main()) ``` 1. 直接设置 OpenAI 模型的名称。 diff --git a/docs/zh/sessions/advanced_sqlite_session.md b/docs/zh/sessions/advanced_sqlite_session.md index 70c42a86f2..d52d39c585 100644 --- a/docs/zh/sessions/advanced_sqlite_session.md +++ b/docs/zh/sessions/advanced_sqlite_session.md @@ -68,7 +68,7 @@ session = AdvancedSQLiteSession( # With persistent storage session = AdvancedSQLiteSession( session_id="user_123", - db_path="path/to/conversations.db", + db_path="conversations.db", create_tables=True ) diff --git a/docs/zh/tools.md b/docs/zh/tools.md index 9136ae74cc..457ff86c77 100644 --- a/docs/zh/tools.md +++ b/docs/zh/tools.md @@ -466,7 +466,7 @@ def score_b(score: Annotated[int, Field(..., ge=0, le=100, description="Score fr ```python import asyncio -from agents import Agent, Runner, function_tool +from agents import Agent, function_tool @function_tool(timeout=2.0) @@ -549,9 +549,10 @@ def get_user_profile(user_id: str) -> str: 在某些工作流中,你可能希望由一个中央智能体编排由多个专用智能体组成的网络,而不是转移控制权。你可以通过将智能体建模为工具来实现这一点。 ```python -from agents import Agent, Runner import asyncio +from agents import Agent, Runner + spanish_agent = Agent( name="Spanish agent", instructions="You translate the user's message to Spanish", @@ -583,6 +584,10 @@ orchestrator_agent = Agent( async def main(): result = await Runner.run(orchestrator_agent, input="Say 'Hello, how are you?' in Spanish.") print(result.final_output) + + +if __name__ == "__main__": + asyncio.run(main()) ``` ### 工具智能体自定义 diff --git a/docs/zh/voice/quickstart.md b/docs/zh/voice/quickstart.md index 302a09bb09..c962ed0446 100644 --- a/docs/zh/voice/quickstart.md +++ b/docs/zh/voice/quickstart.md @@ -51,7 +51,6 @@ graph LR 首先,我们来设置一些智能体。如果你曾使用此 SDK 构建过智能体,这部分应该会很熟悉。我们将使用两个智能体、一次任务转移和一个工具。 ```python -import asyncio import random from agents import ( @@ -136,7 +135,6 @@ import sounddevice as sd from agents import ( Agent, function_tool, - set_tracing_disabled, ) from agents.voice import ( AudioInput,