April 22, 2025
The Power of Contextual AI: Enhancing Foundation Models with External Knowledge
Imagine a student taking an exam. Limited to what they’ve memorized, their answers might be incomplete or inaccurate. Now picture that same student with access to their textbooks and notes. They can verify facts, make detailed connections, and develop deeper insights. This is the perfect analogy for Retrieval-Augmented Generation (RAG).
%20%201ddd6bbdbbea80ab97cbc6461ed249d6/ChatGPT_Image_Apr_22_2025_06_00_35_PM.png)
%20%201ddd6bbdbbea80ab97cbc6461ed249d6/ChatGPT_Image_Apr_22_2025_06_01_46_PM.png)
Foundation Models (FMs) have limitations. They’re trained on outdated data, can generate incorrect information, and lack specialized knowledge. RAG solves these issues by connecting FMs to external knowledge sources. This turns them from memory-based systems into intelligent agents that can access current, comprehensive information.
Think of RAG as giving your AI system access to a constantly updated library. It can fact-check, research, and synthesize information in real time. This dynamic knowledge access transforms traditional AI models into more capable and reliable systems. Instead of relying solely on their training data, these enhanced models can now reference specific documents, databases, and other sources to provide precise, contextual responses.
RAG systems prevent hallucinations by grounding their responses in external knowledge sources. Rather than depending exclusively on training data, these systems actively fact-check and reference specific documents and databases. This results in precise, contextual responses that are more trustworthy and less prone to fabrication.
This chapter explores how Amazon Bedrock Knowledge Bases enable you to harness RAG’s potential, making your AI applications more accurate, relevant, and trustworthy. The goal isn’t just to create smarter AI but to build systems people can depend on.
Understanding RAG: Bridging the Knowledge Gap
Why Foundation Models Need Help
Despite their impressive capabilities, Foundation Models suffer from three critical limitations that affect real-world applications:
- Knowledge Cutoff: FMs remain unaware of events and developments after their training period. Ask about recent quantum computing breakthroughs, and they can only reference outdated information.
- Hallucinations: When FMs lack context or proper information, they sometimes confidently generate incorrect or nonsensical responses—basically inventing information to fill knowledge gaps.
- Domain Limitations: While FMs possess broad knowledge, they often struggle with specialized queries about specific medical conditions, proprietary engineering processes, or company-specific policies.
How RAG Works: The Four-Stage Pipeline
RAG solves these problems through a four-stage process that connects FMs to external knowledge:
- Data Ingestion: The system collects and prepares data from various sources—documents, databases, web pages—cleaning and transforming it into a suitable format.
- Indexing: The prepared data is converted into vector embeddings—numerical representations that capture semantic meaning—and stored in specialized vector databases like Amazon OpenSearch Service or Pinecone.
- Retrieval: When a user asks a question, the system converts it into a vector and identifies the most relevant documents through similarity searches, potential filtering, and reranking.
- Generation: The retrieved documents augment the prompt given to the FM, providing the context needed to generate an accurate, informed response.
This approach dramatically improves AI performance. For instance, if asked “What were the main announcements at AWS re:Invent 2023?”, a traditional FM would be limited by its training data. With RAG, the system can access the latest information about the event and deliver a comprehensive, up-to-date response.
Building Your Knowledge Base with Amazon Bedrock
Creating an effective RAG system starts with establishing a robust knowledge base. Amazon Bedrock simplifies this process with a managed service that handles the indexing, retrieval, and augmentation aspects of RAG.
Setting Up Your Knowledge Base
The process begins in the Amazon Bedrock console:
- Creation and Configuration: Give your Knowledge Base a descriptive name and purpose—for example, “CustomerSupportKB” for a collection of support documents.
- Data Source Selection: Choose where your data lives. Most commonly, this is an Amazon S3 bucket containing documents in formats like PDF, TXT, or CSV.
- Embedding Model Selection: Pick the embedding model that will convert your text into vector representations. This choice influences search quality and cost.
- Vector Store Choice: Select the database where your embeddings will be stored. Options include:
- Amazon OpenSearch Service (tightly integrated with AWS)
- Pinecone (performance-focused)
- Redis Enterprise Cloud (excellent speed)
- Aurora PostgreSQL (cost-effective)
- Amazon Kendra (intelligent search capabilities)
Each option offers different trade-offs between cost, performance, and features—consider your specific needs when making this choice.
Automation for Seamless Data Flow
To keep your Knowledge Base current, implement automated data ingestion:
- S3 Event Triggers: Configure Lambda functions to process new documents automatically when they’re uploaded to S3.
- Scheduled Updates: Use Lambda to periodically scan for new or updated files.
For optimal retrieval, consider your chunking strategy—how documents are divided for indexing:
- Fixed-Size Chunks: Simple but may break semantic units
- Semantic Chunks: Preserve meaning but require more complex logic
- Sliding Window: Create overlapping segments to maintain context
The right strategy balances granularity with contextual preservation. Smaller chunks offer precision but may miss broader meaning; larger chunks provide more context but might include irrelevant information.
The Art of Data Preparation and Vectorization
RAG systems are only as good as their data. Like a chef preparing ingredients before cooking, proper data preparation ensures your system can find the most relevant information when needed.
Cleaning Text for Optimal Performance
Raw text often contains noise that can degrade RAG performance. Effective preparation includes:
- Removing Irrelevant Elements: Stripping HTML tags, special characters, and extraneous formatting
- Normalization: Converting text to consistent formats through techniques like lowercasing and lemmatization
- Stop Word Removal: Filtering out common words that add little semantic value
Libraries like NLTK, spaCy, or custom Python functions can handle these tasks efficiently:
def preprocess_text(text):
# Remove HTML tags
text = re.sub(r'<[^>]+>', '', text)
# Convert to lowercase
text = text.lower()
# Tokenize and remove stop words
tokens = [w for w in text.split() if w not in stop_words]
# Lemmatize tokens
lemmatizer = WordNetLemmatizer()
tokens = [lemmatizer.lemmatize(w) for w in tokens]
return ' '.join(tokens)
Converting Text to Numerical Representations
The heart of RAG lies in vector embeddings—numerical representations that capture semantic meaning. Modern RAG systems typically use transformer-based models like:
- BERT: Creates context-aware word embeddings
- Sentence Transformers: Optimized for sentence and paragraph-level embeddings
For Amazon Bedrock implementations, you can leverage embedding models through the Bedrock API:
response = bedrock.invoke_model(
modelId='cohere.embed-english-v3',
body=json.dumps({"texts": texts})
)
embeddings = json.loads(response['body'].read())['embeddings']
These embeddings enable the system to find semantically similar content—not just keyword matches—dramatically improving retrieval relevance.
The Retrieval Process: Finding What Matters
With your data prepared and vectorized, the next challenge is retrieving the most relevant information for each query. This involves sophisticated similarity searches, potential reranking, and prompt augmentation.
Similarity Search Techniques
When a user submits a query, the system:
- Converts the query to a vector embedding
- Searches for the closest document vectors using techniques like:
- Nearest Neighbor Search (exact but potentially slow)
- Approximate Nearest Neighbor Search (faster but slightly less precise)
- Cosine Similarity (measures vector orientation regardless of magnitude)
Vector databases optimize these searches using specialized indexes and algorithms. For example, HNSW (Hierarchical Navigable Small World) graphs enable efficient navigation through high-dimensional vector spaces.
Improving Accuracy with Reranking
Initial retrieval results may not perfectly match user intent. Reranking models address this by applying more sophisticated relevance assessment:
- Cross-Encoders: Transformer models that directly compare query-document pairs for fine-grained relevance scoring
- Semantic Reranking: Uses more powerful models to evaluate contextual relevance
This creates a two-stage process—fast initial retrieval followed by more computationally intensive reranking of promising candidates—balancing speed with accuracy.
Prompt Augmentation: The Final Step
The retrieved documents must be incorporated into the prompt for the FM. Effective prompt augmentation strategies include:
- Clear Formatting: Explicitly separating retrieved content from the query
- Metadata Inclusion: Adding source, date, or author information for context
- Instructional Clarity: Guiding the FM on how to use the retrieved information
A well-structured prompt might look like this:
User Query: What are the main causes of climate change?
Retrieved Document 1:
Source: IPCC Report
Date: 2021
Content: The main causes of climate change are greenhouse gas emissions from human activities, such as burning fossil fuels and deforestation.
Based on the provided documents, answer the user's query.
The quality of this augmentation significantly impacts the final generation, making careful prompt engineering essential for RAG success.
Embracing Diverse Data Sources
Real-world knowledge isn’t confined to a single format. To build truly comprehensive AI systems, your Knowledge Base should integrate various data types—both unstructured and structured.
Working with Unstructured Data
Unstructured data like PDFs, text files, and web pages requires specialized processing:
- PDF Extraction: Libraries like PyPDF2, pdfminer.six, or PyMuPDF can extract text from documents:
def extract_text_from_pdf(pdf_path):
text = ""
with open(pdf_path, 'rb') as file:
reader = PyPDF2.PdfReader(file)
for page in reader.pages:
text += page.extract_text()
return text
- Web Scraping: Tools like Beautiful Soup can extract content from websites:
def scrape_webpage_text(url):
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')
paragraphs = soup.find_all('p')
text = '\n'.join([p.get_text() for p in paragraphs])
return text
Integrating Structured Data
Structured data from databases, spreadsheets, and APIs provides organized, searchable facts:
- Database Connections: Libraries like psycopg2 or SQLAlchemy enable database access:
def connect_to_postgres(db_name, user, password, host, port):
conn = psycopg2.connect(
dbname=db_name, user=user, password=password,
host=host, port=port
)
return conn
- API Integration: The requests library simplifies pulling data from APIs:
def fetch_json_from_api(api_url):
response = requests.get(api_url, timeout=10)
data = response.json()
return data
Combining Data for Holistic Knowledge
The real power comes from linking these diverse sources. Strategies include:
- Data Linking: Connecting unstructured and structured data through common identifiers
- Metadata Enrichment: Tagging documents with structured data attributes
- Prioritization: Weighting sources based on relevance, recency, and reliability
For example, a customer support system might link support tickets (unstructured) with account details (structured) using customer IDs, giving the AI assistant complete context for addressing queries.
Advanced PDF Processing: Amazon Bedrock vs. Unstructured
Many enterprise documents exist as PDFs, presenting unique challenges for RAG systems. Amazon Bedrock offers several approaches to PDF processing:
- Default Parser: Extracts plain text at no additional cost—suitable for simple documents
- Bedrock Data Automation: A managed service processing multimodal content including text, tables, and images
- Foundation Model Parsing: Uses multimodal FMs like Claude to handle complex layouts and tables
For comparison, the open-source Unstructured library specializes in document preprocessing with features like:
- Layout-aware parsing that preserves document structure
- Excellent table extraction capabilities
- Native OCR support for scanned documents
Each approach offers different trade-offs between ease of use, scalability, and customization. Bedrock excels in enterprise-grade, secure implementations, while Unstructured provides greater flexibility for custom pipelines.
Putting It All Together: Building Your RAG Application
With all components in place, you can now implement a complete RAG system using Amazon Bedrock:
def rag_query(query, knowledge_base_id, model_id="anthropic.claude-v2"):
body = json.dumps({"text": query})
response = bedrock_runtime.invoke_model(
body=body,
modelId=model_id,
contentType="application/json",
accept="application/json",
knowledgeBaseId=knowledge_base_id,
retrievalConfiguration={
"vectorSearchConfiguration": {
"numberOfResults": 3
}
}
)
response_body = json.loads(response['body'].read())
results = response_body['results']
return results
This function queries your Knowledge Base, retrieves relevant documents, and enables the FM to generate an informed response based on that context.
Conclusion: The Future of Contextual AI
Retrieval-Augmented Generation represents a significant advancement in AI technology, addressing the fundamental limitations of Foundation Models by connecting them to external knowledge sources. With Amazon Bedrock Knowledge Bases, you can build AI applications that are:
- More accurate: Grounded in factual, up-to-date information
- More relevant: Tailored to specific domains and use cases
- More trustworthy: Less prone to hallucinations and fabrications
As AI becomes increasingly integrated into business processes and decision-making, these improvements in reliability and contextual awareness are not just technical enhancements. They’re important for building systems people can truly depend on.
The techniques and approaches covered in this chapter provide a foundation for implementing effective RAG systems. By carefully preparing your data, choosing appropriate vector embeddings, optimizing retrieval strategies, and crafting effective prompts, you can unlock the full potential of contextual AI for your organization.
This is just the beginning. As RAG technologies evolve, we can expect even more sophisticated approaches to knowledge integration, retrieval, and generation. This will further blur the line between memorized and retrieved knowledge, creating AI systems that can access, understand, and apply information with increasingly human-like capabilities.
If you like this article, try out this chapter in this book.
About the Author
Rick Hightower is an accomplished technical expert and thought leader in artificial intelligence and software engineering. With extensive experience in implementing enterprise-scale AI solutions, he specializes in RAG systems, machine learning, and cloud technologies.
As a prolific writer and educator, Rick shares his expertise through detailed technical articles and tutorials, helping developers and architects understand complex AI concepts and implement cutting-edge solutions. His work focuses particularly on practical applications of AI technologies in business environments.
Rick currently helps organizations leverage AI and cloud technologies to solve real-world problems, with a particular emphasis on Amazon Bedrock, large language models, and enterprise knowledge management systems.
TweetApache Spark Training
Kafka Tutorial
Akka Consulting
Cassandra Training
AWS Cassandra Database Support
Kafka Support Pricing
Cassandra Database Support Pricing
Non-stop Cassandra
Watchdog
Advantages of using Cloudurable™
Cassandra Consulting
Cloudurable™| Guide to AWS Cassandra Deploy
Cloudurable™| AWS Cassandra Guidelines and Notes
Free guide to deploying Cassandra on AWS
Kafka Training
Kafka Consulting
DynamoDB Training
DynamoDB Consulting
Kinesis Training
Kinesis Consulting
Kafka Tutorial PDF
Kubernetes Security Training
Redis Consulting
Redis Training
ElasticSearch / ELK Consulting
ElasticSearch Training
InfluxDB/TICK Training TICK Consulting