Merged backend changes made by salvi to crop controller.js
This commit is contained in:
@@ -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,
|
||||
};
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user