Https Openai - Github.io Openai-Agents-Python Quickstart
Https Openai - Github.io Openai-Agents-Python Quickstart
mkdir my_project
cd my_project
python -m venv .venv
source .venv/bin/activate
export OPENAI_API_KEY=sk-...
agent = Agent(
name="Math Tutor",
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.",
)
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",
)
Define your handoffs
On each agent, you can define an inventory of outgoing handoff
options that the agent can choose from to decide how to make
progress on their task.
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]
)
Add a guardrail
You can define custom guardrails to run on the input or output.
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,
)
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.",
)
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)
if __name__ == "__main__":
asyncio.run(main())
Next steps
Learn how to build more complex agentic flows:
Learn about how to configure Agents.
Learn about running agents.
Learn about tools, guardrails and models.