1+
2+ from agents import Agent , Runner , OpenAIChatCompletionsModel , RunConfig , function_tool
3+ import json
4+ from dotenv import load_dotenv
5+ import datetime
6+ import os
7+ from openai import AsyncOpenAI
8+ load_dotenv ()
9+ from pydantic import BaseModel
10+ from typing import List
11+ import asyncio
12+
13+ class Output_Gen (BaseModel ):
14+ timings : List [str ]
15+
16+
17+ load_dotenv ()
18+ api_key = os .getenv ('gemini' )
19+ gemini_base_url = "https://generativelanguage.googleapis.com/v1beta/openai/"
20+ gemini_model = "gemini-2.5-flash"
21+
22+ external_client = AsyncOpenAI (api_key = api_key , timeout = 300.0 , base_url = gemini_base_url )
23+
24+ model = OpenAIChatCompletionsModel (model = gemini_model , openai_client = external_client )
25+
26+ config = RunConfig (model = model , tracing_disabled = True , model_provider = external_client )
27+
28+ @function_tool ()
29+ def current_month ():
30+ months = {1 : 'January' , 2 : 'February' , 3 : 'March' , 4 : 'April' ,
31+ 5 : 'May' , 6 : 'June' , 7 : 'July' , 8 : 'August' , 9 : 'September' ,
32+ 10 : 'October' , 11 : 'November' , 12 : 'December' }
33+ return months [datetime .date .today ().month ] # August
34+
35+ @function_tool ()
36+ def get_peak_hours (city : str , month ):
37+ if city .lower () == "lahore" or city .lower () == "lhr" :
38+ lhr_data = json .load (open ('lahore_peak_timings.json' ))
39+ return lhr_data
40+ elif city .lower () == "fsd" or city .lower () == "faisalabad" :
41+ fsd_data = json .load (open ('faisalabad_peak_timings.json' ))
42+ return fsd_data
43+ else :
44+ return False
45+
46+ fsd_agent = Agent (name = "Faisalabad Agent" , instructions = "You are an agent for Faisalabad related queries if"
47+ "a customers asks for peak hours from any time"
48+ "you first check the current month and then you fetch "
49+ "peak hours for that month, if user provides month himself"
50+ "then give him peak hours for that month for faisalad" ,
51+ model = model ,
52+ tools = [current_month , get_peak_hours ],
53+ handoff_description = "For Faisalabad or Fsd related peak hours queries" )
54+
55+
56+
57+ lhr_agent = Agent (name = "Lahore Agent" , instructions = "You are an agent for Lahore related queries if"
58+ "a customers asks for peak hours from any time"
59+ "you first check the current month and then you fetch "
60+ "peak hours for that month"
61+ "if user provides month himself"
62+ "then give him peak hours for that month for lhr" ,
63+ model = model ,
64+ tools = [current_month , get_peak_hours ],
65+ handoff_description = "For Lahore or Lhr related peak hours queries" )
66+
67+ orchestrating_tool = Agent (name = "Orchestrator" , instructions = "You will decide which agent to call for a task" ,
68+ model = model ,
69+ handoffs = [lhr_agent , fsd_agent ])
70+
71+ # query = input("Which City are you from?\n").lower()
72+ # query = f"I am from {query} what are my current peak hours?"
73+
74+ async def ask_user ():
75+ query2 = "I am from lhr what are peak hours for current month?"
76+ query3 = "I am from Faisalabad the peak hours 7 PM to 11 PM are for which months? "
77+ result = await Runner .run (orchestrating_tool , query2 , run_config = config )
78+ return result .final_output
79+
80+ print (asyncio .run (ask_user ()))
0 commit comments