Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -115,29 +115,35 @@ def expand_query(state):
|
|
115 |
Dict: The updated state with the expanded query.
|
116 |
"""
|
117 |
print("---------Expanding Query---------")
|
118 |
-
system_message = '''You are
|
119 |
-
Your task is to
|
|
|
|
|
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 |
-
|
128 |
-
|
129 |
-
Expanded Query: "Provide details on eating disorders, including types (e.g., anorexia nervosa, bulimia nervosa), symptoms, causes, and treatment options."
|
130 |
|
131 |
-
|
132 |
-
|
|
|
|
|
|
|
133 |
|
134 |
-
|
135 |
-
|
|
|
136 |
|
137 |
-
|
138 |
-
|
139 |
|
140 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
|
142 |
expand_prompt = ChatPromptTemplate.from_messages([
|
143 |
("system", system_message),
|
@@ -209,27 +215,34 @@ def craft_response(state: Dict) -> Dict:
|
|
209 |
Returns:
|
210 |
Dict: The updated state with the generated response.
|
211 |
"""
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
233 |
|
234 |
response_prompt = ChatPromptTemplate.from_messages([
|
235 |
("system", system_message),
|
@@ -256,37 +269,43 @@ def score_groundedness(state: Dict) -> Dict:
|
|
256 |
Dict: The updated state with the groundedness score.
|
257 |
"""
|
258 |
print("---------check_groundedness---------")
|
259 |
-
system_message = '''You are an
|
|
|
|
|
|
|
|
|
260 |
|
261 |
-
|
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 |
-
|
267 |
-
|
268 |
-
|
|
|
269 |
|
270 |
-
|
271 |
-
|
|
|
272 |
|
273 |
-
|
274 |
-
|
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 |
-
|
279 |
-
|
280 |
-
|
|
|
281 |
|
282 |
-
|
283 |
-
|
284 |
-
|
285 |
|
286 |
-
|
|
|
|
|
287 |
|
288 |
-
|
289 |
-
|
|
|
|
|
290 |
|
291 |
groundedness_prompt = ChatPromptTemplate.from_messages([
|
292 |
("system", system_message),
|
@@ -317,40 +336,45 @@ def check_precision(state: Dict) -> Dict:
|
|
317 |
Dict: The updated state with the precision score.
|
318 |
"""
|
319 |
print("---------check_precision---------")
|
320 |
-
system_message = '''You are an
|
321 |
-
|
322 |
-
|
323 |
-
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
|
|
|
|
|
|
|
|
|
|
354 |
precision_prompt = ChatPromptTemplate.from_messages([
|
355 |
("system", system_message),
|
356 |
("user", "Query: {query}\nResponse: {response}\n\nPrecision score:")
|
@@ -380,30 +404,33 @@ def refine_response(state: Dict) -> Dict:
|
|
380 |
Dict: The updated state with response refinement suggestions.
|
381 |
"""
|
382 |
print("---------refine_response---------")
|
|
|
|
|
|
|
383 |
|
384 |
-
|
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 |
-
|
395 |
-
|
396 |
|
397 |
-
2. Query: "How is bulimia nervosa treated?"
|
398 |
-
|
399 |
-
|
400 |
|
401 |
-
3. Query: "What causes binge eating disorder?"
|
402 |
-
|
403 |
-
|
404 |
|
405 |
-
Now, suggest improvements for the following response:
|
406 |
-
'''
|
407 |
|
408 |
refine_response_prompt = ChatPromptTemplate.from_messages([
|
409 |
("system", system_message),
|
@@ -433,28 +460,32 @@ def refine_query(state: Dict) -> Dict:
|
|
433 |
Dict: The updated state with query refinement suggestions.
|
434 |
"""
|
435 |
print("---------refine_query---------")
|
436 |
-
system_message = '''You are
|
|
|
|
|
|
|
|
|
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 |
-
|
446 |
-
|
447 |
|
448 |
-
2. Original Query: "What is anorexia?"
|
449 |
-
|
450 |
-
|
451 |
|
452 |
-
3. Original Query: "How to treat bulimia?"
|
453 |
-
|
454 |
-
|
455 |
|
456 |
-
Now, suggest improvements for the following expanded query:
|
457 |
-
'''
|
458 |
|
459 |
refine_query_prompt = ChatPromptTemplate.from_messages([
|
460 |
("system", system_message),
|
|
|
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 |
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 |
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 |
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 |
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 |
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),
|