What Shipping a RAG Assistant Taught Me About Retrieval
Lessons from building a support-docs assistant on Amazon Bedrock and OpenSearch, and the five research papers that changed how I think about retrieval, chunking, and evaluation.
Last quarter I built a question-answering assistant over our internal support docs. The stack was boring on purpose: documents in Amazon S3, Amazon Textract for the PDFs, Amazon Titan Text Embeddings v2 for vectors, Amazon OpenSearch Service for search, and Claude on Amazon Bedrock for generation. The first demo answered every question confidently, and roughly one answer in five was wrong. Not vague. Wrong, with citations attached.
The model wasn't the problem. My retrieval was.
The retriever is the product
When Lewis et al. introduced RAG in 2020, they described it as pairing a model's parametric memory with a non-parametric one: a retriever that decides what the generator gets to read [1]. That framing stuck with me. The generator can only be as good as the passages it sees, and every "hallucination" I traced during that project traced back to a missing chunk, a wrong chunk, or the right chunk drowned in noise.
So I stopped tuning prompts first and started tuning retrieval first.
Pure vector search is not the ceiling
My first version was semantic search only: embed the query, grab the k nearest chunks. It felt modern, and it regularly lost to the keyword baseline I kept around for comparison. I should have seen that coming. The BEIR benchmark showed BM25 stays a strong zero-shot baseline, and plenty of dense retrievers trained on one domain quietly fall behind on another [2].
The fix wasn't picking a winner; it was running both and fusing the results. OpenSearch Service supports hybrid queries that combine a match query with k-NN search and normalize the scores with reciprocal rank fusion:
response = client.search(
index="support-docs",
body={
"query": {
"hybrid": {
"queries": [
{"match": {"text": {"query": question, "operator": "and"}}},
{"knn": {"embedding": {"vector": query_vector, "k": 5}}},
]
}
},
"size": 5,
},
)The hybrid query needs a search pipeline with a normalization processor, but that's a one-time setup, and it produced the single biggest quality jump of the project.
Chunks are a design decision, and position matters
Chunking looks like plumbing but behaves like a model parameter. Chunks that are too small lose the context an answer needs. Chunks that are too big dilute the signal, and the DPR paper shows embedding quality depends heavily on how passages are shaped and what the encoder was trained on [3]. I settled on 300 to 500 token chunks with a little overlap, tuned against a labeled set rather than a blog post's recommendation.
The second surprise: even correct chunks get ignored. The Lost in the Middle study showed that models read information at the start and end of the context window far better than information buried in the middle [4]. Cutting top-k from 8 to 3 and putting the strongest chunk first beat adding more context. More passages didn't mean better answers, it meant more places for the model to lose the plot.
Evaluate faithfulness, not vibes
The change that made the assistant shippable was measurement. I used the metrics the Ragas paper formalizes: faithfulness (does the answer actually follow from the retrieved context), answer relevancy, and context recall [5]. A small nightly job samples real questions, runs the full pipeline, and grades the answers with a second Bedrock call:
grade_prompt = (
"You are checking a support answer for groundedness.\n"
f"Context:\n{context}\n\n"
f"Answer:\n{answer}\n\n"
"Reply with one word: GROUNDED if every claim in the answer "
"is supported by the context, otherwise HALLUCINATED."
)
verdict = bedrock.converse(
modelId="anthropic.claude-3-5-sonnet-20240620-v1:0",
messages=[{"role": "user", "content": [{"text": grade_prompt}]}],
)["output"]["message"]["content"][0]["text"]The generator prompt also got stricter: answer only from the given context, cite the chunk, and say "I don't know" when the context doesn't contain the answer. On the 120-question eval set we traced, faithfulness moved from the low 0.6s to above 0.85 once hybrid search and the stricter prompt were in. That number is why I could hand the assistant to the support team without holding my breath.
What I'd tell my past self
Build the eval set on day one, before writing much code. Keep both lexical and vector search, fusion is cheap. Keep top-k small and chunks human-readable. Treat faithfulness as a metric with a threshold, not a feeling. The model was the easy part.
References
- Lewis et al., 2020. Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks
- Karpukhin et al., 2020. Dense Passage Retrieval for Open-Domain Question Answering
- Liu et al., 2023. Lost in the Middle: How Language Models Use Long Contexts
- Es et al., 2023. Ragas: Automated Evaluation of Retrieval Augmented Generation
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article