diff --git a/server/ai.js b/server/ai.js index f11af46..c71364b 100644 --- a/server/ai.js +++ b/server/ai.js @@ -2,28 +2,28 @@ import {Ollama} from 'ollama' const ollama = new Ollama({ host: '172.29.186.121:11434' }) -const response = await ollama.chat({ - model: 'llama3:latest', - messages: [{ role: 'user', content: - `Generate a recipe for blueberry muffins: - Format it in a json object like this and don't send any other text: - { - "name": "Blueberry Muffins", - "description": "A delicious and healthy blueberry muffin recipe", - "nutritional_information": {}, - ingredients: [ - { - "name": "flour", - "quantity": "2 cups" - } - ], - instructions: [ - "Preheat oven to 350 degrees" - ] - } - ` - }], -}); +// const response = await ollama.chat({ +// model: 'llama3:latest', +// messages: [{ role: 'user', content: +// `Generate a recipe for blueberry muffins: + // Format it in a json object like this and don't send any other text: + // { + // "name": "Blueberry Muffins", + // "description": "A delicious and healthy blueberry muffin recipe", + // "nutritional_information": {}, + // ingredients: [ + // { + // "name": "flour", + // "quantity": "2 cups" + // } + // ], + // instructions: [ + // "Preheat oven to 350 degrees" + // ] + // } +// ` +// }], +// }); const questions = [ "Is this for breakfast, lunch, dinner, snack or dessert?", @@ -42,31 +42,27 @@ const questions = [ "Are you looking for budget-friendly options, or are you willing to splurge a bit for a special meal?", "What’s the calorie range you want your meal to stay in? (You can specify yes or no)" ] -const testAnswers =[ - "Lunch", - "Italian", - "Healthy", - "Cheese", - "Beans", - "Moderate cook time", - "Chicken", - "Medium", - "Wine", - "Regular Day", - "Crispy", - "Oven,Microwave,Stove", - "few leftovers", - "Budget-Friendly", - "800-1000" -] +// const testAnswers =[ +// "Lunch", +// "Italian", +// "Healthy", +// "Cheese", +// "Beans", +// "Moderate cook time", +// "Chicken", +// "Medium", +// "Wine", +// "Regular Day", +// "Crispy", +// "Oven,Microwave,Stove", +// "few leftovers", +// "Budget-Friendly", +// "800-1000" +// ] -const restrictions=[ - -] - -class aiClass{ +export default class FDAI { constructor(){ - this.ai = new Ollama({ host: '172.29.186.121:11434' }); + this.ai = new Ollama({ host: process.env.AI_URL }); } async suggestFood(questionAmount, answers, restrictions) { @@ -77,19 +73,52 @@ class aiClass{ qaA += `Q${i}. ${answers[i]}\nA${i}. ${answers[i]}` } - const response = await ollama.chat({ + const response = await ollama.generate({ model: 'llama3:latest', - messages: [{ role: 'user', content: - `Give one food suggestion this question answers. - + format: "json", + prompt: + `Give one food suggestion for these question answers and then generate a recipe. + ${qaA} + Consider the following restrictions: ${restrictions.join(", ")} + Format it in a json object like this example: + { + "name": "Blueberry Muffins", + "description": "A delicious and healthy blueberry muffin recipe", + "cuisineType": "American", + "estimatedExpense": 5, + "mealType": "Breakfast", + "nutritionFacts": { + "calories": "350-400", + "fat": "15-20g", + "carbs": "30-40g", + "protein": "35-40g" + }, + ingredients: [ + { + "name": "flour", + "quantity": "2 cups" + } + ], + instructions: [ + "Preheat oven to 350 degrees" + ] + } + Do not send any other text other than the json object and or add extra text to the json object. ` - }], }); + + this.clearChatHistory(); + return response; } + async clearChatHistory() { + await ollama.generate({ + model: 'llama3:latest', + prompt: "clear all previous responses", + format: "json" + }); + } + } -console.log(response) -console.log(response.message.content) - diff --git a/server/api/recipes.js b/server/api/recipes.js index 6e50d71..b2e1f72 100644 --- a/server/api/recipes.js +++ b/server/api/recipes.js @@ -1,18 +1,24 @@ import APIRoute from "../APIRoute.js"; -export default class UsersAPI extends APIRoute { +import FDAI from "../ai.js"; + +const ai = new FDAI(); + +export default class RecipeAPI extends APIRoute { constructor() { super('/recipes'); - this.addSubRoute('/create', 'post', createRecipe); + this.addSubRoute('/create', 'post', this.createRecipe); + this.addSubRoute('/:id', 'get', this.get); + this.addSubRoute('/:id/rate', 'post', this.rate); } async get(req, res) { - res.send('GET request'); - } + let db = req.app.get('mongo').recipes; - async post(req, res) { - res.send('POST request'); + let result = await db.get(req.params.id); + + res.send(result); } async createRecipe(req, res) { @@ -20,16 +26,37 @@ export default class UsersAPI extends APIRoute { let db = req.app.get('mongo').recipes; - let result = await db.create({ - userID: recipe.userID, - ingredients: recipe.ingredients, - productName: recipe.productName, - nutritionFacts: recipe.nutritionFacts, - rating: recipe.rating, - cuisine: recipe.cuisine, - expense: recipe.expense, - mealType: recipe.mealType - }); + let aiResult = await ai.suggestFood(recipe.currentQuestion, recipe.answers, recipe.restrictions); + let suggestFood = JSON.parse(aiResult.response); + + let result = await db.create(suggestFood); + + res.json(result); } + + async rate(req, res) { + let db = req.app.get('mongo').recipes; + + let recipe = await db.get(req.params.id); + + if (!recipe) { + res.status(404).send('Recipe not found'); + return; + } + + let rating = req.body.rating; + + if (rating < 0 || rating > 5) { + res.status(400).send('Rating must be between 0 and 5'); + return; + } + + recipe.rating = rating; + + let result = await db.update(req.params.id, { rating: rating }); + + res.send(result); + } + } \ No newline at end of file diff --git a/server/storage/Mongo.js b/server/storage/Mongo.js index cea65ce..b222827 100644 --- a/server/storage/Mongo.js +++ b/server/storage/Mongo.js @@ -1,10 +1,12 @@ import mongoose from "mongoose"; import Users from "./collections/users.js"; +import Recipes from "./collections/recipes.js"; export default class Mongo { constructor(uri) { this.connect(uri); this.users = new Users(); + this.recipes = new Recipes(); } async connect(uri) { diff --git a/server/storage/collections/recipes.js b/server/storage/collections/recipes.js index e121e33..570d847 100644 --- a/server/storage/collections/recipes.js +++ b/server/storage/collections/recipes.js @@ -10,18 +10,16 @@ const reqString = { const recipesSchema = new mongoose.Schema({ id: reqString, + description: String, userID: String, ingredients: Array, - productName: String, + instructions: Array, + name: String, nutritionFacts: Object, rating: Number, cuisine: String, expense: Number, mealType: Object - - - - }, { timestamps: true }); export default class Recipes { @@ -39,18 +37,15 @@ export default class Recipes { id: Id, userID: recipe.userID, ingredients: recipe.ingredients, - productName: recipe.productName, + description: recipe.description, + name: recipe.name, nutritionFacts: recipe.nutritionFacts, - rating: recipe.rating, cuisine: recipe.cuisine, expense: recipe.expense, - mealType: recipe.mealType - - - - + mealType: recipe.mealType, + instructions: recipe.instructions }, this.upsert); - return await this.get(user.id); + return await this.get(Id); } async get(Id) { diff --git a/src/lib/components/Navbar.svelte b/src/lib/components/Navbar.svelte index d643c24..58b3f99 100644 --- a/src/lib/components/Navbar.svelte +++ b/src/lib/components/Navbar.svelte @@ -1,3 +1,7 @@ + + \ No newline at end of file diff --git a/src/lib/components/form/QuestionSelect.svelte b/src/lib/components/form/QuestionSelect.svelte index a44d026..aab2f92 100644 --- a/src/lib/components/form/QuestionSelect.svelte +++ b/src/lib/components/form/QuestionSelect.svelte @@ -3,11 +3,7 @@ import Survey from "./Survey.svelte"; export let parentDiv; - console.log(parentDiv); - function open5Survey(questions) { - console.log(questions); - parentDiv.innerHTML = ""; new Survey({ target: parentDiv, @@ -18,8 +14,6 @@ } function open10Survey(questions) { - console.log(questions); - parentDiv.innerHTML = ""; new Survey({ target: parentDiv, @@ -30,8 +24,6 @@ } function open15Survey(questions) { - console.log(questions); - parentDiv.innerHTML = ""; new Survey({ target: parentDiv, diff --git a/src/lib/components/form/Survey.svelte b/src/lib/components/form/Survey.svelte index 3ab38d0..8972fd9 100644 --- a/src/lib/components/form/Survey.svelte +++ b/src/lib/components/form/Survey.svelte @@ -1,11 +1,15 @@ -
- {questions[$formData.currentQuestion]} + {questions[$formData.currentQuestion-1]}
Butter Chicken
-Butter Chicken is a delicious Indian Meal. It was made by the British after they colonized the country.
-Nullam id dolor id nibh ultricies vehicula ut id elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus.
-Nullam id dolor id nibh ultricies vehicula ut id elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus.
-Nullam id dolor id nibh ultricies vehicula ut id elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus.
-Nullam id dolor id nibh ultricies vehicula ut id elit. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Donec id elit non mi porta gravida at eget metus.
-{foodData.name}
+{foodData.description}
++