forked from deathblade287/DietCalc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
77 lines (62 loc) · 3.14 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const OpenAI = require("openai");
const { Configuration, OpenAIApi } = OpenAI;
const express = require('express');
const cors = require('cors');
const configuration = new Configuration({
organization: "org-j7SGVrKIxMo7ymkkfrBxuEgF",
apiKey: "<api-key>", // Dont be smart, I have deleted the API key
});
const openai = new OpenAIApi(configuration);
const PORT = 3001;
const app = express();
app.use(cors());
async function dietPlan(userInfo, calories) {
console.log('Request 2: Making Diet Plan...')
const response = await openai.createCompletion({
model: "text-davinci-003",
prompt: `
Create a meal plan for a ${userInfo.age} year old ${userInfo.gender}. The daily macro split is 25% carbs, 35% protein, 40% fats with no more than 50g of sugar and 28g of fibre. ${calories.Breakfast} calorie post workout breakfast with healthy fats and b vitamins. ${calories.Lunch} calorie lunch with moderate fats and vitamin D. ${calories.Dinner} calorie dinner with high carbs, low fat and magnesium. Consider foods that are low glycemic but don't avoid fruit, vegetables, and fish. Only use lean meat. Your answer should include all 3 meals (breakfast, lunch, dinner) with headings of the same and then the food in bulleted format. All these headings should include the number calories (given above) for that meal in the brackets and a colon in the end.\n`,
max_tokens: 256,
temperature: 0.7,
});
return response.data.choices[0].text;
}
app.get('/', async (req, res) => {
console.log(req.query.object);
const userInfo = JSON.parse(req.query.object);
console.log("--\n", userInfo, "\n--")
const response = await openai.createCompletion({
model: "code-davinci-002",
prompt: `
The user will provide you with certain parameters regarding his body and health and you need to reply with how many calories he should eat per day in breakfast, lunch and dinner. Your answer should be formated exactly like the example (json). Don't repeat any text and reply only once with the json.
User => I am 15 yeards old. My height is 150cm. I am a male. My activity level is around 2 hours a day. My weight is 60kg.
You =>
{
"Breakfast": "600-700",
"Lunch": "800-900",
"Dinner": "800-900"
}
User => I am 30 years old. My height is 170cm. I am a female. My activity level is around 1 hours a day. My weight is 70kg.
You =>
{
"Breakfast": "600-700",
"Lunch": "700-800",
"Dinner": "700-800"
}
User => I am ${userInfo.age} years old.My height is ${userInfo.height}cm. I am a ${userInfo.gender}. My activity level is around ${userInfo.activityLevel} hours a day. My weight is ${userInfo.weight}kg.
You =>`,
max_tokens: 200,
temperature: 0.1,
stop: "###",
});
const calories = response.data.choices[0].text;
console.log(calories);
Promise.resolve(dietPlan(userInfo, calories)).then((ans) => {
res.json({
"answer": String(ans)
});
})
})
app.listen(PORT, () => {
console.log(`Server Running On Port ${PORT}`);
})