|
| 1 | +# エージェント |
| 2 | + |
| 3 | +エージェント はアプリケーションの基本的な構成要素です。エージェント は、大規模言語モデル(LLM)であり、 instructions と tools を用いて設定されます。 |
| 4 | + |
| 5 | +## 基本設定 |
| 6 | + |
| 7 | +エージェント の設定でよく使われるプロパティは以下の通りです。 |
| 8 | + |
| 9 | +- `instructions` : 開発者メッセージまたはシステムプロンプトとも呼ばれます。 |
| 10 | +- `model` : 使用する LLM を指定します。オプションで `model_settings` を指定し、temperature や top_p などのモデル調整パラメータを設定できます。 |
| 11 | +- `tools` : エージェント がタスクを達成するために使用できるツールです。 |
| 12 | + |
| 13 | +```python |
| 14 | +from agents import Agent, ModelSettings, function_tool |
| 15 | + |
| 16 | +@function_tool |
| 17 | +def get_weather(city: str) -> str: |
| 18 | + return f"The weather in {city} is sunny" |
| 19 | + |
| 20 | +agent = Agent( |
| 21 | + name="Haiku agent", |
| 22 | + instructions="Always respond in haiku form", |
| 23 | + model="o3-mini", |
| 24 | + tools=[get_weather], |
| 25 | +) |
| 26 | +``` |
| 27 | + |
| 28 | +## コンテキスト |
| 29 | + |
| 30 | +エージェント は、 `context` 型に対してジェネリックです。コンテキストは依存性注入のためのツールであり、作成したオブジェクトを `Runner.run()` に渡すことで、各エージェント、ツール、ハンドオフなどに渡されます。これはエージェントの実行に必要な依存関係や状態をまとめて保持するためのものです。任意の Python オブジェクトをコンテキストとして提供できます。 |
| 31 | + |
| 32 | +```python |
| 33 | +@dataclass |
| 34 | +class UserContext: |
| 35 | + uid: str |
| 36 | + is_pro_user: bool |
| 37 | + |
| 38 | + async def fetch_purchases() -> list[Purchase]: |
| 39 | + return ... |
| 40 | + |
| 41 | +agent = Agent[UserContext]( |
| 42 | + ..., |
| 43 | +) |
| 44 | +``` |
| 45 | + |
| 46 | +## 出力タイプ |
| 47 | + |
| 48 | +デフォルトでは、エージェント はプレーンテキスト(つまり `str` )を出力します。特定の型の出力を生成させたい場合は、 `output_type` パラメータを使用します。一般的には [Pydantic](https://docs.pydantic.dev/) オブジェクトを使用しますが、Pydantic の [TypeAdapter](https://docs.pydantic.dev/latest/api/type_adapter/) でラップ可能な型(データクラス、リスト、TypedDict など)であればどのような型でもサポートしています。 |
| 49 | + |
| 50 | +```python |
| 51 | +from pydantic import BaseModel |
| 52 | +from agents import Agent |
| 53 | + |
| 54 | + |
| 55 | +class CalendarEvent(BaseModel): |
| 56 | + name: str |
| 57 | + date: str |
| 58 | + participants: list[str] |
| 59 | + |
| 60 | +agent = Agent( |
| 61 | + name="Calendar extractor", |
| 62 | + instructions="Extract calendar events from text", |
| 63 | + output_type=CalendarEvent, |
| 64 | +) |
| 65 | +``` |
| 66 | + |
| 67 | +!!! note |
| 68 | + |
| 69 | + `output_type` を指定すると、モデルは通常のプレーンテキストのレスポンスではなく、 [structured outputs](https://platform.openai.com/docs/guides/structured-outputs) を使用します。 |
| 70 | + |
| 71 | +## ハンドオフ |
| 72 | + |
| 73 | +ハンドオフ は、エージェント が処理を委譲できるサブエージェントです。ハンドオフのリストを提供すると、エージェント は必要に応じてそれらに処理を委譲できます。これは、特定のタスクに特化したモジュール型のエージェントを組み合わせて調整するための強力なパターンです。詳細は [ハンドオフ](handoffs.md) のドキュメントを参照してください。 |
| 74 | + |
| 75 | +```python |
| 76 | +from agents import Agent |
| 77 | + |
| 78 | +booking_agent = Agent(...) |
| 79 | +refund_agent = Agent(...) |
| 80 | + |
| 81 | +triage_agent = Agent( |
| 82 | + name="Triage agent", |
| 83 | + instructions=( |
| 84 | + "Help the user with their questions." |
| 85 | + "If they ask about booking, handoff to the booking agent." |
| 86 | + "If they ask about refunds, handoff to the refund agent." |
| 87 | + ), |
| 88 | + handoffs=[booking_agent, refund_agent], |
| 89 | +) |
| 90 | +``` |
| 91 | + |
| 92 | +## 動的な instructions |
| 93 | + |
| 94 | +多くの場合、エージェント 作成時に instructions を指定しますが、関数を通じて動的に instructions を提供することも可能です。この関数はエージェントとコンテキストを受け取り、プロンプトを返します。通常の関数と `async` 関数の両方が使用可能です。 |
| 95 | + |
| 96 | +```python |
| 97 | +def dynamic_instructions( |
| 98 | + context: RunContextWrapper[UserContext], agent: Agent[UserContext] |
| 99 | +) -> str: |
| 100 | + return f"The user's name is {context.context.name}. Help them with their questions." |
| 101 | + |
| 102 | + |
| 103 | +agent = Agent[UserContext]( |
| 104 | + name="Triage agent", |
| 105 | + instructions=dynamic_instructions, |
| 106 | +) |
| 107 | +``` |
| 108 | + |
| 109 | +## ライフサイクルイベント(フック) |
| 110 | + |
| 111 | +エージェント のライフサイクルを監視したい場合があります。例えば、イベントをログに記録したり、特定のイベント発生時にデータを事前取得したりできます。エージェント のライフサイクルにフックするには、 `hooks` プロパティを使用します。[`AgentHooks`][agents.lifecycle.AgentHooks] クラスをサブクラス化し、関心のあるメソッドをオーバーライドします。 |
| 112 | + |
| 113 | +## ガードレール |
| 114 | + |
| 115 | +ガードレール を使用すると、エージェント の実行と並行してユーザー入力に対するチェックや検証を実行できます。例えば、ユーザー入力の関連性を検証できます。詳細は [ガードレール](guardrails.md) のドキュメントを参照してください。 |
| 116 | + |
| 117 | +## エージェント の複製(クローン) |
| 118 | + |
| 119 | +エージェント の `clone()` メソッドを使用すると、エージェント を複製し、必要に応じてプロパティを変更できます。 |
| 120 | + |
| 121 | +```python |
| 122 | +pirate_agent = Agent( |
| 123 | + name="Pirate", |
| 124 | + instructions="Write like a pirate", |
| 125 | + model="o3-mini", |
| 126 | +) |
| 127 | + |
| 128 | +robot_agent = pirate_agent.clone( |
| 129 | + name="Robot", |
| 130 | + instructions="Write like a robot", |
| 131 | +) |
| 132 | +``` |
| 133 | + |
| 134 | +## ツール使用の強制 |
| 135 | + |
| 136 | +ツールのリストを提供しても、必ずしも LLM がツールを使用するとは限りません。ツールの使用を強制するには、 [`ModelSettings.tool_choice`][agents.model_settings.ModelSettings.tool_choice] を設定します。有効な値は以下の通りです。 |
| 137 | + |
| 138 | +1. `auto` : LLM がツールを使用するかどうかを決定します。 |
| 139 | +2. `required` : LLM にツールの使用を強制します(ただし、どのツールを使用するかは LLM が判断します)。 |
| 140 | +3. `none` : LLM にツールを使用しないことを強制します。 |
| 141 | +4. 特定の文字列(例: `my_tool` )を設定すると、LLM はその特定のツールを使用することを強制されます。 |
| 142 | + |
| 143 | +!!! note |
| 144 | + |
| 145 | + 無限ループを防ぐため、フレームワークはツール呼び出し後に自動的に `tool_choice` を "auto" にリセットします。この動作は [`agent.reset_tool_choice`][agents.agent.Agent.reset_tool_choice] で設定可能です。無限ループは、ツールの実行結果が LLM に送信され、再びツール呼び出しが生成されるために発生します。 |
| 146 | + |
| 147 | + ツール呼び出し後に完全に停止させたい場合(自動モードで続行しない場合)は、 [`Agent.tool_use_behavior="stop_on_first_tool"`] を設定すると、ツールの出力を最終レスポンスとして直接使用し、それ以上の LLM 処理を行いません。 |
0 commit comments