Last active
March 12, 2025 03:11
-
-
Save MAnfal/b24e812783e646ac065a130f3487ccaf to your computer and use it in GitHub Desktop.
OpenAI's agent SDK experiment
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import dotenv | |
import time | |
from agents import Agent, InputGuardrail,GuardrailFunctionOutput, Runner | |
from pydantic import BaseModel | |
import asyncio | |
dotenv.load_dotenv() | |
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 cleopatra?") | |
print(result.final_output) | |
if __name__ == "__main__": | |
start_time = time.time() | |
asyncio.run(main()) | |
end_time = time.time() | |
print(f"Time taken: {end_time - start_time:.2f} seconds") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment