OpenAI Agents SDK のコア・ビルディングブロック – エージェントの機能の説明です。
OpenAI Agents SDK : エージェント
作成 : クラスキャット・セールスインフォメーション
作成日時 : 03/18/2025
* 本記事は openai.github.io/openai-agents-python の以下のページを独自に翻訳した上でまとめ直し、補足説明を加えています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
◆ お問合せ : 下記までお願いします。
- クラスキャット セールス・インフォメーション
- sales-info@classcat.com
- ClassCatJP
OpenAI Agents SDK : エージェント
エージェントはアプリケーションのはコア・ビルディングブロックです。エージェントは指示 (instructions) とツールで構成された、大規模言語モデル (LLM) です、
基本構成
構成するエージェントの最も一般的なプロパティは以下です :
- instructions: 開発者メッセージやシステムプロンプトとも呼ばれます。
- model: どの LLM を使用するか、そして temperature, top_p のようなモデル調整パラメータを設定するオプションの model_settings。
- tools: エージェントがタスクを遂行するために使用できるツール。
from agents import Agent, ModelSettings, function_tool
@function_tool
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny"
agent = Agent(
name="Haiku agent",
instructions="Always respond in haiku form",
model="o3-mini",
tools=[get_weather],
)
コンテキスト
エージェントはコンテキストタイプについて汎用的 (generic) です。コンテキストは依存性注入 (dependency-injection) ツールです : それは作成して Runner.run() に渡すオブジェクトで、すべてのエージェント、ツール、ハンドオフ等に渡され、そしてエージェント実行の依存性と状態の詰め合わせ (grab bag) として機能します。任意の Python オブジェクトをコンテキストとして提供できます。
@dataclass
class UserContext:
uid: str
is_pro_user: bool
async def fetch_purchases() -> list[Purchase]:
return ...
agent = Agent[UserContext](
...,
)
出力タイプ
デフォルトでは、エージェントは plain テキスト (i.e. str) 出力を生成します。エージェントに特定のタイプの出力を生成することを望む場合、output_type パラメータを使用できます。一般的な選択は Pydantic オブジェクトを使用することですが、Pydantic TypeAdapter – dataclasses, lists, TypedDict 等でラップできる任意のタイプをサポートします。
from pydantic import BaseModel
from agents import Agent
class CalendarEvent(BaseModel):
name: str
date: str
participants: list[str]
agent = Agent(
name="Calendar extractor",
instructions="Extract calendar events from text",
output_type=CalendarEvent,
)
Note : When you pass an output_type, that tells the model to use structured outputs instead of regular plain text responses.
ハンドオフ
ハンドオフはエージェントが委任できるサブエージェントです。ハンドオフのリストを提供すると、エージェントは関連する場合にはそれらに委任することを選択できます。これは、単一のタスクについて優れた、モジュール型の専門エージェントを編成することを可能にする強力なパターンです。Read more in the handoffs documentation.
from agents import Agent
booking_agent = Agent(...)
refund_agent = Agent(...)
triage_agent = Agent(
name="Triage agent",
instructions=(
"Help the user with their questions."
"If they ask about booking, handoff to the booking agent."
"If they ask about refunds, handoff to the refund agent."
),
handoffs=[booking_agent, refund_agent],
)
動的な指示 (instructions)
殆どの場合、エージェントを作成するときに指示を提供できます。但し関数を通して動的な指示を提供することもできます。関数はエージェントとコンテキストを受け取り、プロンプトを返す必要があります。通常の関数と async 関数の両方が受け入れられます。
def dynamic_instructions(
context: RunContextWrapper[UserContext], agent: Agent[UserContext]
) -> str:
return f"The user's name is {context.context.name}. Help them with their questions."
agent = Agent[UserContext](
name="Triage agent",
instructions=dynamic_instructions,
)
ライフサイクル・イベント (フック)
エージェントのライフサイクルを観察したい場合があります。例えば、イベントをログ記録したり、特定のイベントが発生するときデータを事前取得したい場合があるかもしれません。hooks プロパティを使用してエージェントのライフサイクルをフックできます。AgentHooks クラスをサブクラス化して、関心のあるメソッドをオーバライドします。
ガードレール
ガードレールは、エージェントの実行と並行して、ユーザ入力のチェック/検証を実行することを可能にします。例えば、関連性のためにユーザの入力をスクリーニングできるでしょう。Read more in the guardrails documentation.
エージェントのクローニング (複製) /コピー
エージェントの clone() メソッドを使用して、エージェントを複製し、オプションで好みのプロパティを変更することができます。
pirate_agent = Agent(
name="Pirate",
instructions="Write like a pirate",
model="o3-mini",
)
robot_agent = pirate_agent.clone(
name="Robot",
instructions="Write like a robot",
)
以上