Building a Document Q&A Assistant with Amazon Bedrock Knowledge Bases
Hypothetical scenario, created for learning purposes.
Building a Document Q&A Assistant with Amazon Bedrock Knowledge Bases
Hypothetical scenario, created for learning purposes.
The problem
Imagine Vale Seguros, a fictional insurance company with hundreds of policy PDFs and internal manuals. Support agents spend minutes searching for answers like "Is water damage from a burst pipe covered under Plan B?" Answers vary by agent, and documents get outdated.
The goal is to build an assistant that answers questions based on the company's own documents and shows where each answer came from.
Architecture
This pattern is called Retrieval-Augmented Generation (RAG). The model doesn't rely on memory alone. It first retrieves relevant passages, then writes an answer grounded in them.
- Amazon S3: stores the source documents (PDFs, Word files, text).
- Amazon Bedrock Knowledge Bases: splits documents into chunks, creates embeddings, and syncs them into a vector store.
- Vector store: for example, Amazon OpenSearch Serverless or Aurora PostgreSQL with pgvector.
- Foundation model on Bedrock: generates the final answer from the retrieved passages.
- Amazon API Gateway + AWS Lambda: expose the assistant to the support tool.
- Bedrock Guardrails: filter unwanted topics and sensitive data.
Step by step
1. Prepare the documents
s3://vale-seguros-docs/
├── policies/plan-a.pdf
├── policies/plan-b.pdf
└── manuals/claims-process.pdf
Document quality decides answer quality. Remove duplicates, keep one current version of each policy, and prefer text-based PDFs over scanned images.
2. Create the knowledge base
In the Bedrock console (or with infrastructure as code), create a knowledge base, point it to the S3 bucket, choose an embedding model and a vector store, then run a sync. Run it again whenever documents change.
Chunking is the setting that most affects results. Chunks that are too small lose context; chunks that are too large dilute relevance. Start with the defaults, test with real questions, and adjust.
3. Query it from Lambda
The RetrieveAndGenerate API handles retrieval and generation in one call:
import boto3
client = boto3.client("bedrock-agent-runtime")
KB_ID = "YOUR_KNOWLEDGE_BASE_ID"
MODEL_ARN = "arn:aws:bedrock:us-east-1::foundation-model/YOUR_MODEL_ID"
def handler(event, context):
question = event["question"]
resp = client.retrieve_and_generate(
input={"text": question},
retrieveAndGenerateConfiguration={
"type": "KNOWLEDGE_BASE",
"knowledgeBaseConfiguration": {
"knowledgeBaseId": KB_ID,
"modelArn": MODEL_ARN,
},
},
)
sources = [
ref["location"]
for c in resp.get("citations", [])
for ref in c.get("retrievedReferences", [])
]
return {"answer": resp["output"]["text"], "sources": sources}
Returning the sources lets agents verify the answer in the original document, which builds trust.
4. Add guardrails
Attach a Bedrock Guardrail to block off-topic requests (for example, legal advice), mask personal data such as ID numbers, and reduce harmful content. Guardrails are a safety layer, not a replacement for testing.
5. Evaluate before launch
Build a small test set of real questions with expected answers from the policy team. Check three things: Is the answer correct? Is it supported by the cited passage? Does the assistant say "I don't know" when the documents don't cover the question?
Lessons learned
- Garbage in, garbage out. Outdated or contradictory documents produce confusing answers. Fix the content first.
- Always show citations. Users trust, and can verify, answers that point to a source.
- Prefer "I don't know" over a confident guess. Instruct the model to answer only from retrieved context.
- Keep the index fresh. Automate re-sync when files change, for example with an S3 event and Lambda.
- Control access. The assistant should only retrieve documents the user is allowed to see. Plan metadata filtering and IAM permissions early.
- Watch the cost drivers. Vector store capacity, embedding volume, and model usage all add up. Set budgets and alarms.
Next steps
- Metadata filtering to restrict answers by product, region, or document version;
- Bedrock Agents when the assistant should also take actions, such as opening a claim ticket;
- Amazon CloudWatch dashboards for latency, error rates, and unanswered questions;
- A feedback button ("helpful / not helpful") to collect real data for improving the documents and the setup.
Conclusion
RAG lets you put a language model to work on your own knowledge without retraining anything. The technology is the easy part. The real work is curating good documents, measuring answer quality, and being honest about what the assistant doesn't know.
How are you grounding generative AI applications in your own data? Share your experience in the comments.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article