Skip to content

Retrieval Pipeline

PantheraNestah edited this page Nov 5, 2026 · 1 revision

Retrieval Pipeline

Source: app/services/retriever.py. Public entry point: retrieve(query, top_k=3).

Stages

Stage What it does Detail
1. Semantic search Dense vector search in ChromaDB Embedding model all-mpnet-base-v2; top 10; score = 1 - distance (the collection uses Chroma's default L2 distance)
2. BM25 search Keyword search BM25Okapi over whitespace-split, lower-cased text; top 10; zero-score hits dropped
3. Fusion Reciprocal Rank Fusion 0.6 / (60 + rank_semantic) + 0.4 / (60 + rank_bm25); top 15 pass on
4. Re-ranking Cross-encoder cross-encoder/ms-marco-MiniLM-L-6-v2 scores each (query, chunk) pair; best top_k returned

The result for each chunk is {text, source, authority, domain}; internal scores are removed before the text reaches the model.

Chunks returned per endpoint: /query 3, /learn/structured 6, /learn/text 10.

Why hybrid

Dense retrieval alone handles tables poorly: the food-composition document (209 of 810 chunks) is mostly numeric tables. Keyword matching recovers exact terms such as food names. The cross-encoder then fixes the ordering. Earlier measurements that motivated this are in Evaluation (historical).

Things to know

  • Index lifetime. The BM25 index is built in memory from the whole collection at first use (or at startup) and is never refreshed. After re-ingesting documents, restart the server.
  • Collection choice. The first collection returned by ChromaDB is used. The expected collection is nutrition_kb.
  • Source name bug. The vector store stores each chunk's file name under source_document. The semantic path reads that key, but the BM25 path reads source, so a chunk found only by keyword search is reported with source="unknown". Chunks found by semantic search, or by both, are correct (Known Issues and Limitations).
  • Tuning. Candidate counts (10, 10, 15), the RRF weights and constant (0.6, 0.4, 60) and the model names are constants in the file, not environment variables.
  • Missing store. If app/chroma_storage/ has no collection, retrieval raises No ChromaDB collections found. Build it first (Getting Started).

Clone this wiki locally