Retrieval-Augmented Generation with Bedrock Knowledge Bases
A foundation model does not know your company’s policies, products or tickets – and when it doesn’t know, it may hallucinate. Retrieval-augmented generation (RAG) fixes this by retrieving relevant passages from your documents at question time and asking the model to answer only from them, with citations.
The RAG pipeline
flowchart LR
subgraph Ingestion["Ingestion (when documents change)"]
D[(S3 documents)] --> P[Parse] --> C[Chunk] --> E[Embed] --> V[(Vector store)]
end
subgraph Query["Query (every question)"]
Q[User question] --> QE[Embed question] --> R["Retrieve top-k similar chunks"]
V --> R
R --> G["Prompt = instructions + chunks + question"] --> LLM[Foundation model] --> A["Answer + citations"]
end
| Step | What happens |
|---|---|
| Parse | Extract text from PDF, Word, HTML, Markdown… (advanced parsing for tables/images) |
| Chunk | Split into passages – fixed-size with overlap, hierarchical, semantic, or none |
| Embed | Convert each chunk to a vector (e.g. Titan Text Embeddings V2, 1024 dimensions) |
| Store | Save vectors + text + metadata in a vector store |
| Retrieve | Embed the question, find nearest chunks by cosine similarity |
| Generate | Model answers from the retrieved chunks and cites sources |
Bedrock Knowledge Bases
A fully managed RAG service:
| Setting | Options |
|---|---|
| Data source | Amazon S3, web crawler, Confluence, SharePoint, Salesforce… |
| Chunking | Default (~300 tokens, 20 % overlap), fixed-size, hierarchical, semantic, none |
| Embeddings model | Titan Text Embeddings V2, Cohere Embed |
| Vector store | OpenSearch Serverless (quick create), Amazon S3 Vectors, Aurora PostgreSQL (pgvector), Pinecone, Redis… |
| Sync | Re-ingest after documents change (incremental) |
APIs: Retrieve (chunks + scores – build your own prompt) and RetrieveAndGenerate (managed answer with citations).
import { BedrockAgentRuntimeClient, RetrieveAndGenerateCommand } from '@aws-sdk/client-bedrock-agent-runtime';
const kb = new BedrockAgentRuntimeClient({});
const r = await kb.send(new RetrieveAndGenerateCommand({
input: { text: 'How many days of annual leave do I get?' },
retrieveAndGenerateConfiguration: {
type: 'KNOWLEDGE_BASE',
knowledgeBaseConfiguration: { knowledgeBaseId: 'KB12345678', modelArn: 'arn:aws:bedrock:ap-south-1::foundation-model/amazon.nova-lite-v1:0' },
},
}));
console.log(r.output.text, r.citations);
Getting good results
- Chunk size: too large dilutes relevance; too small loses context. Use overlap.
- Metadata filters (department, year, product) narrow retrieval.
- Hybrid search combines keyword and semantic search; reranking improves top results.
- Instruct the model: answer only from the context; if not present, say you don’t know.
- Show citations so users can verify.
- Keep data fresh with scheduled sync; respect access control per user.
Evaluating RAG
| Measure | Question |
|---|---|
| Retrieval quality | Did the right chunks come back (context relevance, recall)? |
| Faithfulness / groundedness | Is every claim supported by the retrieved text? |
| Answer relevance | Does it answer the question? |
| Citations | Do they point to the supporting source? |
RAG gives models fresh, private, citable knowledge without retraining. Quality depends more on documents, chunking and retrieval than on the model.
Practise it
Virtual lab · Amazon Web Services · 35 min
Build a RAG assistant with Bedrock Knowledge Bases
Turn HR policy documents in S3 into a retrieval-augmented generation (RAG) assistant: chunking, embeddings and a vector store, sync, retrieval scores, grounded answers with citations – and see the assistant decline questions its documents cannot answer.
Quick revision
- RAG = retrieve relevant chunks, then generate from them.
- Pipeline: parse → chunk → embed → store → retrieve → generate.
- Knowledge Bases manage ingestion, vector store and retrieval.
- Retrieve vs RetrieveAndGenerate APIs.
- Evaluate retrieval, faithfulness and citations.
Practise this topic
Hands-on virtual lab
Apply what you just read in a realistic, guided simulation. Free with a verified account.
Create a free account to track your progress and unlock virtual labs.