@@ -4,16 +4,16 @@ search:
44---
55# エージェント
66
7- エージェントは、アプリにおける中核的なビルディングブロックです。エージェントは、インストラクションとツールで構成された LLM です。
7+ エージェントはアプリの中心的な構成要素です。エージェントとは、指示とツールで構成された大規模言語モデル( LLM ) です。
88
99## 基本設定
1010
11- エージェントで最もよく設定するプロパティは次のとおりです:
11+ エージェントを設定する際によく使うプロパティは次のとおりです。
1212
13- - ` name ` : エージェントを識別する必須の文字列です。
14- - ` instructions ` : 開発者メッセージ、または system prompt とも呼ばれます。
15- - ` model ` : 使用する LLM を指定します。 ` model_settings ` を併用すると temperature、 top_p などのモデル調整パラメーターを設定できます。
16- - ` tools ` : エージェントがタスク達成のために使用できるツールです。
13+ - ` name ` : エージェントを識別する必須の文字列です。
14+ - ` instructions ` : 開発者メッセージ、またはシステムプロンプトとも呼ばれます。
15+ - ` model ` : 使用する LLM と、 ` temperature ` 、 ` top_p ` などのモデル調整用パラメーターを設定する任意の ` model_settings ` 。
16+ - ` tools ` : エージェントがタスクを達成するために利用できるツール群です。
1717
1818``` python
1919from agents import Agent, ModelSettings, function_tool
@@ -33,7 +33,7 @@ agent = Agent(
3333
3434## コンテキスト
3535
36- エージェントは ` context ` 型に対してジェネリックです。コンテキストは依存性注入の仕組みで 、` Runner.run() ` に渡すオブジェクトです。これはすべてのエージェント、ツール、ハンドオフなどに渡され、エージェント実行時の依存関係と状態を格納する入れ物として機能します。コンテキストには任意の Python オブジェクトを指定できます 。
36+ エージェントは ` context ` 型を汎用的に扱います。コンテキストは依存性注入のためのツールで 、` Runner.run() ` に渡すオブジェクトです。これはすべてのエージェント、ツール、ハンドオフなどに渡され、エージェント実行時の依存関係や状態をまとめて保持します。任意の Python オブジェクトをコンテキストとして提供できます 。
3737
3838``` python
3939@dataclass
@@ -52,7 +52,7 @@ agent = Agent[UserContext](
5252
5353## 出力タイプ
5454
55- 既定では 、エージェントはプレーンテキスト (つまり ` str ` ) を出力します。特定の型で出力させたい場合は ` output_type ` パラメーターを使用できます。よく使われるのは Pydantic オブジェクトですが 、Pydantic の TypeAdapter でラップできる型 — dataclass、list 、TypedDict など — であれば何でもサポートしています 。
55+ デフォルトでは 、エージェントはプレーンテキスト( ` str ` ) を出力します。特定の型で出力させたい場合は ` output_type ` パラメーターを使用します。一般的には [ Pydantic] ( https://docs.pydantic.dev/ ) オブジェクトを指定しますが 、Pydantic の [ TypeAdapter] ( https://docs.pydantic.dev/latest/api/type_adapter/ ) でラップできる型( dataclass、リスト 、TypedDict など)であれば利用できます 。
5656
5757``` python
5858from pydantic import BaseModel
@@ -73,11 +73,11 @@ agent = Agent(
7373
7474!!! note
7575
76- `output_type` を渡すと、モデルは通常のプレーンテキスト応答の代わりに structured outputs を使用するよう指示されます 。
76+ `output_type` を渡すと、モデルは通常のプレーンテキスト応答ではなく [ structured outputs](https://platform.openai.com/docs/guides/structured-outputs) を使用するようになります 。
7777
7878## ハンドオフ
7979
80- ハンドオフは、エージェントが委譲できるサブエージェントです。ハンドオフのリストを渡すと、必要に応じてエージェントがそれらに処理を委譲できます。これは、単一タスクに特化したモジュール式のエージェントをオーケストレーションする強力なパターンです 。詳細は [ handoffs] ( handoffs.md ) ドキュメントをご覧ください。
80+ ハンドオフは、エージェントが委譲できるサブエージェントです。ハンドオフのリストを渡すと、エージェントは必要に応じてそれらに委譲します。これにより、単一タスクに特化したモジュール型のエージェントをオーケストレーションする強力なパターンが実現します 。詳細は [ handoffs] ( handoffs.md ) ドキュメントをご覧ください。
8181
8282``` python
8383from agents import Agent
@@ -98,7 +98,7 @@ triage_agent = Agent(
9898
9999## 動的インストラクション
100100
101- 通常はエージェント作成時にインストラクションを指定しますが、関数を介して動的に渡すこともできます 。その関数はエージェントとコンテキストを受け取り、プロンプトを返す必要があります。通常の関数と ` async ` 関数の両方が利用可能です 。
101+ 多くの場合、エージェント作成時に instructions を指定しますが、関数を通じて動的に instructions を提供することも可能です 。その関数はエージェントとコンテキストを受け取り、プロンプトを返す必要があります。通常の関数と ` async ` 関数の両方を使用できます 。
102102
103103``` python
104104def dynamic_instructions (
@@ -115,13 +115,13 @@ agent = Agent[UserContext](
115115
116116## ライフサイクルイベント(フック)
117117
118- エージェントのライフサイクルを観察したい場合があります 。たとえば、イベントをログに記録したり、特定のイベント発生時にデータをプリフェッチしたりするケースです。そのようなときは ` hooks ` プロパティを使ってライフサイクルにフックできます 。[ ` AgentHooks ` ] [ agents.lifecycle.AgentHooks ] クラスを継承し、関心のあるメソッドをオーバーライドしてください 。
118+ エージェントのライフサイクルを監視したい場合があります 。たとえば、イベントをログに記録したり、特定のイベント発生時にデータを事前取得したりできます。 ` hooks ` プロパティを使ってエージェントのライフサイクルにフックできます 。[ ` AgentHooks ` ] [ agents.lifecycle.AgentHooks ] クラスをサブクラス化し、必要なメソッドをオーバーライドしてください 。
119119
120120## ガードレール
121121
122- ガードレールを使用すると、エージェントの実行と並行してユーザー入力のチェックやバリデーションを行えます 。たとえば、ユーザー入力の関連性をスクリーニングできます 。詳細は [ guardrails] ( guardrails.md ) ドキュメントを参照してください 。
122+ ガードレールを使用すると、エージェント実行と並行してユーザー入力に対するチェックやバリデーションを実行できます 。たとえば、ユーザー入力の関連性をスクリーニングすることが可能です 。詳細は [ guardrails] ( guardrails.md ) ドキュメントをご覧ください 。
123123
124- ## エージェントのクローン/ コピー
124+ ## エージェントのクローン/ コピー
125125
126126エージェントの ` clone() ` メソッドを使用すると、エージェントを複製し、任意のプロパティを変更できます。
127127
@@ -140,15 +140,109 @@ robot_agent = pirate_agent.clone(
140140
141141## ツール使用の強制
142142
143- ツールのリストを指定しても、 LLM が必ずしもツールを使用するとは限りません 。[ ` ModelSettings.tool_choice ` ] [ agents.model_settings.ModelSettings.tool_choice ] を設定することでツール使用を強制できます。指定可能な値は次のとおりです:
143+ ツールをリストで渡しても、必ずしも LLM がツールを使用するとは限りません 。[ ` ModelSettings.tool_choice ` ] [ agents.model_settings.ModelSettings.tool_choice ] を設定することでツール使用を強制できます。利用可能な値は次のとおりです。
144144
145- 1 . ` auto ` — LLM がツールを使うかどうかを判断します。
146- 2 . ` required ` — LLM にツールの使用を必須とします (ただしどのツールを使うかは自動で判断)。
147- 3 . ` none ` — LLM にツールを使用しないことを要求します。
148- 4 . 具体的な文字列 ( 例: ` my_tool ` ) — LLM にその特定のツールを使用させます。
145+ 1 . ` auto ` : LLM がツールを使用するか否かを決定します。
146+ 2 . ` required ` : LLM にツールの使用を必須とします(どのツールを使用するかは自動で選択)。
147+ 3 . ` none ` : LLM にツールを使用しないことを要求します。
148+ 4 . 特定の文字列( 例: ` my_tool ` )を設定すると、そのツールを必ず使用します。
149149
150- !!! note
150+ ``` python
151+ from agents import Agent, Runner, function_tool, ModelSettings
152+
153+ @function_tool
154+ def get_weather (city : str ) -> str :
155+ """ Returns weather info for the specified city."""
156+ return f " The weather in { city} is sunny "
157+
158+ agent = Agent(
159+ name = " Weather Agent" ,
160+ instructions = " Retrieve weather details." ,
161+ tools = [get_weather],
162+ model_settings = ModelSettings(tool_choice = " get_weather" )
163+ )
164+ ```
165+
166+ ## ツール使用時の挙動
167+
168+ ` Agent ` の ` tool_use_behavior ` パラメーターは、ツール出力の扱い方を制御します。
169+ - ` "run_llm_again" ` : デフォルト。ツールを実行後、その結果を LLM が処理して最終応答を生成します。
170+ - ` "stop_on_first_tool" ` : 最初のツール呼び出しの出力を最終応答として使用し、以降の LLM 処理は行いません。
151171
152- 無限ループを防ぐため、フレームワークはツール呼び出し後に `tool_choice` を自動的に "auto" にリセットします。この挙動は [`agent.reset_tool_choice`][agents.agent.Agent.reset_tool_choice] で設定できます。無限ループは、ツール結果が LLM に送られ、それにより `tool_choice` の設定でさらにツール呼び出しが生成される、というサイクルが延々と続くために発生します。
172+ ``` python
173+ from agents import Agent, Runner, function_tool, ModelSettings
174+
175+ @function_tool
176+ def get_weather (city : str ) -> str :
177+ """ Returns weather info for the specified city."""
178+ return f " The weather in { city} is sunny "
179+
180+ agent = Agent(
181+ name = " Weather Agent" ,
182+ instructions = " Retrieve weather details." ,
183+ tools = [get_weather],
184+ tool_use_behavior = " stop_on_first_tool"
185+ )
186+ ```
187+
188+ - ` StopAtTools(stop_at_tool_names=[...]) ` : 指定したいずれかのツールが呼び出された時点で停止し、そのツールの出力を最終応答として使用します。
189+ ``` python
190+ from agents import Agent, Runner, function_tool
191+ from agents.agent import StopAtTools
192+
193+ @function_tool
194+ def get_weather (city : str ) -> str :
195+ """ Returns weather info for the specified city."""
196+ return f " The weather in { city} is sunny "
197+
198+ @function_tool
199+ def sum_numbers (a : int , b : int ) -> int :
200+ """ Adds two numbers."""
201+ return a + b
202+
203+ agent = Agent(
204+ name = " Stop At Stock Agent" ,
205+ instructions = " Get weather or sum numbers." ,
206+ tools = [get_weather, sum_numbers],
207+ tool_use_behavior = StopAtTools(stop_at_tool_names = [" get_weather" ])
208+ )
209+ ```
210+ - ` ToolsToFinalOutputFunction ` : ツール結果を処理し、停止するか LLM 継続かを決定するカスタム関数です。
211+
212+ ``` python
213+ from agents import Agent, Runner, function_tool, FunctionToolResult, RunContextWrapper
214+ from agents.agent import ToolsToFinalOutputResult
215+ from typing import List, Any
216+
217+ @function_tool
218+ def get_weather (city : str ) -> str :
219+ """ Returns weather info for the specified city."""
220+ return f " The weather in { city} is sunny "
221+
222+ def custom_tool_handler (
223+ context : RunContextWrapper[Any],
224+ tool_results : List[FunctionToolResult]
225+ ) -> ToolsToFinalOutputResult:
226+ """ Processes tool results to decide final output."""
227+ for result in tool_results:
228+ if result.output and " sunny" in result.output:
229+ return ToolsToFinalOutputResult(
230+ is_final_output = True ,
231+ final_output = f " Final weather: { result.output} "
232+ )
233+ return ToolsToFinalOutputResult(
234+ is_final_output = False ,
235+ final_output = None
236+ )
237+
238+ agent = Agent(
239+ name = " Weather Agent" ,
240+ instructions = " Retrieve weather details." ,
241+ tools = [get_weather],
242+ tool_use_behavior = custom_tool_handler
243+ )
244+ ```
245+
246+ !!! note
153247
154- ツール呼び出し後に ( auto モードで続行するのではなく) エージェントを完全に停止させたい場合は、[`Agent.tool_use_behavior="stop_on_first_tool"`] を設定してください。これにより、ツールの出力をそのまま最終レスポンスとして使用し、追加の LLM 処理を行いません 。
248+ 無限ループを防ぐため、ツール呼び出し後にフレームワークは自動的に `tool_choice` を " auto" にリセットします。この挙動は [`agent.reset_tool_choice`][agents.agent.Agent.reset_tool_choice] で設定可能です。ツール結果が LLM に送られるたびに `tool_choice` により再度ツール呼び出しが発生し、無限に続く可能性があるためです 。
0 commit comments