AI and Food

This commit is contained in:
2024-09-15 01:38:51 -04:00
parent 39873ab767
commit f8ad3b2f72
14 changed files with 344 additions and 193 deletions

View File

@@ -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?",
"Whats 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)

View File

@@ -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);
}
}

View File

@@ -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) {

View File

@@ -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) {