From 1966a696ab1c193bae45315b18c783124916f265 Mon Sep 17 00:00:00 2001 From: Kshitij Date: Sun, 23 Feb 2025 09:14:01 +0530 Subject: [PATCH] Merged backend changes made by salvi to crop controller.js --- Backend/Controllers/crop.controller.js | 60 ++++++++++++++++ Backend/Routes/crop.routes.js | 9 +++ Backend/Utils/model.js | 99 ++++++++++++++++++++++++++ 3 files changed, 168 insertions(+) diff --git a/Backend/Controllers/crop.controller.js b/Backend/Controllers/crop.controller.js index 326f16e..e2e8b80 100644 --- a/Backend/Controllers/crop.controller.js +++ b/Backend/Controllers/crop.controller.js @@ -1,6 +1,7 @@ const Crop = require("../Models/crop.model.js"); const Farm = require("../Models/farm.model.js"); const { uploadOnCloudinary } = require("../Utils/cloudinary.js"); +const { run } = require("../Utils/model.js"); // Create a new crop const createCrop = async (req, res) => { @@ -145,6 +146,61 @@ const updateHealthStatus = async (req, res) => { } }; +const cropHarvest = async (req, res) => { + try { + const crop = await Crop.findById(req.params.cropId).populate("farm"); + console.log(crop); + + const message = `My crop is ${crop.name}, given that I have planted it on ${crop.plantedDate}, suggest me a date as to when it will be harvested. Give output in the form: ${crop.name} will take .. months to harvest around (give month name and year).`; // for harvest expectation + + const result = await run(message); + res.status(200).json({ message: result }); + } catch (error) { + res.status(500).json({ message: error.message }); + } +}; + +const suggestNextCrop = async (req, res) => { + try { + const crop = await Crop.findById(req.params.cropId).populate("farm"); + console.log(crop); + + const message = `Currently I have ${crop.name} in my field. Considering the best optimal time for its harvestation give my location, ${crop.farm.location} and water content of my field is ${crop.farm.waterContent}. Suggest next crop I should grow and give more suggestions around it. Don't tell me when to harvest ${crop.name}, only tell me next best crop to plant in my field and give suggestions around it.`; // for next sowing suggestion + + const result = await run(message); + res.status(200).json({ message: result }); + } catch (error) { + res.status(500).json({ message: error.message }); + } +}; + +const suggestPesticides = async (req, res) => { + try { + const crop = await Crop.findById(req.params.cropId).populate("farm"); + console.log(crop); + + const message = `Considering I have grown ${crop.name} in my field located in ${crop.farm.location} and has water content of ${crop.farm.waterContent}. Suggest pesticides I should use on the crop and when to use it considering my sow date is ${crop.sowDate}. Give precautionary measures and suggestions around it.`; // for pesticides + + const result = await run(message); + res.status(200).json({ message: result }); + } catch (error) { + res.status(500).json({ message: error.message }); + } +}; + +const suggestFertilizers = async (req, res) => { + try { + const crop = await Crop.findById(req.params.cropId).populate("farm"); + console.log(crop); + + const message = `Considering I have grown ${crop.name} in my field located in ${crop.farm.location} and has water content of ${crop.farm.waterContent}. Suggest fertilizers I should use on the crop and when to use it, i.e. after how many months after sowing considering sowing date is ${crop.sowDate} considering my sow date is ${crop.sowDate}. Give me precautionary measures.`; // for fertilizers + + const result = await run(message); + res.status(200).json({ message: result }); + } catch (error) { + res.status(500).json({ message: error.message }); + } +}; module.exports = { createCrop, @@ -154,4 +210,8 @@ module.exports = { deleteCrop, updateGrowthStage, updateHealthStatus, + cropHarvest, + suggestNextCrop, + suggestPesticides, + suggestFertilizers, }; diff --git a/Backend/Routes/crop.routes.js b/Backend/Routes/crop.routes.js index 8a43234..e176f36 100644 --- a/Backend/Routes/crop.routes.js +++ b/Backend/Routes/crop.routes.js @@ -7,6 +7,10 @@ const { deleteCrop, updateHealthStatus, updateGrowthStage, + cropHarvest, + suggestNextCrop, + suggestPesticides, + suggestFertilizers, } = require("../Controllers/crop.controller.js"); const { checkAuthenticated } = require("../Middlewares/authentication.js"); const upload = require("../Middlewares/multer.js"); @@ -22,4 +26,9 @@ router.delete("/:cropId", checkAuthenticated, deleteCrop); // Delete a crop router.put("/health/:cropId", checkAuthenticated, updateHealthStatus); router.put("/growth/:cropId", checkAuthenticated, updateGrowthStage); +router.get("/harvest/:cropId", checkAuthenticated, cropHarvest); +router.get("/nextCrop/:cropId", checkAuthenticated, suggestNextCrop); +router.get("/pesticides/:cropId", checkAuthenticated, suggestPesticides); +router.get("/fertilizers/:cropId", checkAuthenticated, suggestFertilizers); + module.exports = router; diff --git a/Backend/Utils/model.js b/Backend/Utils/model.js index 45c9ef4..2d48466 100644 --- a/Backend/Utils/model.js +++ b/Backend/Utils/model.js @@ -272,6 +272,88 @@ // console.log(result.response.text()); // } +// const { +// GoogleGenerativeAI, +// HarmCategory, +// HarmBlockThreshold, +// } = require("@google/generative-ai"); + +// const apiKey = "AIzaSyDsXug23r4umgcJpj77KeqNyYW0hQnYDgg"; +// const genAI = new GoogleGenerativeAI(apiKey); + +// const model = genAI.getGenerativeModel({ +// model: "gemini-2.0-flash", +// }); + +// const generationConfig = { +// temperature: 1, +// topP: 0.95, +// topK: 40, +// maxOutputTokens: 8192, +// responseMimeType: "text/plain", +// }; + +// async function run(message) { +// const chatSession = model.startChat({ +// generationConfig, +// history: [ +// { +// role: "user", +// parts: [ +// { +// text: "AI Guidelines:\n- The AI must only provide answers related to farming, including crops, sowing, irrigation, harvesting, fertilizers, pesticides, soil health, and climate conditions. \n- The AI must not answer anything outside farming. \n- If asked an unrelated question, the AI must not respond. \n- If the query is unclear, the AI must default to farming-related guidance. \n- The AI must provide only one-line responses without extra details. \n- No harmful, hurtful, or controversial responses. \n- No advice on illegal, unsafe, or unethical farming practices.\n", +// }, +// ], +// }, +// { +// role: "model", +// parts: [ +// { +// text: "Understood. I will only provide one-line responses directly related to farming.\n", +// }, +// ], +// }, +// { +// role: "user", +// parts: [ +// { +// text: "you can also receive json or javascript object as an input ", +// }, +// ], +// }, +// { +// role: "model", +// parts: [ +// { +// text: "Understood. I will process the JSON or JavaScript object only if it pertains to farming and respond with a single line.\n", +// }, +// ], +// }, +// { +// role: "user", +// parts: [ +// { +// text: "Answer everything which falls under the farming and agriculture even if the prompt does not include any farm or agriculture word in it", +// }, +// ], +// }, +// { +// role: "model", +// parts: [ +// { +// text: "Understood. I will assume all queries are related to farming and provide a one-line farming-related response.\n", +// }, +// ], +// }, +// ], +// }); + +// const result = await chatSession.sendMessage(message); +// console.log(result.response.text()); + +// return result.response.text(); +// } + const { GoogleGenerativeAI, HarmCategory, @@ -345,11 +427,28 @@ async function run(message) { }, ], }, + { + role: "user", + parts: [ + { + text: "Give me a percise answer no matter what, dont give useless or incomplete answer \n", + }, + ], + }, + { + role: "model", + parts: [ + { + text: "Understood. I will strive to provide precise and complete one-line farming-related answers.\n", + }, + ], + }, ], }); const result = await chatSession.sendMessage(message); console.log(result.response.text()); + return result.response.text(); } module.exports = { run };