import json from datetime import datetime import logging from groq import Groq # Set up logging logger = logging.getLogger(__name__) class WeatherAgent: def __init__(self, groq_api_key): logger.info("Initializing WeatherAgent") self.groq_client = Groq(api_key=groq_api_key) def analyze_weather_data(self, forecast_data): """Use Groq to analyze weather data and provide insights""" if not forecast_data: logger.warning("No forecast data provided for analysis") return "I couldn't access the weather data at the moment." logger.info("Creating weather analysis prompt") # Create a detailed prompt for the weather analysis prompt = f""" As a Burning Man weather expert, analyze this 7-day forecast for Black Rock City and provide insights: Raw Weather Data: {json.dumps(forecast_data, indent=2)} Please provide: 1. A summary of the overall weather pattern 2. Key highlights and potential concerns 3. Specific recommendations for Burners 4. Any notable weather changes or patterns Format your response in a friendly, Burning Man style. """ try: logger.info("Sending weather data to Groq for analysis") response = self.groq_client.chat.completions.create( messages=[ { "role": "system", "content": "You are a weather expert for Burning Man, experienced in desert climate patterns and their implications for festival participants." }, { "role": "user", "content": prompt } ], model="llama-3.1-8b-instant", temperature=0.7, ) logger.info("Successfully received AI analysis") return response.choices[0].message.content except Exception as e: logger.error(f"Error in AI analysis: {str(e)}") return f"I'm having trouble analyzing the weather patterns right now. Basic forecast data is still available: {self.format_basic_forecast(forecast_data)}" def format_basic_forecast(self, forecast): """Fallback formatter for basic weather data""" logger.info("Using basic forecast formatter") if not forecast: logger.warning("No forecast data available for basic formatting") return "Weather data unavailable at the moment." report = "Here's the basic weather forecast for Black Rock City:\n\n" for day in forecast: report += f"📅 {day['date']}: High {day['high']}°F, Low {day['low']}°F\n" report += f" Conditions: {day['conditions']}, Wind: {day['wind_speed']} mph\n\n" return report