RAG (Retrieval-Augmented Generation)
Goal: inject relevant source text into the LLM's context when generating —
reduces hallucination, supplies fresh/private knowledge.
Pipeline
- Load & chunk: split documents into ~300–800 token chunks with 10–20% overlap. Preserve semantic boundaries (paragraph/heading).
- Embed: turn each chunk into a vector with an embedding model (e.g. OpenAI
text-embedding-3-small, localbge/e5). - Store: write vectors to a vector store (sqlite-vec, FAISS, pgvector, Qdrant). Keep metadata (source, title) alongside.
- Retrieve: embed the query -> find the nearest k chunks (cosine). Typical k=4–8.
- Rerank (optional): rerank the top-k with a cross-encoder.
- Generate: pass the retrieved chunks to the prompt as sources; instruct the model to "answer only from the given context, else say you don't know".
Quality tips
- Chunk size is the most critical knob: too large -> noise, too small -> broken context.
- Hybrid search: combine vector + keyword (BM25); good for exact terms.
- Cite sources: state which chunk an answer came from (trust + auditability).
- Evaluate: measure "retrieval hit rate" and "answer correctness" over a QA set.
- In Milyonus the embedding layer is optional (
sqlite-vec); it is also used for dedup / negative-memory similarity.