Ritesh-hf commited on
Commit
ecc33a7
·
verified ·
1 Parent(s): f3af235

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +191 -16
app.py CHANGED
@@ -42,6 +42,9 @@ os.environ["TOKENIZERS_PARALLELISM"] = 'true'
42
  # Initialize LLM
43
  llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2)
44
 
 
 
 
45
  # Initialize Router
46
  router = ChatGroq(model="llama-3.2-3b-preview", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
47
 
@@ -180,22 +183,191 @@ def generate_session_key():
180
  return session_key
181
 
182
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
183
  def answer_generator(formated_input, session_id):
184
  # QA system prompt and chain
185
  qa_system_prompt = """
186
  You are an AI assistant developed by Nutrigenics AI, specializing in intelligent recipe information retrieval and recipe suggestions. Your purpose is to help users by recommending recipes, providing detailed nutritional values, listing ingredients, offering step-by-step cooking instructions, and filtering recipes based on context and user queries.
187
- Operational Guidelines:
188
- 1. Input Structure:
189
- - Context: You may receive contextual information related to recipes, such as specific data sets, user preferences, dietary restrictions, or previously selected dishes.
190
- - User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient substitutions, cooking instructions, and more.
191
- 2. Response Strategy:
192
- - Utilize Provided Context: If the context contains relevant information that addresses the user's query, base your response on this provided data to ensure accuracy and relevance.
193
- - Respond to User Query Directly: If the context does not contain the necessary information to answer the user's query, kindly state that you do not have the required information.
194
- Core Functionalities:
195
- - Nutritional Information: Accurately provide nutritional values for each recipe, including calories, macronutrients (proteins, fats, carbohydrates), and essential vitamins and minerals, using contextual data when available.
196
- - Ingredient Details: List all ingredients required for recipes, including substitute options for dietary restrictions or ingredient availability, utilizing context when relevant.
197
- - Step-by-Step Cooking Instructions: Deliver clear, easy-to-follow instructions for preparing and cooking meals, informed by any provided contextual data.
198
- - Recipe Recommendations: Suggest dishes based on user preferences, dietary restrictions, available ingredients, and contextual data if provided.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
199
  Additional Instructions:
200
  - Precision and Personalization: Always aim to provide precise, personalized, and relevant information to users based on both the provided context and their specific queries.
201
  - Clarity and Coherence: Ensure all responses are clear, well-structured, and easy to understand, facilitating a seamless user experience.
@@ -326,6 +498,7 @@ def recommendation_node(query):
326
  Recipe filtering instructions:
327
  - If a user asked for the highest nutrient recipe such as "high protein or high calories" then filtered recipes should be the top highest recipes from all the recipes with high nutrients.
328
  - sort or rearrange recipes based on which recipes are more appropriate for the user.
 
329
 
330
  Your output instructions:
331
  - The function name should be filter_recipes. The input to the function should be the file name.
@@ -410,25 +583,27 @@ def get_answer(image=[], message='', sessionID='abc123'):
410
  'input': message,
411
  'context': data
412
  }
413
- response = answer_generator(formated_input, session_id=sessionID)
 
414
  except Exception as e:
415
  print(e)
416
  response = {'content':"An error occurred while processing your request."}
417
  elif (image is None) and (message is not None):
418
  task = router_node(message)
419
- print(task)
420
  if task == 'recommendation':
421
  recipes = recommendation_node(message)
422
  if not recipes:
423
  response = {'content': "An error occurred while processing your request."}
424
  else:
425
- response = answer_formatter_node(message, recipes)
 
426
  else:
427
  formated_input = {
428
  'input': message,
429
  'context': CURR_CONTEXT
430
  }
431
- response = answer_generator(formated_input, session_id=sessionID)
 
432
  return response
433
 
434
  import json
 
42
  # Initialize LLM
43
  llm = ChatGroq(model="llama-3.1-8b-instant", temperature=0, max_tokens=1024, max_retries=2)
44
 
45
+ # JSON response LLM
46
+ json_llm = ChatGroq(model="llama-3.1-70b-versatile", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
47
+
48
  # Initialize Router
49
  router = ChatGroq(model="llama-3.2-3b-preview", temperature=0, max_tokens=1024, max_retries=2, model_kwargs={"response_format": {"type": "json_object"}})
50
 
 
183
  return session_key
184
 
185
 
186
+ def json_answer_generator(user_query, context):
187
+ system_prompt = """
188
+ Given a recipe context in JSON format, respond to user queries by extracting and returning the requested information in JSON format with an additional `"header"` key containing a response starter. Use the following rules:
189
+
190
+ 1. **Recipe Information Extraction**:
191
+ - If the user query explicitly requests specific recipe data (e.g., ingredients, nutrients, or instructions), return only those JSON objects from the provided recipe context.
192
+ - For example, if the user asks, “What are the ingredients?” or “Show me the nutrient details,” your output should be limited to only the requested JSON objects (e.g., `recipe_ingredients`, `recipe_nutrients`).
193
+ - Include `"header": "Here is the information you requested:"` at the start of each response.
194
+
195
+ 2. **Multiple Information Points**:
196
+ - If a user query asks for more than one piece of information, return each requested JSON object from the recipe context in a combined JSON response.
197
+ - For example, if the query is “Give me the ingredients and instructions,” the output should include both `recipe_ingredients` and `recipe_instructions` objects.
198
+ - Include `"header": "Here is the information you requested:"` at the start of each response.
199
+
200
+ 3. **Non-Specific Recipe Information**:
201
+ - If the query does not directly refer to recipe data but instead asks for a general response based on the context, return a JSON object with a single key `"content"` and a descriptive response as its value.
202
+ - Include `"header": "Here is a suggestion based on the recipe:"` as the response starter.
203
+ - For example, if the query is “How can I use this recipe for a healthy lunch?” return a response like:
204
+ ```json
205
+ {
206
+ "header": "Here is a suggestion based on the recipe:",
207
+ "content": "This Asian Potato Salad with Seven Minute Egg is a nutritious and light option, ideal for a balanced lunch. It provides protein and essential nutrients with low calories."
208
+ }
209
+ ```
210
+
211
+ **Example Context**:
212
+ ```json
213
+ {
214
+ "recipe_name": "Asian Potato Salad with Seven Minute Egg",
215
+ "recipe_time": 0,
216
+ "recipe_yields": "4 servings",
217
+ "recipe_ingredients": [
218
+ "2 1/2 cup Multi-Colored Fingerling Potato",
219
+ "3/4 cup Celery",
220
+ "1/4 cup Red Onion",
221
+ "2 tablespoon Fresh Parsley",
222
+ "1/3 cup Mayonnaise",
223
+ "1 tablespoon Chili Garlic Sauce",
224
+ "1 teaspoon Hoisin Sauce",
225
+ "1 splash Soy Sauce",
226
+ "to taste Salt",
227
+ "to taste Ground Black Pepper",
228
+ "4 Egg"
229
+ ],
230
+ "recipe_instructions": "Fill a large stock pot with water. Add the Multi-Colored Fingerling Potato...",
231
+ "recipe_image": "https://www.sidechef.com/recipe/eeeeeceb-493e-493d-8273-66c800821b13.jpg?d=1408x1120",
232
+ "blogger": "sidechef.com",
233
+ "recipe_nutrients": {
234
+ "calories": "80 calories",
235
+ "proteinContent": "2.1 g",
236
+ "fatContent": "6.2 g",
237
+ "carbohydrateContent": "3.9 g",
238
+ "fiberContent": "0.5 g",
239
+ "sugarContent": "0.4 g",
240
+ "sodiumContent": "108.0 mg",
241
+ "saturatedFatContent": "1.2 g",
242
+ "transFatContent": "0.0 g",
243
+ "cholesterolContent": "47.4 mg",
244
+ "unsaturatedFatContent": "3.8 g"
245
+ },
246
+ "tags": [
247
+ "Salad",
248
+ "Lunch",
249
+ "Brunch",
250
+ "Appetizers",
251
+ "Side Dish",
252
+ "Budget-Friendly",
253
+ "Vegetarian",
254
+ "Pescatarian",
255
+ "Eggs",
256
+ "Potatoes",
257
+ "Easy",
258
+ "Dairy-Free",
259
+ "Shellfish-Free",
260
+ "Entertaining",
261
+ "Fish-Free",
262
+ "Peanut-Free",
263
+ "Tree Nut-Free",
264
+ "Sugar-Free",
265
+ "Global",
266
+ "Tomato-Free",
267
+ "Stove",
268
+ ""
269
+ ],
270
+ "id_": "0000001"
271
+ }
272
+
273
+ **Example Query & Output**:
274
+
275
+ **Query**: "What are the ingredients and calories?"
276
+ **Output**:
277
+ ```json
278
+ {
279
+ "header": "Here is the information you requested:",
280
+ "recipe_ingredients": [
281
+ "2 1/2 cup Multi-Colored Fingerling Potato",
282
+ "3/4 cup Celery",
283
+ "1/4 cup Red Onion",
284
+ "2 tablespoon Fresh Parsley",
285
+ "1/3 cup Mayonnaise",
286
+ "1 tablespoon Chili Garlic Sauce",
287
+ "1 teaspoon Hoisin Sauce",
288
+ "1 splash Soy Sauce",
289
+ "to taste Salt",
290
+ "to taste Ground Black Pepper",
291
+ "4 Egg"
292
+ ],
293
+ "recipe_nutrients": {
294
+ "calories": "80 calories"
295
+ }
296
+ }
297
+
298
+ Try to format the output as JSON object with key value pairs.
299
+ """
300
+
301
+ formatted_input = f"""
302
+ User Query: {user_query}
303
+
304
+ Recipe data as Context:
305
+ {context}
306
+ """
307
+ response = router.invoke(
308
+ [SystemMessage(content=system_prompt)]
309
+ + [
310
+ HumanMessage(
311
+ content=formatted_input
312
+ )
313
+ ]
314
+ )
315
+ res = json.loads(response.content)
316
+ return res
317
+
318
+
319
  def answer_generator(formated_input, session_id):
320
  # QA system prompt and chain
321
  qa_system_prompt = """
322
  You are an AI assistant developed by Nutrigenics AI, specializing in intelligent recipe information retrieval and recipe suggestions. Your purpose is to help users by recommending recipes, providing detailed nutritional values, listing ingredients, offering step-by-step cooking instructions, and filtering recipes based on context and user queries.
323
+ Operational Guidelines: \n
324
+ 1. Input Structure: \n
325
+ - Context: You may receive contextual information related to recipes, such as specific recipe name, ingredients, nutritional informations, intsructions, recipe tags, or previously selected dishes. \n
326
+ - User Query: Users will pose questions or requests related to recipes, nutritional information, ingredient, cooking instructions, and more. \n
327
+ 2. Response Strategy: \n
328
+ - Utilize Provided Context: If the context contains relevant information that addresses the user's query, base your response on this provided data to ensure accuracy and relevance. \n
329
+ - Respond to User Query Directly: If the context does not contain the necessary information to answer the user's query, kindly state that you do not have the required information. \n
330
+ Output Format: \n
331
+ - The output format should be JSON.
332
+ - The output should have a key 'header' with response message header such as "Here is your ....",
333
+ - Then there should be other key with the actual response information. If the user query asks recipe ingredients then the key should be named "ingredients" with
334
+ JSON object as its value. The JSON object should have ingredient and its measurement as key-value pairs. Similarly if user asked for nutritional information then the output should have 'header' key with header text and 'nutrients' key
335
+ with a JSON object og nutrient and its content as key-value pairs. Similarly if the user query asks for recipe instructions then JSON output should include 'header key with header text and
336
+ 'instructions' key with a list of instructions as its value.
337
+
338
+ Following are the output formats for some cases:
339
+ 1. if user query asks for all recipe information, then output should be of following format:
340
+ {
341
+ header: header text,
342
+ recipe_name: Recipe Name,
343
+ recipe_instructions: List of recipe instructions,
344
+ recipe_nutrients: key-value pairs of nutrients name and its content,
345
+ recipe_ingredients: key-value pairs of ingredients name and its content,
346
+ recipe_tags: List of tags related to recipe,
347
+ .
348
+ .
349
+ .
350
+ }
351
+
352
+ 2. if user query asks for recipe nutrients information, then output should be of following format:
353
+ {
354
+ header: header text,
355
+ recipe_nutrients: key-value pairs of nutrients name and its content.
356
+ }
357
+
358
+ 3. if user query asks for recipe instructions information, then output should be of following format:
359
+ {
360
+ header: header text,
361
+ recipe_instructions: List of recipe instructions,
362
+ }
363
+
364
+ 4. if user query asks for recipe instructions information, then output should be of following format:
365
+ {
366
+ header: header text,
367
+ recipe_instructions: List of recipe instructions,
368
+ }
369
+
370
+
371
  Additional Instructions:
372
  - Precision and Personalization: Always aim to provide precise, personalized, and relevant information to users based on both the provided context and their specific queries.
373
  - Clarity and Coherence: Ensure all responses are clear, well-structured, and easy to understand, facilitating a seamless user experience.
 
498
  Recipe filtering instructions:
499
  - If a user asked for the highest nutrient recipe such as "high protein or high calories" then filtered recipes should be the top highest recipes from all the recipes with high nutrients.
500
  - sort or rearrange recipes based on which recipes are more appropriate for the user.
501
+ - Suggest dishes based on user preferences, dietary restrictions, available ingredients if specified by user.
502
 
503
  Your output instructions:
504
  - The function name should be filter_recipes. The input to the function should be the file name.
 
583
  'input': message,
584
  'context': data
585
  }
586
+ # response = answer_generator(formated_input, session_id=sessionID)
587
+ response = json_answer_generator(message, data)
588
  except Exception as e:
589
  print(e)
590
  response = {'content':"An error occurred while processing your request."}
591
  elif (image is None) and (message is not None):
592
  task = router_node(message)
 
593
  if task == 'recommendation':
594
  recipes = recommendation_node(message)
595
  if not recipes:
596
  response = {'content': "An error occurred while processing your request."}
597
  else:
598
+ # response = answer_formatter_node(message, recipes)
599
+ response = recipes
600
  else:
601
  formated_input = {
602
  'input': message,
603
  'context': CURR_CONTEXT
604
  }
605
+ # response = answer_generator(formated_input, session_id=sessionID)
606
+ response = json_answer_generator(message, CURR_CONTEXT)
607
  return response
608
 
609
  import json