Spaces:
Runtime error
Runtime error
Download rag.py from LeeHan23/nutribot-api: direct link, hf CLI and curl.
- Browser
- Download file 1.53 kB
-
https://huggingface.co/spaces/LeeHan23/nutribot-api/resolve/main/rag.py
- Command line
-
hf download hf://spaces/LeeHan23/nutribot-api/rag.py
-
curl -L -o rag.py https://huggingface.co/spaces/LeeHan23/nutribot-api/resolve/main/rag.py
1.53 kB
| from llm import get_direct_llm_response | |
| from image_handler import parse_response_for_image | |
| from chain_factory import create_conversational_chain | |
| RAG_FAILURE_PHRASES = ["i don't know", "i am not sure", "i cannot answer"] | |
| def identify_target_disease(question: str) -> str: | |
| """ | |
| Uses a direct LLM call to identify the primary health condition in the user's query. | |
| """ | |
| prompt = f""" | |
| Analyze the following user question and identify the primary health condition or disease mentioned. | |
| If a specific condition like 'Type 2 Diabetes', 'hypertension', 'CKD', or 'high cholesterol' is mentioned, return that name. | |
| If no specific disease is mentioned, return the phrase 'general health and wellness'. | |
| Respond with only the name of the condition and nothing else. | |
| User Question: "{question}" | |
| """ | |
| disease = get_direct_llm_response(prompt) | |
| print(f"[DEBUG] Identified target condition: {disease}") | |
| return disease.strip() | |
| def get_rag_response(question: str, client_id: int, chat_session_id: str) -> dict: | |
| target_disease = identify_target_disease(question) | |
| qa_chain = create_conversational_chain(client_id, target_disease) | |
| response = qa_chain({"question": question}) | |
| answer = response.get("answer", "") | |
| if not answer or any(phrase.lower() in answer.lower() for phrase in RAG_FAILURE_PHRASES): | |
| print("RAG response insufficient. Falling back to direct LLM.") | |
| answer = get_direct_llm_response(question) | |
| return parse_response_for_image(answer) |