| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import asyncio |
| import python_weather |
| from loguru import logger |
| from pipecat.frames.frames import LLMTextFrame, TTSSpeakFrame |
| from pipecat.processors.frame_processor import FrameDirection |
| from pipecat.services.llm_service import FunctionCallParams |
|
|
| HTTP_REQUEST_TIMEOUT = 10.0 |
|
|
|
|
| async def tool_get_city_weather(params: FunctionCallParams, city_name: str): |
| """Get the current weather of a city. The result includes city name, weather description, |
| temperature, wind speed, wind direction, precipitation, humidity, visibility, and UV index. |
| |
| Args: |
| city_name: The name of the city to get the weather of. For example, "London", "Beijing", "Paris". |
| Other examples are: "Paris, TX, US", "Paris, FR" and "Tokyo, JP". |
| """ |
| message = f"Looking up weather data for {city_name}. Please wait a moment..." |
| |
| await params.llm.push_frame(LLMTextFrame(message), direction=FrameDirection.UPSTREAM) |
| |
| await params.llm.push_frame(TTSSpeakFrame(message)) |
|
|
| |
| |
| async with python_weather.Client(unit=python_weather.METRIC) as client: |
| |
| logger.debug(f"Fetching weather forecast for `{city_name}`") |
| try: |
| weather: python_weather.Forecast = await asyncio.wait_for( |
| client.get(city_name), |
| timeout=HTTP_REQUEST_TIMEOUT, |
| ) |
| except asyncio.TimeoutError: |
| error_msg = f"python_weather API request timed out after {HTTP_REQUEST_TIMEOUT} seconds for `{city_name}`" |
| logger.error(error_msg) |
| await params.result_callback({"error": error_msg}) |
| return |
| except Exception as e: |
| error_msg = f"Error fetching weather forecast for `{city_name}`: {str(e)}" |
| logger.error(error_msg) |
| await params.result_callback({"error": error_msg}) |
| return |
|
|
| results = { |
| "city": city_name, |
| "description": str(weather.description), |
| "temperature": f"{weather.temperature} degrees Celsius", |
| "wind_speed": f"{weather.wind_speed} kilometers per hour", |
| "wind_direction": str(weather.wind_direction.name), |
| "precipitation": f"{weather.precipitation} millimeters", |
| "humidity": f"{weather.humidity} percent", |
| "visibility": f"{weather.visibility} kilometers", |
| "uv_index": str(weather.ultraviolet), |
| } |
| logger.debug(f"Weather results for {city_name}: {results}") |
| await params.result_callback(results) |
|
|