Spaces:
Running
Running
| import { generateAIContent } from '../utils/aiHelper.js'; | |
| import { extractLanguage, getLanguageName } from '../utils/aiOrchestrator.js'; | |
| import dotenv from 'dotenv'; | |
| export const getWaterOptimizations = async (req, res) => { | |
| dotenv.config(); | |
| const { weather, soilMoisture, cropStage, evaporationRate } = req.body; | |
| const lang = extractLanguage(req); | |
| const langName = getLanguageName(lang); | |
| const langInstruction = lang !== 'en' ? `\n\nRespond STRICTLY in ${langName} language. Translate all fields and values.` : ''; | |
| try { | |
| const prompt = ` | |
| You are an expert irrigation scientist. | |
| Based on the following details: | |
| - Weather: ${weather} | |
| - Soil Moisture (%): ${soilMoisture} | |
| - Crop Stage: ${cropStage} | |
| - Evaporation Rate (mm/day): ${evaporationRate} | |
| Calculate: | |
| 1. Recommended water requirement per day (in liters per plant OR mm per acre — choose best based on input). | |
| 2. Expected water savings (%) compared to traditional irrigation. | |
| 3. A short explanation (1–2 lines). | |
| Respond ONLY in this JSON structure: | |
| { | |
| "water_required": "value + units", | |
| "water_saving_percent": number, | |
| "note": "short explanation" | |
| }${langInstruction} | |
| `; | |
| const recommendation = await generateAIContent(prompt.trim()); | |
| const formattedRecommendation = recommendation.replace("```json", "").replace("```", "").trim(); | |
| res.status(200).json({ recommendation:formattedRecommendation }); | |
| } catch (err) { | |
| console.error("Error fetching recommendations: ", err); | |
| res.status(500).json({ error: "Failed to fetch recommendations" }); | |
| } | |
| }; | |