クイックスタート
プロジェクトと仮想環境の作成
これは一度だけ実行すれば十分です。
仮想環境の有効化
新しいターミナル セッションを開始するたびに実行します。
Agents SDK のインストール
OpenAI API キーの設定
お持ちでない場合は、これらの手順に従って OpenAI API キーを作成してください。
最初のエージェントの作成
エージェントは 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 ダッシュボードのトレース ビューアー へ移動して、実行のトレースを確認します。
次のステップ
より複雑なエージェント フローの構築方法を学びましょう: