Upload app.py with huggingface_hub
Browse files
app.py
CHANGED
@@ -116,28 +116,28 @@ def expand_query(state):
|
|
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 |
-
|
130 |
|
131 |
-
2. User Query: "What is anorexia?"
|
132 |
-
|
133 |
|
134 |
-
3. User Query: "How to treat bulimia?"
|
135 |
-
|
136 |
|
137 |
-
4. User Query: "What are the effects of malnutrition?"
|
138 |
-
|
139 |
|
140 |
-
Now, expand the following query:'''
|
141 |
|
142 |
expand_prompt = ChatPromptTemplate.from_messages([
|
143 |
("system", system_message),
|
@@ -210,26 +210,26 @@ def craft_response(state: Dict) -> Dict:
|
|
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),
|
@@ -258,35 +258,35 @@ def score_groundedness(state: Dict) -> Dict:
|
|
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 |
-
|
264 |
-
|
265 |
|
266 |
-
2. **Citation Check**:
|
267 |
-
|
268 |
-
|
269 |
|
270 |
-
3. **Scoring**:
|
271 |
-
|
272 |
|
273 |
-
Examples:
|
274 |
-
1. Response: "Anorexia nervosa is caused by genetic factors (Source 1)."
|
275 |
-
|
276 |
-
|
277 |
|
278 |
-
2. Response: "Bulimia nervosa can be cured with diet alone."
|
279 |
-
|
280 |
-
|
281 |
|
282 |
-
3. Response: "Anorexia nervosa has a high mortality rate."
|
283 |
-
|
284 |
-
|
285 |
|
286 |
-
|
287 |
|
288 |
-
Now, evaluate the following response:
|
289 |
-
'''
|
290 |
|
291 |
groundedness_prompt = ChatPromptTemplate.from_messages([
|
292 |
("system", system_message),
|
@@ -318,39 +318,39 @@ def check_precision(state: Dict) -> Dict:
|
|
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 |
-
|
333 |
-
|
334 |
-
|
335 |
-
2. Query: "How is bulimia nervosa treated?"
|
336 |
-
|
337 |
-
|
338 |
-
|
339 |
-
3. Query: "What causes binge eating disorder?"
|
340 |
-
|
341 |
-
|
342 |
-
|
343 |
-
4. Query: "What are the effects of malnutrition?"
|
344 |
-
|
345 |
-
|
346 |
-
|
347 |
-
5. Query: "What is the mortality rate of anorexia nervosa?"
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
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:")
|
@@ -383,27 +383,27 @@ def refine_response(state: Dict) -> Dict:
|
|
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 |
-
|
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),
|
@@ -435,26 +435,26 @@ def refine_query(state: Dict) -> Dict:
|
|
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 |
-
|
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),
|
@@ -643,8 +643,9 @@ class NutritionBot:
|
|
643 |
self.client = ChatOpenAI(
|
644 |
model_name="gpt-4o-mini", # Specify the model to use (e.g., GPT-4 optimized version)
|
645 |
api_key=config.get("API_KEY"), # API key for authentication
|
646 |
-
|
647 |
-
temperature=0 # Controls randomness in responses; 0 ensures deterministic results
|
|
|
648 |
)
|
649 |
|
650 |
|
|
|
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),
|
|
|
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),
|
|
|
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),
|
|
|
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:")
|
|
|
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),
|
|
|
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),
|
|
|
643 |
self.client = ChatOpenAI(
|
644 |
model_name="gpt-4o-mini", # Specify the model to use (e.g., GPT-4 optimized version)
|
645 |
api_key=config.get("API_KEY"), # API key for authentication
|
646 |
+
base_url = config.get("OPENAI_API_BASE"),
|
647 |
+
temperature=0, # Controls randomness in responses; 0 ensures deterministic results
|
648 |
+
streaming=False
|
649 |
)
|
650 |
|
651 |
|