Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save sarthaksavvy/9db6b2cc8f34ea9db6dbcb1e603a95c5 to your computer and use it in GitHub Desktop.

Select an option

Save sarthaksavvy/9db6b2cc8f34ea9db6dbcb1e603a95c5 to your computer and use it in GitHub Desktop.
02-aug-2025 youtube live
import asyncio
from agents import Agent, Runner, function_tool
@function_tool
def weatherSearch(city: str) -> str:
"""
Search the web for the weather of given city using OpenWeatherMap API.
Args:
city: The city to search for.
Returns:
The weather information for the city.
"""
# Get API key from environment variable
api_key = "api key comes here"
if not api_key:
return "Error: OpenWeatherMap API key not found. Please set the OPENWEATHER_API_KEY environment variable."
# OpenWeatherMap API endpoint for current weather
base_url = "https://api.openweathermap.org/data/2.5/weather"
# Parameters for the API request
params = {
'q': city,
'appid': api_key,
'units': 'metric' # Use metric units (Celsius, meters/sec, etc.)
}
try:
# Make synchronous request (you can make this async later if needed)
import requests
response = requests.get(base_url, params=params)
response.raise_for_status()
data = response.json()
# Extract weather information
weather_description = data['weather'][0]['description']
temperature = data['main']['temp']
humidity = data['main']['humidity']
wind_speed = data['wind']['speed']
city_name = data['name']
country = data['sys']['country']
# Format the response
weather_info = f"Weather in {city_name}, {country}:\n"
weather_info += f"• Condition: {weather_description.capitalize()}\n"
weather_info += f"• Temperature: {temperature}°C\n"
weather_info += f"• Humidity: {humidity}%\n"
weather_info += f"• Wind Speed: {wind_speed} m/s"
return weather_info
except requests.exceptions.RequestException as e:
return f"Error fetching weather data: {str(e)}"
except KeyError as e:
return f"Error parsing weather data: {str(e)}"
except Exception as e:
return f"Unexpected error: {str(e)}"
my_firsT_agent = Agent(
name="my_first_agent",
instructions="You are a helpful assistant that can give weather information of a city and nothing else.",
model="gpt-4.1-mini",
tools=[weatherSearch],
)
async def main():
result = await Runner.run(my_firsT_agent, "What is weather of Delhi?")
print(result.final_output)
if __name__ == "__main__":
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment