How to Make AI Answer Questions About Your Documents: A Practical RAG Guide
In-depth discussion
Easy to understand
0 0 1
The article introduces Retrieval-Augmented Generation (RAG) as a way to let AI answer questions over long documents without pasting them into the context window. It walks through a fictional company's onboarding docs and builds a pipeline with three steps: chunking documents with paragraph-level overlap, embedding chunks with Amazon Titan Embeddings V2, and retrieving top-K chunks via cosine similarity before generating answers with Claude on Amazon Bedrock. The author demonstrates a working ~60-line Python pipeline, then previews a failure case where retrieval succeeds but the answer fails due to context split across chunks.
main points
unique insights
practical applications
key topics
key insights
learning outcomes
• main points
1
Clear conceptual explanation of RAG as 'retrieval first, then generation', with each term (retrieval, augmented, generation) mapped to a concrete step
2
Complete, runnable code for each pipeline stage (chunking, embedding, retrieval, generation) that readers can adapt directly
3
Honest discussion of failure modes, including a concrete example where retrieval works but answers still fail, plus a useful diagnostic heuristic ('what chunk did it retrieve?')
• unique insights
1
Concrete illustration of why chunk overlap matters: a split chunk beginning with 'These days do not carry over' loses its referent, making the retrieved text meaningless on its own
2
Framing RAG as 'enterprise search with a language model at the end of the pipeline', connecting it to existing search technology rather than presenting it as entirely new
• practical applications
Readers get a reproducible blueprint for building document Q&A over private data with citations, along with guidance on choosing separate models for embedding (cheap, fast) versus generation (capable), and an early warning about retrieval-versus-answer failure modes that they will likely encounter.
Text embeddings and semantic search via cosine similarity
4
Grounded answer generation with source citation using Amazon Bedrock
• key insights
1
End-to-end working code for a document Q&A pipeline in roughly 60 lines of Python, using AWS Bedrock
2
Explicit separation of embedding and generation models with the rationale for each, plus a note that the same pattern works with local or other cloud providers
3
Early diagnosis of a realistic RAG failure mode (retrieval succeeds, answer fails due to split context), setting up a practical follow-up on fixing retrieval
• learning outcomes
1
Understand the three-stage RAG pipeline (chunk, embed, retrieve and generate) and why retrieval is needed before generation
2
Implement a basic document Q&A system with paragraph-level chunking, Titan embeddings, cosine-similarity retrieval, and Claude-based answer generation on Bedrock
3
Diagnose RAG failures by checking which chunks were retrieved, rather than assuming the model is at fault
A 2,000-page handbook cannot fit into a model's context window in a usable way. Even when it fits, models lose attention to material in the middle of long inputs and may answer confidently from the wrong section. The solution is a step before generation: find the few paragraphs that answer the question and pass only those to the model. That step is retrieval, and the full technique is Retrieval-Augmented Generation: search first, then generate.
“ What RAG Means: Retrieval, Augmentation, Generation
Retrieval means locating relevant information, much like checking a textbook index before reading a chapter. Augmentation means adding that retrieved text to the prompt, supplementing the model's built-in knowledge with specific context. Generation means the model writes its answer while the retrieved evidence sits in the conversation, so the answer is grounded in the actual data rather than recalled from training. In one sentence: find the right chunks, put them in the prompt, and let the model answer from them.
“ How RAG Relates to Traditional Enterprise Search
Search tools such as Elasticsearch, Kendra, and SharePoint search have located relevant passages for decades. The retrieval step is not new. What RAG adds is a language model at the end of the pipeline, which reads the retrieved evidence and writes an answer instead of returning a results page for the user to read. RAG can be understood as enterprise search with a language model at the end.
“ The Example Setup: Onboarding Documents for a Fictional Company
The example uses PineRidge Solutions, a fictional company whose six onboarding documents cover the employee handbook, benefits, leave policy, expenses, engineering onboarding, and IT security. The system should answer questions like 'how many vacation days do I get?' by finding the right section. The build uses Kiro IDE and Amazon Bedrock, but the same pattern works with any embeddings model, local or cloud, such as Ollama, OpenAI, or Cohere. The full code is in the author's GitHub repository.
“ Step 1: Chunking Documents with Overlap
Documents are split into paragraph-based chunks so that retrieval returns a relevant section rather than an entire file. Chunk size involves a trade-off: too large reintroduces the context problem, too small can cut an answer in half. Overlap matters: if a sentence like 'These days do not carry over' is split from the sentence defining which days, the chunk loses its referent. Including one paragraph of overlap from the preceding chunk keeps each chunk self-contained. The chunking function loops over markdown files, splits on blank lines, and stores each chunk's text with its source filename for citations. The example yields roughly 150 chunks.
“ Step 2: Turning Chunks into Embeddings
An embedding converts text into a vector, a list of numbers placed in a space where distance reflects meaning. Similar chunks land near each other even when their wording differs, so 'parental leave top-up' and 'salary during maternity leave' are numerically close. The embeddings model is separate from the generation model: it is small and fast and only produces searchable numbers. The example calls Amazon Titan Embeddings V2 through Bedrock to produce 1024-dimensional vectors. For this demo the vectors are cached to a local file; production systems can use Amazon S3 Vectors, OpenSearch Serverless, pgvector, or Bedrock Knowledge Bases.
“ Step 3: Retrieval via Cosine Similarity
At query time the question is embedded with the same model, placing it in the same number space as the chunks. The retrieve function computes cosine similarity between the question vector and every chunk vector, where 1 means identical and 0 means unrelated, sorts the scores, and returns the top three chunks. This is semantic search: a query about 'working from home rules' matches a handbook section titled 'remote work policy' because the meanings are close.
“ Step 4: Generating Grounded Answers with Claude
The generation function labels each retrieved chunk with its source file, builds a prompt instructing the model to use only the supplied context and to say so if the answer is absent, and calls Claude through Bedrock's Converse API. In the example, asking about the RRSP matching policy returned a grounded answer: dollar-for-dollar matching up to 5% of base salary, starting after 90 days, with a vesting schedule, all traceable to specific chunks that serve as citations. The pipeline is chunk, embed, retrieve, generate, in about 60 lines of Python.
“ Where RAG Fails: When Context Is Split Across Chunks
Retrieval can succeed while the answer fails. Asking 'How many vacation days do I get as a senior engineer?' retrieved the correct vacation table, but the model could not map 'senior engineer' to a job level, because that mapping lived in a different chunk. The model was not hallucinating; it honestly reported missing evidence. When a RAG system gives a bad answer, the first question is which chunks were retrieved, not why the model is wrong.
“ Key Takeaways and What Comes Next
For newcomers: RAG lets AI answer questions about your documents without pasting them into a chat. It searches first, then answers from what it finds, in three steps of chunking, embedding, and retrieval, and the model never sees the full document. For builders: RAG is a pipeline whose chunking strategy, embedding model, retrieval method, and generation model each independently affect quality, and different models can serve different jobs, such as Titan for fast cheap search and Claude for generation. The next post addresses chunks that are too small, answers spread across sections, and retrieval that succeeds while answers still fail, covering the strategies that make retrieval reliable.
We use cookies that are essential for our site to work. To improve our site, we would like to use additional cookies to help us understand how visitors use it, measure traffic to our site from social media platforms and to personalise your experience. Some of the cookies that we use are provided by third parties. To accept all cookies click ‘Accept’. To reject all optional cookies click ‘Reject’.
Comment(0)