Movatterモバイル変換


[0]ホーム

URL:


コンテンツにスキップ

クイックスタート

プロジェクトと仮想環境の作成

これは最初の 1 回のみ必要です。

mkdirmy_projectcdmy_projectpython-mvenv.venv

仮想環境の有効化

新しいターミナル セッションを開始するたびに実行します。

source.venv/bin/activate

Agents SDK のインストール

pipinstallopenai-agents# or `uv add openai-agents`, etc

OpenAI API キーの設定

まだお持ちでない場合は、これらの手順に従って OpenAI API キーを作成してください。

exportOPENAI_API_KEY=sk-...

最初の エージェント の作成

エージェント は instructions、名前、そしてオプションの設定(model_config など)で定義されます。

fromagentsimportAgentagent=Agent(name="Math Tutor",instructions="You provide help with math problems. Explain your reasoning at each step and include examples",)

いくつかの エージェント の追加

追加の エージェント も同様に定義できます。handoff_descriptions は、ハンドオフ のルーティング判断のための追加コンテキストを提供します。

fromagentsimportAgenthistory_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",)

ハンドオフ の定義

各 エージェント で、タスクを進める方法を決めるためにエージェントが選択できる送信側のハンドオフ オプションの一覧を定義できます。

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 つの専門 エージェント 間を正しくルーティングすることを確認しましょう。

fromagentsimportRunnerasyncdefmain():result=awaitRunner.run(triage_agent,"What is the capital of France?")print(result.final_output)

ガードレールの追加

入力または出力に対して実行するカスタム ガードレールを定義できます。

fromagentsimportGuardrailFunctionOutput,Agent,RunnerfrompydanticimportBaseModelclassHomeworkOutput(BaseModel):is_homework:boolreasoning:strguardrail_agent=Agent(name="Guardrail check",instructions="Check if the user is asking about homework.",output_type=HomeworkOutput,)asyncdefhomework_guardrail(ctx,agent,input_data):result=awaitRunner.run(guardrail_agent,input_data,context=ctx.context)final_output=result.final_output_as(HomeworkOutput)returnGuardrailFunctionOutput(output_info=final_output,tripwire_triggered=notfinal_output.is_homework,)

すべてを組み合わせる

ハンドオフ と入力ガードレールを使用して、すべてを組み合わせてワークフロー全体を実行しましょう。

fromagentsimportAgent,InputGuardrail,GuardrailFunctionOutput,Runnerfromagents.exceptionsimportInputGuardrailTripwireTriggeredfrompydanticimportBaseModelimportasyncioclassHomeworkOutput(BaseModel):is_homework:boolreasoning:strguardrail_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.",)asyncdefhomework_guardrail(ctx,agent,input_data):result=awaitRunner.run(guardrail_agent,input_data,context=ctx.context)final_output=result.final_output_as(HomeworkOutput)returnGuardrailFunctionOutput(output_info=final_output,tripwire_triggered=notfinal_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),],)asyncdefmain():# Example 1: History questiontry:result=awaitRunner.run(triage_agent,"who was the first president of the united states?")print(result.final_output)exceptInputGuardrailTripwireTriggeredase:print("Guardrail blocked this input:",e)# Example 2: General/philosophical questiontry:result=awaitRunner.run(triage_agent,"What is the meaning of life?")print(result.final_output)exceptInputGuardrailTripwireTriggeredase:print("Guardrail blocked this input:",e)if__name__=="__main__":asyncio.run(main())

トレースの表示

エージェント の実行中に何が起きたかを確認するには、OpenAI ダッシュボードの Trace viewer に移動して、実行のトレースを表示します。

次のステップ

より複雑な エージェント フローの構築方法を学びます:


[8]ページ先頭

©2009-2025 Movatter.jp