Building Retrieval-Augmented Generation (RAG) with Amazon Bedrock

Learn how to implement Retrieval-Augmented Generation (RAG) using Amazon Bedrock with Python, embeddings, and vector databases for enterprise-grade AI search.

Building Retrieval-Augmented Generation (RAG) with Amazon Bedrock

Summary: Retrieval-Augmented Generation (RAG) blends document retrieval with AI model generation to produce accurate, grounded responses. Using Amazon Bedrock, you can implement scalable, secure RAG pipelines natively in AWS.

<h2>1. What is RAG?</h2>
<p><strong>RAG = Retrieval + Generation.</strong> It enriches generative models with factual context from a knowledge base. Instead of relying solely on model memory, RAG fetches relevant documents and uses them to answer queries more reliably.</p>
<h2>2. Architecture Overview</h2>
<ul>
<li><strong>Index:</strong> Create embeddings from documents and store them in a vector database.</li>
<li><strong>Retrieve:</strong> Query the vector database for similar embeddings.</li>
<li><strong>Generate:</strong> Feed retrieved context into a Bedrock foundation model (like Claude or Titan) for generation.</li>
</ul>
<div class="note">💡 <strong>Tip:</strong> Amazon Bedrock offers <em>Knowledge Bases</em>, a managed RAG service that handles embeddings, storage, and retrieval automatically.</div>
<h2>3. Tools You’ll Need</h2>
<ul>
<li><strong>Amazon Bedrock</strong> — to access LLMs (Claude, Titan, etc.)</li>
<li><strong>Pinecone / OpenSearch / Bedrock KB</strong> — for vector storage</li>
<li><strong>SentenceTransformers</strong> — to create embeddings (or use Bedrock embeddings)</li>
<li><strong>Boto3</strong> — to invoke Bedrock models via AWS SDK</li>
</ul>
<h2>4. Example: Python Implementation</h2>
<pre><code class="language-python"># pip install sentence-transformers pinecone-client boto3

from sentence_transformers import SentenceTransformer import pinecone, boto3, json, os

PINECONE_API_KEY = os.environ.get("PINECONE_API_KEY") PINECONE_ENV = "us-west1-gcp" INDEX_NAME = "my-docs" AWS_REGION = "us-east-1" BEDROCK_MODEL_ID = "anthropic.claude-v1"

1️⃣ Initialize models and clients

embed_model = SentenceTransformer("all-MiniLM-L6-v2") pinecone.init(api_key=PINECONE_API_KEY, environment=PINECONE_ENV) if INDEX_NAME not in pinecone.list_indexes(): pinecone.create_index(INDEX_NAME, dimension=embed_model.get_sentence_embedding_dimension(), metric="cosine") index = pinecone.Index(INDEX_NAME)

2️⃣ Create document embeddings

docs = [ {"id": "doc1", "text": "Amazon Bedrock supports multiple foundation models."}, {"id": "doc2", "text": "Vector databases enable semantic search using embeddings."} ]

embs = embed_model.encode([d["text"] for d in docs]).tolist() index.upsert([(docs[i]["id"], embs[i], {"text": docs[i]["text"]}) for i in range(len(docs))])

3️⃣ Query and retrieve

query = "How does Bedrock use embeddings?" q_emb = embed_model.encode([query])[0].tolist() res = index.query(vector=q_emb, top_k=2, include_metadata=True) context = "\n".join([m["metadata"]["text"] for m in res["matches"]])

4️⃣ Invoke Bedrock model

client = boto3.client("bedrock-runtime", region_name=AWS_REGION) prompt = f"Use the context below to answer:\n{context}\nQuestion: {query}" body = {"prompt": prompt, "max_tokens_to_sample": 300, "temperature": 0.3} response = client.invoke_model( modelId=BEDROCK_MODEL_ID, contentType="application/json", accept="application/json", body=json.dumps(body).encode("utf-8") ) print(json.loads(response['body'].read()))

<h2>5. Managed Alternative: Bedrock Knowledge Bases</h2>
<p>If you prefer not to handle embeddings and vector databases manually, Amazon Bedrock’s <strong>Knowledge Bases</strong> provide an integrated way to:</p>
<ul>
<li>Ingest documents directly from S3</li>
<li>Automatically generate embeddings</li>
<li>Perform semantic retrieval and generation in one API</li>
</ul>
<h2>6. Best Practices</h2>
<ul>
<li>Use <strong>chunk sizes</strong> of 200–500 tokens for document passages</li>
<li>Re-rank retrieved passages for better precision</li>
<li>Include source citations in model prompts</li>
<li>Secure secrets via AWS Secrets Manager</li>
<li>Monitor costs and token usage</li>
</ul>
<h2>7. Final Thoughts</h2>
<p>Amazon Bedrock simplifies enterprise-grade RAG by combining LLMs, embeddings, and managed retrieval. Whether you build it yourself or use Bedrock Knowledge Bases, RAG ensures your AI models stay <strong>factual, explainable, and grounded</strong>.</p>
<div class="cta">
<a href="blog.html">← Back to Blog Index</a>
</div>
← Back to Blog Index