# クイックスタート ## プロジェクトと仮想環境の作成 これは一度だけ行えば大丈夫です。 ```bash mkdir my_project cd my_project python -m venv .venv ``` ### 仮想環境をアクティブにする 新しいターミナルセッションを開始するたびに行ってください。 ```bash source .venv/bin/activate ``` ### Agents SDK のインストール ```bash pip install openai-agents # or `uv add openai-agents`, etc ``` ### OpenAI API キーの設定 お持ちでない場合は、[こちらの手順](https://platform.openai.com/docs/quickstart#create-and-export-an-api-key)に従って OpenAI API キーを作成してください。 ```bash export OPENAI_API_KEY=sk-... ``` ## 最初のエージェントを作成する エージェントは、instructions、名前、オプションの設定(`model_config` など)で定義されます。 ```python from agents import Agent agent = Agent( name="Math Tutor", instructions="You provide help with math problems. Explain your reasoning at each step and include examples", ) ``` ## さらにエージェントを追加する 追加のエージェントも同様に定義できます。`handoff_descriptions` は、ハンドオフルーティングを決定するための追加のコンテキストを提供します。 ```python from agents import Agent history_tutor_agent = Agent( name="History Tutor", handoff_description="Specialist agent for historical questions", instructions="You provide assistance with historical queries. Explain important events and context clearly.", ) math_tutor_agent = Agent( name="Math Tutor", handoff_description="Specialist agent for math questions", instructions="You provide help with math problems. Explain your reasoning at each step and include examples", ) ``` ## ハンドオフを定義する 各エージェントで、タスクを進める方法を決定するための送信ハンドオフオプションのインベントリを定義できます。 ```python triage_agent = Agent( name="Triage Agent", instructions="You determine which agent to use based on the user's homework question", handoffs=[history_tutor_agent, math_tutor_agent] ) ``` ## エージェントオーケストレーションを実行する ワークフローが実行され、トリアージエージェントが2つの専門エージェント間を正しくルーティングするか確認しましょう。 ```python from agents import Runner async def main(): result = await Runner.run(triage_agent, "What is the capital of France?") print(result.final_output) ``` ## ガードレールを追加する 入力または出力に対してカスタムガードレールを定義できます。 ```python from agents import GuardrailFunctionOutput, Agent, Runner from pydantic import BaseModel class HomeworkOutput(BaseModel): is_homework: bool reasoning: str guardrail_agent = Agent( name="Guardrail check", instructions="Check if the user is asking about homework.", output_type=HomeworkOutput, ) async def homework_guardrail(ctx, agent, input_data): result = await Runner.run(guardrail_agent, input_data, context=ctx.context) final_output = result.final_output_as(HomeworkOutput) return GuardrailFunctionOutput( output_info=final_output, tripwire_triggered=not final_output.is_homework, ) ``` ## すべてをまとめる すべてをまとめて、ハンドオフと入力ガードレールを使用して全体のワークフローを実行しましょう。 ```python from agents import Agent, InputGuardrail, GuardrailFunctionOutput, Runner from pydantic import BaseModel import asyncio class HomeworkOutput(BaseModel): is_homework: bool reasoning: str guardrail_agent = Agent( name="Guardrail check", instructions="Check if the user is asking about homework.", output_type=HomeworkOutput, ) math_tutor_agent = Agent( name="Math Tutor", handoff_description="Specialist agent for math questions", instructions="You provide help with math problems. Explain your reasoning at each step and include examples", ) history_tutor_agent = Agent( name="History Tutor", handoff_description="Specialist agent for historical questions", instructions="You provide assistance with historical queries. Explain important events and context clearly.", ) async def homework_guardrail(ctx, agent, input_data): result = await Runner.run(guardrail_agent, input_data, context=ctx.context) final_output = result.final_output_as(HomeworkOutput) return GuardrailFunctionOutput( output_info=final_output, tripwire_triggered=not final_output.is_homework, ) triage_agent = Agent( name="Triage Agent", instructions="You determine which agent to use based on the user's homework question", handoffs=[history_tutor_agent, math_tutor_agent], input_guardrails=[ InputGuardrail(guardrail_function=homework_guardrail), ], ) async def main(): result = await Runner.run(triage_agent, "who was the first president of the united states?") print(result.final_output) result = await Runner.run(triage_agent, "what is life") print(result.final_output) if __name__ == "__main__": asyncio.run(main()) ``` ## トレースを表示する エージェントの実行中に何が起こったかを確認するには、[OpenAI ダッシュボードのトレースビューア](https://platform.openai.com/traces)に移動して、エージェント実行のトレースを表示してください。 ## 次のステップ より複雑なエージェントフローの構築方法を学びましょう: - [エージェント](agents.md)の設定方法を学ぶ。 - [エージェントの実行](running_agents.md)について学ぶ。 - [ツール](tools.md)、[ガードレール](guardrails.md)、[モデル](models.md)について学ぶ。