File size: 2,979 Bytes
60c04c3
 
 
 
 
 
 
 
 
b0cf9c2
60c04c3
b0cf9c2
60c04c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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