Overview
The Chat API powers the interactive weather assistant feature in SkyCast IA. It uses Groq’s LLaMA 3.1 model to provide weather-related advice with a direct, honest, and slightly sarcastic tone.POST /api/chat
Sends a chat message and receives an AI-generated response based on current weather context.Endpoint
Request Body
Array of message objects following the OpenAI chat format
Current weather context data
Google reCAPTCHA v3 token (required for first message only)
Response
AI-generated response text (max 25 words, 2 emojis max)
Example
reCAPTCHA Verification
The endpoint implements Google reCAPTCHA v3 verification for security:- When to send: Include
captchaTokenonly for the first message in a conversation - Validation: Token is verified against Google’s API using
RECAPTCHA_SECRET_KEY - Failure handling: Returns 403 status with error message if verification fails
- Subsequent messages: No token required after first successful verification
AI Personality & Behavior
The chat endpoint configures the AI with specific guidelines: Tone: Direct, honest, and slightly sarcasticResponse Length: Maximum 25 words
Emoji Limit: Maximum 2 per response
Temperature: 0.7 (balanced creativity and consistency)
Max Tokens: 150 Key Rules:
- Uses technical data (pressure, probability of precipitation) for real advice
- Doesn’t sugarcoat bad weather conditions
- Uses
feels_liketemperature for clothing recommendations - Provides practical, actionable suggestions
Status Codes
| Code | Meaning |
|---|---|
| 200 | Success - AI response generated |
| 403 | Forbidden - reCAPTCHA verification failed |
| 500 | Server Error - Missing API keys or connection error |
| 503 | Service Unavailable - Groq API unavailable |
getAiWeatherAnalysis
Server action that generates automatic weather analysis for the main weather card.Parameters
OpenWeatherMap API response object
Returns
Brief AI-generated analysis (max 20 words, 1 emoji)
Example
Configuration
Model:llama-3.1-8b-instant (Groq)Temperature: 0.3 (low for consistency)
Max Response: 20 words
Endpoint:
https://api.groq.com/openai/v1/chat/completions
System Prompt
Error Handling
Returns fallback messages instead of throwing errors:| Scenario | Return Value |
|---|---|
| Missing API key | "Falta la API Key. 🔑" |
| API error | "Análisis no disponible. ☁️" |
| Network error | "Análisis no disponible. ☁️" |
| Empty response | "¡Disfrutá el día! ☀️" |
Environment Variables
Required environment variables:API Keys
- GROQ_API_KEY: Get from console.groq.com
- RECAPTCHA_SECRET_KEY: Get from Google reCAPTCHA Admin
Security Notes
- Never expose
RECAPTCHA_SECRET_KEYto the client - Use
NEXT_PUBLIC_prefix only for reCAPTCHA site key - Groq API key must remain server-side only
