|
| 1 | +import asyncio |
| 2 | +import json |
| 3 | +from typing import AsyncIterator |
| 4 | + |
| 5 | +import httpx |
| 6 | +from agno.agent import Agent |
| 7 | +from agno.tools import tool |
| 8 | + |
| 9 | + |
| 10 | +class DemoTools: |
| 11 | + @tool(description="Get the top hackernews stories") |
| 12 | + @staticmethod |
| 13 | + async def get_top_hackernews_stories(agent: Agent) -> str: |
| 14 | + num_stories = agent.context.get("num_stories", 5) if agent.context else 5 |
| 15 | + |
| 16 | + # Fetch top story IDs |
| 17 | + response = httpx.get("https://hacker-news.firebaseio.com/v0/topstories.json") |
| 18 | + story_ids = response.json() |
| 19 | + |
| 20 | + # Get story details |
| 21 | + for story_id in story_ids[:num_stories]: |
| 22 | + async with httpx.AsyncClient() as client: |
| 23 | + story_response = await client.get( |
| 24 | + f"https://hacker-news.firebaseio.com/v0/item/{story_id}.json" |
| 25 | + ) |
| 26 | + story = story_response.json() |
| 27 | + if "text" in story: |
| 28 | + story.pop("text", None) |
| 29 | + return json.dumps(story) |
| 30 | + |
| 31 | + @tool( |
| 32 | + description="Get the current weather for a city using the MetaWeather public API" |
| 33 | + ) |
| 34 | + async def get_current_weather(agent: Agent) -> str: |
| 35 | + city = ( |
| 36 | + agent.context.get("city", "San Francisco") |
| 37 | + if agent.context |
| 38 | + else "San Francisco" |
| 39 | + ) |
| 40 | + |
| 41 | + async with httpx.AsyncClient() as client: |
| 42 | + # Geocode city to get latitude and longitude |
| 43 | + geo_resp = await client.get( |
| 44 | + "https://geocoding-api.open-meteo.com/v1/search", |
| 45 | + params={"name": city, "count": 1, "language": "en", "format": "json"}, |
| 46 | + ) |
| 47 | + geo_data = geo_resp.json() |
| 48 | + if not geo_data.get("results"): |
| 49 | + return json.dumps({"error": f"City '{city}' not found."}) |
| 50 | + location = geo_data["results"][0] |
| 51 | + lat, lon = location["latitude"], location["longitude"] |
| 52 | + |
| 53 | + # Get current weather |
| 54 | + weather_resp = await client.get( |
| 55 | + "https://api.open-meteo.com/v1/forecast", |
| 56 | + params={ |
| 57 | + "latitude": lat, |
| 58 | + "longitude": lon, |
| 59 | + "current_weather": True, |
| 60 | + "timezone": "auto", |
| 61 | + }, |
| 62 | + ) |
| 63 | + weather_data = weather_resp.json() |
| 64 | + current_weather = weather_data.get("current_weather") |
| 65 | + if not current_weather: |
| 66 | + return json.dumps({"error": f"No weather data found for '{city}'."}) |
| 67 | + |
| 68 | + result = { |
| 69 | + "city": city, |
| 70 | + "weather_state": f"{current_weather['weathercode']}", # Open-Meteo uses weather codes |
| 71 | + "temp_celsius": current_weather["temperature"], |
| 72 | + "humidity": None, # Open-Meteo current_weather does not provide humidity |
| 73 | + "date": current_weather["time"], |
| 74 | + } |
| 75 | + return json.dumps(result) |
| 76 | + |
| 77 | + |
| 78 | +agent = Agent( |
| 79 | + name="HackerNewsAgent", |
| 80 | + context={ |
| 81 | + "num_stories": 2, |
| 82 | + }, |
| 83 | + tools=[DemoTools.get_top_hackernews_stories], |
| 84 | +) |
| 85 | +asyncio.run(agent.aprint_response("What are the top hackernews stories?")) |
| 86 | + |
| 87 | + |
| 88 | +agent = Agent( |
| 89 | + name="WeatherAgent", |
| 90 | + context={ |
| 91 | + "city": "San Francisco", |
| 92 | + }, |
| 93 | + tools=[DemoTools().get_current_weather], |
| 94 | +) |
| 95 | +asyncio.run(agent.aprint_response("What is the weather like?")) |
0 commit comments