OpenAI Agents SDK は、軽量かつ強力な Python マルチエージェントワークフロー構築フレームワークです。これは OpenAI が公式にリリースしたプロダクションレベルのエージェント開発ツールであり、以前の実験的プロジェクト Swarm のアップグレード版です。このフレームワークは、シンプルでありながら機能が充実しており、複雑なマルチエージェント AI アプリケーションの構築に特化しています。
# Python 仮想環境の作成
python -m venv env
source env/bin/activate
# 基本バージョンのインストール
pip install openai-agents
# 音声サポートを含むバージョンのインストール
pip install 'openai-agents[voice]'
from agents import Agent, Runner
agent = Agent(name="Assistant", instructions="You are a helpful assistant")
result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
print(result.final_output)
from agents import Agent, Runner
import asyncio
spanish_agent = Agent(
name="Spanish agent",
instructions="You only speak Spanish.",
)
english_agent = Agent(
name="English agent",
instructions="You only speak English",
)
triage_agent = Agent(
name="Triage agent",
instructions="Handoff to the appropriate agent based on the language of the request.",
handoffs=[spanish_agent, english_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())
import asyncio
from agents import Agent, Runner, function_tool
@function_tool
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny."
agent = Agent(
name="Weather Assistant",
instructions="You are a helpful weather agent.",
tools=[get_weather],
)
async def main():
result = await Runner.run(agent, input="What's the weather in Tokyo?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
output_type
が設定されている場合、LLM が一致するタイプのコンテンツを返すと終了output_type
がない場合、エージェントがツール呼び出しまたはハンドオフのないメッセージを生成すると終了OPENAI_API_KEY
環境変数の設定が必要# uv パッケージマネージャーのインストール
uv --version
# 依存関係のインストール
make sync
# テストの実行
make tests
# 型チェック
make mypy
# コード規約チェック
make lint
OpenAI Agents Python は、複雑な AI コラボレーションをシンプルで使いやすくするプロフェッショナルレベルのマルチエージェント開発フレームワークです。そのコアとなるエージェント、ハンドオフ、保護、および追跡メカニズムを通じて、開発者は強力な AI アプリケーションシステムを迅速に構築できます。単純なシングルエージェントタスクでも、複雑なマルチエージェントコラボレーションシナリオでも、このフレームワークはエレガントなソリューションを提供します。