Spaces:
Running
Running
| import dotenv from 'dotenv'; | |
| import { extractLanguage } from '../utils/aiOrchestrator.js'; | |
| import { generateAIContent } from '../utils/aiHelper.js'; | |
| export const getRecommendations = async (req, res) => { | |
| dotenv.config(); | |
| const { climate, soilType, cropType, cropInfo, weatherDetails, cropConditions } = req.body; | |
| const lang = extractLanguage(req); | |
| try { | |
| // Construct a detailed prompt for the API based on the farmer's inputs | |
| const promptText = ` | |
| Please provide farming recommendations based on the following information: | |
| 1. **Climate**: ${climate} | |
| 2. **Soil Type**: ${soilType} | |
| 3. **Crop Type**: ${cropType} | |
| 4. **Information about the Crop**: ${cropInfo} | |
| 5. **Today's Weather**: ${weatherDetails} | |
| 6. **Crop Conditions**: ${cropConditions} | |
| Based on this information, please suggest: | |
| - Suitable farming practices for today. | |
| - Care tips for the specified crop considering the current weather and soil conditions. | |
| - Any precautions to take given today's weather and crop requirements. | |
| Make the recommendations clear and easy to understand for farmers. Thank you! | |
| ${lang !== 'en' ? `\n\nRespond STRICTLY in ${req.langName || 'the user\'s preferred language'}.` : ''} | |
| `; | |
| const recommendation = await generateAIContent(promptText.trim()); | |
| res.status(200).json({ recommendation }); | |
| } catch (err) { | |
| console.error("Error fetching recommendations: ", err); | |
| res.status(500).json({ error: "Failed to fetch recommendations" }); | |
| } | |
| }; | |