Skip to content

General RAG query returns incorrect/irrelevant medicines #1

Description

@zenitsu0509

Summary

  • In the Full RAG Query flow (tab: "❓ General FAQ"), the selected “top relevant medicine” is often incorrect or not aligned with the user’s intent. This leads to wrong context being passed to the LLM and an unhelpful final answer.

Repro

  1. Run the Streamlit app and go to tab "❓ Full RAG Query".
  2. Enter a general query like: "What are the side effects of a medicine for headaches?" or "Which medicine helps with fever?"
  3. Click "🔍 Run Full RAG".
  4. Observe the "Top relevant medicine" card and the LLM response.

Expected

  • The chosen anchor medicine should match the intended condition (e.g., for headache queries, a relevant analgesic; for fever, an antipyretic). The context should include appropriate uses/conditions and side effects for that selection.

Actual

  • The chosen anchor medicine is sometimes unrelated or loosely related, suggesting the selection heuristics are not robust.

Suspected Areas

  • Condition extraction heuristic: GraphQueryEngine._extract_condition_from_query in graph_rag_query.py may miss or mis-parse condition phrases.
  • Condition-to-medicine ranking: GraphQueryEngine._best_medicine_for_condition may pick suboptimal results (ordering/filters).
  • Vector fallback: GraphQueryEngine.retrieve_context_for_rag uses a 1-nearest vector search on the entire user query, which can skew results when the prompt contains generic words.

Proposed Fix Plan

  • Improve _extract_condition_from_query with a small set of additional patterns (e.g., handle plurals, “relief for”, “good for”, “treats”, “used for”, “helps with”) and strip common stop words.
  • In _best_medicine_for_condition, add a tie-breaker using number of matching :TREATS or uses_text density; ensure we return medicines with an image_url when available to aid UI, but not as the main rank factor.
  • In vector fallback, encode only the extracted condition phrase (if present) rather than the entire question; otherwise, try a hybrid: 1) vector search top-5, 2) re-rank by mention of the extracted condition in uses_text and :TREATS edges.
  • Add minimal unit-style checks (simple script) to validate that queries like “medicine for fever” or “medicine for headache” map to a sensible medicine from the dataset.

Additional Context

  • Relevant files: graph_rag_query.py (selection and retrieval), ingest_graph.py (embedding text construction and index config).
  • Vector index: medicine_embeddings on Medicine.embedding (dim 384, cosine).

Acceptance Criteria

  • For common queries (fever, headache, cold), the top medicine chosen matches typical use cases from the ingested CSV and graph relationships.
  • Manual smoke test on at least 5 queries shows improved precision of the anchor medicine.

Labels

  • bug
  • rag
  • priority:medium
  • area:querying

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions