PK!A7��nconst { Configuration, OpenAIApi } = require("openai"); const log = require("../helper/log"); const serviceController = {}; serviceController.getResult = async (req, res) => { try { const { service, subService, gender, mealType, goal, healthType, nutritionFoodName, age, contentTopic, contentForCorrection, noOfWords } = req.body; log.info(`----------------------------------------------------------------------------`); log.info(`[Service Controller], [getResult], Service [${service}], SubService [${subService}], Gender [${gender}], Meal Type [${mealType}], Goal [${goal}], Health Type [${healthType}], Food Name [${nutritionFoodName}], Age [${age}], content Topic [${contentTopic}], No. Of Words [${noOfWords}] content For Correction [${contentForCorrection}]`); if (!service || !subService) { return res.status(400).json({ msg: "Missing Parameters" }); } let prompt = ""; if (service === "health" && subService === "meal_plan") { prompt = `Create a ${mealType} meal plan for ${gender}`; } else if (service === "health" && subService === "exercise") { prompt = `Recommend me some exercise for ${goal}`; } else if (service === "health" && subService === "health_advice") { prompt = `Give health advice for ${healthType}`; } else if (service === "health" && subService === "nutrition") { prompt = `Share Nutrition Information of ${nutritionFoodName}`; } else if (service === "health" && subService === "sleep") { prompt = `Suggest some sleep improvement tips for ${age} years old`; } else if (service === "health" && subService === "mental") { prompt = `How to improve mental health having age around ${age}`; } else if (service === "content" && subService === "content_creation") { prompt = `Create content on the topic of ${contentTopic} in ${noOfWords} words`; } else if (service === "content" && subService === "content_correction") { prompt = `Correct this text: ${contentForCorrection}`; } log.info(`[Service Controller], [getResult], Prompt [${prompt}]`); const configuration = new Configuration({ apiKey: process.env.OPENAI_API_KEY, }); const openai = new OpenAIApi(configuration); if (prompt) { try { const completion = await openai.createCompletion({ model: "text-davinci-003", prompt, max_tokens: 200, }); const result = JSON.stringify(completion.data.choices[0].text); log.info(`[Service Controller], [getResult], Result [${result}]`); res.status(200).json({ result }); } catch (err) { throw err.response; } } else { res.status(200).json({ result: "Couldn't get result" }); log.info(`[Service Controller], [getResult], [Couldn't get result]`); } } catch (err) { log.info(`[Service Controller], [getResult], Error [${err}]`); res.status(400).json({ msg: err }); } }; module.exports = serviceController;