Movatterモバイル変換


[0]ホーム

URL:


跳转至

快速开始

项目与虚拟环境创建

你只需执行一次。

mkdirmy_projectcdmy_projectpython-mvenv.venv

虚拟环境激活

每次打开新的终端会话都需要执行。

source.venv/bin/activate

安装 Agents SDK

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

设置 OpenAI API key

如果你还没有,请按照这些说明创建一个 OpenAI API key。

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])

智能体编排运行

让我们检查工作流是否运行,并且分诊智能体能在两个专家型智能体之间正确路由。

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 Dashboard 的 Trace viewer 查看你的运行追踪记录。

后续步骤

了解如何构建更复杂的智能体流程:


[8]ページ先頭

©2009-2025 Movatter.jp