stckwok commited on
Commit
35213e1
·
verified ·
1 Parent(s): 5da5181

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +133 -166
app.py CHANGED
@@ -115,35 +115,29 @@ def expand_query(state):
115
  Dict: The updated state with the expanded query.
116
  """
117
  print("---------Expanding Query---------")
118
- system_message = '''You are a nutrition expert and language model specialized in nutritional disorders.
119
- Your task is to expand the provided query by incorporating related keywords, synonyms,
120
- and additional context that can improve the retrieval of detailed nutrition disorder-related information.
121
- Please expand the following query:'''
122
 
 
 
 
 
 
123
 
124
- # system_message = '''You are an AI specializing in improving search queries to retrieve the most relevant nutrition disorder-related information.
125
- # Your task is to **refine** and **expand** the given query so that better search results are obtained, while **keeping the original intent** unchanged.
 
126
 
127
- # Guidelines:
128
- # - Add **specific details** where needed. Example: If a user asks about "anorexia," specify aspects like symptoms, causes, or treatment options.
129
- # - Include **related terms** to improve retrieval (e.g., “bulimia” → “bulimia nervosa vs binge eating disorder”).
130
- # - If the user provides an unclear query, suggest necessary clarifications.
131
- # - **DO NOT** answer the question. Your job is only to enhance the query.
132
 
133
- # Examples:
134
- # 1. User Query: "Tell me about eating disorders."
135
- # Expanded Query: "Provide details on eating disorders, including types (e.g., anorexia nervosa, bulimia nervosa), symptoms, causes, and treatment options."
136
 
137
- # 2. User Query: "What is anorexia?"
138
- # Expanded Query: "Explain anorexia nervosa, including its symptoms, causes, risk factors, and treatment options."
139
 
140
- # 3. User Query: "How to treat bulimia?"
141
- # Expanded Query: "Describe treatment options for bulimia nervosa, including psychotherapy, medications, and lifestyle changes."
142
-
143
- # 4. User Query: "What are the effects of malnutrition?"
144
- # Expanded Query: "Explain the effects of malnutrition on physical and mental health, including specific nutrient deficiencies and their consequences."
145
-
146
- # Now, expand the following query:'''
147
 
148
  expand_prompt = ChatPromptTemplate.from_messages([
149
  ("system", system_message),
@@ -215,34 +209,27 @@ def craft_response(state: Dict) -> Dict:
215
  Returns:
216
  Dict: The updated state with the generated response.
217
  """
218
-
219
- system_message = '''You are a nutrition expert and your responses should be clear, concise,
220
- and evidence-based. Use the provided context to accurately address the user's query regarding
221
- nutritional disorders.
222
- Please generate a response to the following query: '''
223
-
224
-
225
- # system_message = '''You are a professional AI nutrition disorder specialist generating responses based on retrieved documents.
226
- # Your task is to use the given **context** to generate a highly accurate, informative, and user-friendly response.
227
-
228
- # Guidelines:
229
- # - **Be direct and concise** while ensuring completeness.
230
- # - **DO NOT include information that is not present in the context.**
231
- # - If multiple sources exist, synthesize them into a coherent response.
232
- # - If the context does not fully answer the query, state what additional information is needed.
233
- # - Use bullet points when explaining complex concepts.
234
-
235
- # Example:
236
- # User Query: "What are the symptoms of anorexia nervosa?"
237
- # Context:
238
- # 1. Anorexia nervosa is characterized by extreme weight loss and fear of gaining weight.
239
- # 2. Common symptoms include restricted eating, distorted body image, and excessive exercise.
240
- # Response:
241
- # "Anorexia nervosa is an eating disorder characterized by extreme weight loss and an intense fear of gaining weight. Common symptoms include:
242
- # - Restricted eating
243
- # - Distorted body image
244
- # - Excessive exercise
245
- # If you or someone you know is experiencing these symptoms, it is important to seek professional help."'''
246
 
247
  response_prompt = ChatPromptTemplate.from_messages([
248
  ("system", system_message),
@@ -269,43 +256,37 @@ def score_groundedness(state: Dict) -> Dict:
269
  Dict: The updated state with the groundedness score.
270
  """
271
  print("---------check_groundedness---------")
272
- system_message = '''You are an evaluator for response groundedness. Given the context and the response related to nutritional disorders,
273
- provide a numerical score between 0 and 1 where 0 means the response is not grounded at all, and 1 means it is completely grounded
274
- in the context.
275
- Please generate a response to the following query: '''
276
-
277
 
278
- # system_message = '''You are an AI tasked with evaluating whether a response is grounded in the provided context and includes proper citations.
 
 
 
279
 
280
- # Guidelines:
281
- # 1. **Groundedness Check**:
282
- # - Verify that the response accurately reflects the information in the context.
283
- # - Flag any unsupported claims or deviations from the context.
284
 
285
- # 2. **Citation Check**:
286
- # - Ensure that the response includes citations to the source material (e.g., "According to [Source], ...").
287
- # - If citations are missing, suggest adding them.
288
 
289
- # 3. **Scoring**:
290
- # - Assign a groundedness score between 0 and 1, where 1 means fully grounded and properly cited.
 
 
291
 
292
- # Examples:
293
- # 1. Response: "Anorexia nervosa is caused by genetic factors (Source 1)."
294
- # Context: "Anorexia nervosa is influenced by genetic, environmental, and psychological factors (Source 1)."
295
- # Evaluation: "The response is grounded and properly cited. Groundedness score: 1.0."
296
 
297
- # 2. Response: "Bulimia nervosa can be cured with diet alone."
298
- # Context: "Treatment for bulimia nervosa involves psychotherapy and medications (Source 2)."
299
- # Evaluation: "The response is ungrounded and lacks citations. Groundedness score: 0.2."
300
 
301
- # 3. Response: "Anorexia nervosa has a high mortality rate."
302
- # Context: "Anorexia nervosa has one of the highest mortality rates among psychiatric disorders (Source 3)."
303
- # Evaluation: "The response is grounded but lacks a citation. Groundedness score: 0.7. ."
304
 
305
- # ****Return only a float score (e.g., 0.9). Do not provide explanations.****
306
-
307
- # Now, evaluate the following response:
308
- # '''
309
 
310
  groundedness_prompt = ChatPromptTemplate.from_messages([
311
  ("system", system_message),
@@ -336,45 +317,40 @@ def check_precision(state: Dict) -> Dict:
336
  Dict: The updated state with the precision score.
337
  """
338
  print("---------check_precision---------")
339
- system_message = '''You are an evaluator for response precision. Given the query and the response, provide a numerical score between
340
- 0 and 1 where 0 indicates that the response does not address the query at all, and 1 indicates that the response precisely addresses
341
- the query.
342
- Please generate a response to the following query: '''
343
-
344
- # system_message = '''You are an AI evaluator assessing the **precision** of the response.
345
- # Your task is to **score** how well the response addresses the user’s original nutrition disorder-related query.
346
-
347
- # Scoring Criteria:
348
- # - 1.0 → The response is fully precise, directly answering the question.
349
- # - 0.7 → The response is mostly correct but contains some generalization.
350
- # - 0.5 The response is somewhat relevant but lacks key details.
351
- # - 0.3 The response is vague or only partially correct.
352
- # - 0.0 → The response is incorrect or misleading.
353
-
354
- # Examples:
355
- # 1. Query: "What are the symptoms of anorexia nervosa?"
356
- # Response: "The symptoms of anorexia nervosa include extreme weight loss, fear of gaining weight, and a distorted body image."
357
- # Precision Score: 1.0
358
-
359
- # 2. Query: "How is bulimia nervosa treated?"
360
- # Response: "Bulimia nervosa is treated with therapy and medications."
361
- # Precision Score: 0.7
362
-
363
- # 3. Query: "What causes binge eating disorder?"
364
- # Response: "Binge eating disorder is caused by a combination of genetic, psychological, and environmental factors."
365
- # Precision Score: 0.5
366
-
367
- # 4. Query: "What are the effects of malnutrition?"
368
- # Response: "Malnutrition can lead to health problems."
369
- # Precision Score: 0.3
370
-
371
- # 5. Query: "What is the mortality rate of anorexia nervosa?"
372
- # Response: "Anorexia nervosa is a type of eating disorder."
373
- # Precision Score: 0.0
374
-
375
- # *****Return only a float score (e.g., 0.9). Do not provide explanations.*****
376
- # Now, evaluate the following query and response:
377
- # '''
378
  precision_prompt = ChatPromptTemplate.from_messages([
379
  ("system", system_message),
380
  ("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")
@@ -404,33 +380,30 @@ def refine_response(state: Dict) -> Dict:
404
  Dict: The updated state with response refinement suggestions.
405
  """
406
  print("---------refine_response---------")
407
- system_message = '''You are an expert editor in nutritional science communications. Your role is to review the response given to a
408
- nutritional query and provide clear suggestions to improve its accuracy, clarity, and completeness.
409
- Focus on making sure that the response fully addresses the query and is supported by evidence-based nutritional guidelines.'''
410
 
411
- # system_message = '''You are an AI response refinement assistant. Your task is to suggest **improvements** for the given response.
412
 
413
- # ### Guidelines:
414
- # - Identify **gaps in the explanation** (missing key details).
415
- # - Highlight **unclear or vague parts** that need elaboration.
416
- # - Suggest **additional details** that should be included for better accuracy.
417
- # - Ensure the refined response is **precise** and **grounded** in the retrieved context.
418
 
419
- # ### Examples:
420
- # 1. Query: "What are the symptoms of anorexia nervosa?"
421
- # Response: "The symptoms include weight loss and fear of gaining weight."
422
- # Suggestions: "The response is missing key details about behavioral and emotional symptoms. Add details like 'distorted body image' and 'restrictive eating patterns.'"
423
 
424
- # 2. Query: "How is bulimia nervosa treated?"
425
- # Response: "Bulimia nervosa is treated with therapy."
426
- # Suggestions: "The response is too vague. Specify the types of therapy (e.g., cognitive-behavioral therapy) and mention other treatments like nutritional counseling and medications."
427
 
428
- # 3. Query: "What causes binge eating disorder?"
429
- # Response: "Binge eating disorder is caused by psychological factors."
430
- # Suggestions: "The response is incomplete. Add details about genetic and environmental factors, and explain how they contribute to the disorder."
431
 
432
- # Now, suggest improvements for the following response:
433
- # '''
434
 
435
  refine_response_prompt = ChatPromptTemplate.from_messages([
436
  ("system", system_message),
@@ -460,32 +433,28 @@ def refine_query(state: Dict) -> Dict:
460
  Dict: The updated state with query refinement suggestions.
461
  """
462
  print("---------refine_query---------")
463
- system_message = '''You are a search query refinement expert. Given the original and expanded queries related to nutritional disorders,
464
- provide suggestions to refine the query further for improved search results.'''
465
-
466
-
467
- # system_message = '''You are an AI query refinement assistant. Your task is to suggest **improvements** for the expanded query.
468
 
469
- # ### Guidelines:
470
- # - Add **specific keywords** to improve document retrieval.
471
- # - Identify **missing details** that should be included.
472
- # - Suggest **ways to narrow the scope** for better precision.
473
 
474
- # ### Examples:
475
- # 1. Original Query: "Tell me about eating disorders."
476
- # Expanded Query: "Provide details on eating disorders, including types, symptoms, causes, and treatment options."
477
- # Suggestions: "Add specific types of eating disorders like 'anorexia nervosa' and 'bulimia nervosa' to improve retrieval."
478
 
479
- # 2. Original Query: "What is anorexia?"
480
- # Expanded Query: "Explain anorexia nervosa, including its symptoms and causes."
481
- # Suggestions: "Include details about treatment options and risk factors to make the query more comprehensive."
482
 
483
- # 3. Original Query: "How to treat bulimia?"
484
- # Expanded Query: "Describe treatment options for bulimia nervosa."
485
- # Suggestions: "Specify types of treatments like 'cognitive-behavioral therapy' and 'medications' for better precision."
486
 
487
- # Now, suggest improvements for the following expanded query:
488
- # '''
489
 
490
  refine_query_prompt = ChatPromptTemplate.from_messages([
491
  ("system", system_message),
@@ -636,7 +605,7 @@ def agentic_rag(query: str):
636
  #================================ Guardrails ===========================#
637
  llama_guard_client = Groq(api_key=llama_api_key)
638
  # Function to filter user input with Llama Guard
639
- def filter_input_with_llama_guard(user_input, model="llama-guard-3-8b"):
640
  """
641
  Filters user input using Llama Guard to ensure it is safe.
642
 
@@ -872,12 +841,10 @@ def nutrition_disorder_streamlit():
872
  # print(filtered_result)
873
  st.write(filtered_result)
874
 
875
- filtered_result="safe"
876
  # Process through the agent
877
  with st.chat_message("assistant"):
878
- if filtered_result in ["safe", "unsafe S7", "unsafe S6"]:
879
- # if filtered_result in ["SAFE", "S6", "S7"]:
880
- # if not filtered_result: #debug
881
  try:
882
  # Initialize chatbot if not already done
883
  if 'chatbot' not in st.session_state:
 
115
  Dict: The updated state with the expanded query.
116
  """
117
  print("---------Expanding Query---------")
118
+ system_message = '''You are an AI specializing in improving search queries to retrieve the most relevant nutrition disorder-related information.
119
+ Your task is to **refine** and **expand** the given query so that better search results are obtained, while **keeping the original intent** unchanged.
 
 
120
 
121
+ Guidelines:
122
+ - Add **specific details** where needed. Example: If a user asks about "anorexia," specify aspects like symptoms, causes, or treatment options.
123
+ - Include **related terms** to improve retrieval (e.g., “bulimia” → “bulimia nervosa vs binge eating disorder”).
124
+ - If the user provides an unclear query, suggest necessary clarifications.
125
+ - **DO NOT** answer the question. Your job is only to enhance the query.
126
 
127
+ Examples:
128
+ 1. User Query: "Tell me about eating disorders."
129
+ Expanded Query: "Provide details on eating disorders, including types (e.g., anorexia nervosa, bulimia nervosa), symptoms, causes, and treatment options."
130
 
131
+ 2. User Query: "What is anorexia?"
132
+ Expanded Query: "Explain anorexia nervosa, including its symptoms, causes, risk factors, and treatment options."
 
 
 
133
 
134
+ 3. User Query: "How to treat bulimia?"
135
+ Expanded Query: "Describe treatment options for bulimia nervosa, including psychotherapy, medications, and lifestyle changes."
 
136
 
137
+ 4. User Query: "What are the effects of malnutrition?"
138
+ Expanded Query: "Explain the effects of malnutrition on physical and mental health, including specific nutrient deficiencies and their consequences."
139
 
140
+ Now, expand the following query:'''
 
 
 
 
 
 
141
 
142
  expand_prompt = ChatPromptTemplate.from_messages([
143
  ("system", system_message),
 
209
  Returns:
210
  Dict: The updated state with the generated response.
211
  """
212
+ system_message = '''You are a professional AI nutrition disorder specialist generating responses based on retrieved documents.
213
+ Your task is to use the given **context** to generate a highly accurate, informative, and user-friendly response.
214
+
215
+ Guidelines:
216
+ - **Be direct and concise** while ensuring completeness.
217
+ - **DO NOT include information that is not present in the context.**
218
+ - If multiple sources exist, synthesize them into a coherent response.
219
+ - If the context does not fully answer the query, state what additional information is needed.
220
+ - Use bullet points when explaining complex concepts.
221
+
222
+ Example:
223
+ User Query: "What are the symptoms of anorexia nervosa?"
224
+ Context:
225
+ 1. Anorexia nervosa is characterized by extreme weight loss and fear of gaining weight.
226
+ 2. Common symptoms include restricted eating, distorted body image, and excessive exercise.
227
+ Response:
228
+ "Anorexia nervosa is an eating disorder characterized by extreme weight loss and an intense fear of gaining weight. Common symptoms include:
229
+ - Restricted eating
230
+ - Distorted body image
231
+ - Excessive exercise
232
+ If you or someone you know is experiencing these symptoms, it is important to seek professional help."'''
 
 
 
 
 
 
 
233
 
234
  response_prompt = ChatPromptTemplate.from_messages([
235
  ("system", system_message),
 
256
  Dict: The updated state with the groundedness score.
257
  """
258
  print("---------check_groundedness---------")
259
+ system_message = '''You are an AI tasked with evaluating whether a response is grounded in the provided context and includes proper citations.
 
 
 
 
260
 
261
+ Guidelines:
262
+ 1. **Groundedness Check**:
263
+ - Verify that the response accurately reflects the information in the context.
264
+ - Flag any unsupported claims or deviations from the context.
265
 
266
+ 2. **Citation Check**:
267
+ - Ensure that the response includes citations to the source material (e.g., "According to [Source], ...").
268
+ - If citations are missing, suggest adding them.
 
269
 
270
+ 3. **Scoring**:
271
+ - Assign a groundedness score between 0 and 1, where 1 means fully grounded and properly cited.
 
272
 
273
+ Examples:
274
+ 1. Response: "Anorexia nervosa is caused by genetic factors (Source 1)."
275
+ Context: "Anorexia nervosa is influenced by genetic, environmental, and psychological factors (Source 1)."
276
+ Evaluation: "The response is grounded and properly cited. Groundedness score: 1.0."
277
 
278
+ 2. Response: "Bulimia nervosa can be cured with diet alone."
279
+ Context: "Treatment for bulimia nervosa involves psychotherapy and medications (Source 2)."
280
+ Evaluation: "The response is ungrounded and lacks citations. Groundedness score: 0.2."
 
281
 
282
+ 3. Response: "Anorexia nervosa has a high mortality rate."
283
+ Context: "Anorexia nervosa has one of the highest mortality rates among psychiatric disorders (Source 3)."
284
+ Evaluation: "The response is grounded but lacks a citation. Groundedness score: 0.7. ."
285
 
286
+ ****Return only a float score (e.g., 0.9). Do not provide explanations.****
 
 
287
 
288
+ Now, evaluate the following response:
289
+ '''
 
 
290
 
291
  groundedness_prompt = ChatPromptTemplate.from_messages([
292
  ("system", system_message),
 
317
  Dict: The updated state with the precision score.
318
  """
319
  print("---------check_precision---------")
320
+ system_message = '''You are an AI evaluator assessing the **precision** of the response.
321
+ Your task is to **score** how well the response addresses the user’s original nutrition disorder-related query.
322
+
323
+ Scoring Criteria:
324
+ - 1.0 → The response is fully precise, directly answering the question.
325
+ - 0.7 The response is mostly correct but contains some generalization.
326
+ - 0.5 The response is somewhat relevant but lacks key details.
327
+ - 0.3 → The response is vague or only partially correct.
328
+ - 0.0 → The response is incorrect or misleading.
329
+
330
+ Examples:
331
+ 1. Query: "What are the symptoms of anorexia nervosa?"
332
+ Response: "The symptoms of anorexia nervosa include extreme weight loss, fear of gaining weight, and a distorted body image."
333
+ Precision Score: 1.0
334
+
335
+ 2. Query: "How is bulimia nervosa treated?"
336
+ Response: "Bulimia nervosa is treated with therapy and medications."
337
+ Precision Score: 0.7
338
+
339
+ 3. Query: "What causes binge eating disorder?"
340
+ Response: "Binge eating disorder is caused by a combination of genetic, psychological, and environmental factors."
341
+ Precision Score: 0.5
342
+
343
+ 4. Query: "What are the effects of malnutrition?"
344
+ Response: "Malnutrition can lead to health problems."
345
+ Precision Score: 0.3
346
+
347
+ 5. Query: "What is the mortality rate of anorexia nervosa?"
348
+ Response: "Anorexia nervosa is a type of eating disorder."
349
+ Precision Score: 0.0
350
+
351
+ *****Return only a float score (e.g., 0.9). Do not provide explanations.*****
352
+ Now, evaluate the following query and response:
353
+ '''
 
 
 
 
 
354
  precision_prompt = ChatPromptTemplate.from_messages([
355
  ("system", system_message),
356
  ("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")
 
380
  Dict: The updated state with response refinement suggestions.
381
  """
382
  print("---------refine_response---------")
 
 
 
383
 
384
+ system_message = '''You are an AI response refinement assistant. Your task is to suggest **improvements** for the given response.
385
 
386
+ ### Guidelines:
387
+ - Identify **gaps in the explanation** (missing key details).
388
+ - Highlight **unclear or vague parts** that need elaboration.
389
+ - Suggest **additional details** that should be included for better accuracy.
390
+ - Ensure the refined response is **precise** and **grounded** in the retrieved context.
391
 
392
+ ### Examples:
393
+ 1. Query: "What are the symptoms of anorexia nervosa?"
394
+ Response: "The symptoms include weight loss and fear of gaining weight."
395
+ Suggestions: "The response is missing key details about behavioral and emotional symptoms. Add details like 'distorted body image' and 'restrictive eating patterns.'"
396
 
397
+ 2. Query: "How is bulimia nervosa treated?"
398
+ Response: "Bulimia nervosa is treated with therapy."
399
+ Suggestions: "The response is too vague. Specify the types of therapy (e.g., cognitive-behavioral therapy) and mention other treatments like nutritional counseling and medications."
400
 
401
+ 3. Query: "What causes binge eating disorder?"
402
+ Response: "Binge eating disorder is caused by psychological factors."
403
+ Suggestions: "The response is incomplete. Add details about genetic and environmental factors, and explain how they contribute to the disorder."
404
 
405
+ Now, suggest improvements for the following response:
406
+ '''
407
 
408
  refine_response_prompt = ChatPromptTemplate.from_messages([
409
  ("system", system_message),
 
433
  Dict: The updated state with query refinement suggestions.
434
  """
435
  print("---------refine_query---------")
436
+ system_message = '''You are an AI query refinement assistant. Your task is to suggest **improvements** for the expanded query.
 
 
 
 
437
 
438
+ ### Guidelines:
439
+ - Add **specific keywords** to improve document retrieval.
440
+ - Identify **missing details** that should be included.
441
+ - Suggest **ways to narrow the scope** for better precision.
442
 
443
+ ### Examples:
444
+ 1. Original Query: "Tell me about eating disorders."
445
+ Expanded Query: "Provide details on eating disorders, including types, symptoms, causes, and treatment options."
446
+ Suggestions: "Add specific types of eating disorders like 'anorexia nervosa' and 'bulimia nervosa' to improve retrieval."
447
 
448
+ 2. Original Query: "What is anorexia?"
449
+ Expanded Query: "Explain anorexia nervosa, including its symptoms and causes."
450
+ Suggestions: "Include details about treatment options and risk factors to make the query more comprehensive."
451
 
452
+ 3. Original Query: "How to treat bulimia?"
453
+ Expanded Query: "Describe treatment options for bulimia nervosa."
454
+ Suggestions: "Specify types of treatments like 'cognitive-behavioral therapy' and 'medications' for better precision."
455
 
456
+ Now, suggest improvements for the following expanded query:
457
+ '''
458
 
459
  refine_query_prompt = ChatPromptTemplate.from_messages([
460
  ("system", system_message),
 
605
  #================================ Guardrails ===========================#
606
  llama_guard_client = Groq(api_key=llama_api_key)
607
  # Function to filter user input with Llama Guard
608
+ def filter_input_with_llama_guard(user_input, model="meta-llama/llama-guard-4-12b"):
609
  """
610
  Filters user input using Llama Guard to ensure it is safe.
611
 
 
841
  # print(filtered_result)
842
  st.write(filtered_result)
843
 
 
844
  # Process through the agent
845
  with st.chat_message("assistant"):
846
+ # if filtered_result in ["safe", "unsafe S7", "unsafe S6"]:
847
+ if filtered_result in ["SAFE", "S6", "S7"]:
 
848
  try:
849
  # Initialize chatbot if not already done
850
  if 'chatbot' not in st.session_state: