Upload app.py with huggingface_hub
Browse files
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
|
119 |
-
|
120 |
-
and additional context that can improve the retrieval of detailed nutrition disorder-related information.
|
121 |
-
Please expand the following query:'''
|
122 |
|
|
|
|
|
|
|
|
|
|
|
123 |
|
124 |
-
|
125 |
-
|
|
|
126 |
|
127 |
-
|
128 |
-
|
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 |
-
|
134 |
-
|
135 |
-
# Expanded Query: "Provide details on eating disorders, including types (e.g., anorexia nervosa, bulimia nervosa), symptoms, causes, and treatment options."
|
136 |
|
137 |
-
|
138 |
-
|
139 |
|
140 |
-
|
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 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
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
|
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 |
-
|
|
|
|
|
|
|
279 |
|
280 |
-
|
281 |
-
|
282 |
-
|
283 |
-
# - Flag any unsupported claims or deviations from the context.
|
284 |
|
285 |
-
|
286 |
-
|
287 |
-
# - If citations are missing, suggest adding them.
|
288 |
|
289 |
-
|
290 |
-
|
|
|
|
|
291 |
|
292 |
-
|
293 |
-
|
294 |
-
|
295 |
-
# Evaluation: "The response is grounded and properly cited. Groundedness score: 1.0."
|
296 |
|
297 |
-
|
298 |
-
|
299 |
-
|
300 |
|
301 |
-
|
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 |
-
|
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
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
355 |
-
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
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 |
-
|
412 |
|
413 |
-
|
414 |
-
|
415 |
-
|
416 |
-
|
417 |
-
|
418 |
|
419 |
-
|
420 |
-
|
421 |
-
|
422 |
-
|
423 |
|
424 |
-
|
425 |
-
|
426 |
-
|
427 |
|
428 |
-
|
429 |
-
|
430 |
-
|
431 |
|
432 |
-
|
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
|
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 |
-
|
470 |
-
|
471 |
-
|
472 |
-
|
473 |
|
474 |
-
|
475 |
-
|
476 |
-
|
477 |
-
|
478 |
|
479 |
-
|
480 |
-
|
481 |
-
|
482 |
|
483 |
-
|
484 |
-
|
485 |
-
|
486 |
|
487 |
-
|
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-
|
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 |
-
|
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:
|