Post

Simple guide for RAG using Strands Agents and Amazon S3 Vectors

This article was originally published on the AWS Builder Center.

Intro

This guide demonstrates how to build a RAG (Retrieval-Augmented Generation) system using Strands Agents and Amazon’s latest S3 Vectors service. You’ll learn to set up AWS Bedrock Knowledge Base, create vector storage for document embedding, and implement metadata filtering for precise information retrieval. We’ll walk through the complete process from AWS infrastructure setup to building an intelligent agent that can query customer-specific documents using natural language. By the end, you’ll have a working RAG system that combines the power of Strands Agents with AWS’s managed AI services for scalable document search and question answering.

Pre-Requisites

Amazon Bedrock Model Access

Enable model access in AWS Console:

  • Navigate to Amazon Bedrock → Model access
  • Request access to Claude Sonnet and Titan Text Embeddings models
  • Wait for approval (usually instant for most models)

Amazon S3 Vector Bucket

Create S3 Vector bucket via AWS Console:

  • Navigate to Amazon S3 console → Vector buckets (in navigation pane)
  • Choose Create vector bucket
  • Vector bucket name: Enter unique name (3-63 chars, lowercase, numbers, hyphens only)
    • Example: my-rag-vector-bucket
  • Encryption: Choose encryption method
    • Don’t specify encryption type (uses SSE-S3 by default) recommended for simplicity
    • Or Specify encryption type for custom KMS keys
  • Create vector bucket

Note: S3 Vectors is in preview release. Vector buckets are optimized for storing and querying vector embeddings, not documents.

Amazon Bedrock Knowledge Base

Set up Knowledge Base via AWS Console:

  • Navigate to Amazon Bedrock → Knowledge bases → Create
  • Knowledge base details:
    • Name: rag-knowledge-base
    • Description: “RAG knowledge base for document search”
  • Data source configuration:
    • Select “Amazon S3” as data source
    • Choose your S3 bucket which will serve as data
    • Specify folder path (optional)
  • Embeddings model:
    • Select “Titan Text Embeddings G1 Text”
  • Vector database:
    • Choose “Quick create a new vector store”
  • Review and create (takes 5-10 minutes)
  • Sync data source after creation to index your documents

AWS Credentials

Configure AWS credentials locally:

1
2
aws configure
# Enter your AWS Access Key ID, Secret Access Key, and region

Solution

Now we’ll build a complete RAG system by uploading documents to our Knowledge Base, creating a Strands Agent with vector search capabilities, and implementing metadata filtering for precise document retrieval. This solution demonstrates how to query specific customer information from indexed documents using natural language.

Add Documents

  1. Upload your documents (PDF, TXT, etc.) to the S3 data source bucket created in step 3
  2. Add metadata JSON files alongside documents for filtering capabilities
  3. Sync the Knowledge Base in Bedrock console to index new documents
  4. Wait for sync completion before querying (status shows in console)
1
2
3
4
5
6
7
8
{    
    "metadataAttributes": {        
    "tag": "Loan",        
    "year": 2022,        
    "borrower": "John Doe",        
    "customer_id": "CUSTOMER_NO_1"
    }
}

Implementation

Install required dependencies:

Install Strands Agents SDK, tools, and AWS SDK for Python integration.

1
pip install strands-agents strands-agents-tools boto3 --quiet

Import libraries and set environment variables:

Configure AWS profile and Knowledge Base ID for the RAG system.

1
2
3
from strands import Agent, tool
import osimport boto3os.environ["AWS_PROFILE"] = "YOUR_AWS_CLI_PROFILE"
os.environ["KNOWLEDGE_BASE_ID"] = "KB_ID"

Create vector search tool: Define a custom tool that queries Bedrock Knowledge Base with metadata filtering.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@tool
def search_vector_db(query: str, customer_id: str) -> str:    
    """    
    Handle document-based, narrative, and conceptual queries using the unstructured 
    knowledge base.    
    Args:        
        query: A question about business strategies, policies, company information,
        or requiring document comprehension and qualitative analysis        
        customer_id: Customer identifier    
    Returns:        
    Formatted string response from the knowledge base    
    """
    
    region = os.environ.get("AWS_DEFAULT_REGION", "us-west-2")    
    kb_id = os.environ.get("KNOWLEDGE_BASE_ID")    
    bedrock_agent_runtime = boto3.client("bedrock-agent-runtime", region_name=region)    
    try:        
        retrieve_response = bedrock_agent_runtime.retrieve(
            knowledgeBaseId=kb_id,            
            retrievalQuery={"text": query},            
            retrievalConfiguration={                
                "vectorSearchConfiguration": {                    
                    "numberOfResults": 5,                    
                "filter": {"equals": {"key": "customer_id", "value": customer_id}}}
                }
        )
        
        # Format the response for better readability        
        results = []        
        for result in retrieve_response.get('retrievalResults', []):    
            content = result.get('content', {}).get('text', '')  
            
        if content:                
            results.append(content) 
        
        return "\n\n".join(results) if results else "No relevant information found."    
    except Exception as e:        
        return f"Error in unstructured data assistant: {str(e)}"

Initialize Strands Agent: Create an agent with the vector search tool and RAG-specific system prompt.

1
2
3
4
5
6
agent = Agent(
    tools=[search_vector_db], 
    system_prompt=f"You are a rag agent. When a user asks you a question you will 
        first check it in your knowledge base (if you can't answer it from the current 
        conversation memory)".
    )

Query the RAG system: Test the agent by asking for specific information that requires document retrieval.

1
result = agent("get me the lender details for CUSTOMER_NO_1")

Response

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
I'll help you retrieve the lender details for CUSTOMER_NO_1. I'll use the search_vector_db 
function to find the relevant information.
Tool #1: search_vector_db
Based on the search results, I can provide you with the following lender details 
for CUSTOMER_NO_1:
1. Borrower: SCHOOL SUPPLY (a Pennsylvania limited liability company)
2. Loan Amount: $1,300,000.00
3. Loan Documentation:   
    - Promissory Note dated as of the current date   
    - Mortgage and Security Agreement
4. Financial Covenants for the Guarantor:   
    - Maintain a Net Worth of at least $40,000,000   
    - Maintain Liquid Assets of at least $4,000,000
5. Reporting Requirements:   
    - Deliver Financial Covenants Certification within 30 days of each calendar year-end   
    - Provide annual financial statements within 90 days of each calendar year-en
    - Financial statements must be audited by an independent certified public accounting firm
6. Guaranteed Principal Amount:   
    - $1,050,000.00 when the unpaid principal balance is greater than $1,050,000.00  
    - Entire unpaid principal balance when it's equal to or less than $1,050,000.00
7. Title Insurance: Issued by Gotham Abstract & Settlement LLC

The lender has specific requirements and protections in place, including financial covenants 
and reporting obligations for the borrower and guarantor.

Is there anything specific about the lender details you would like me to elaborate on?

The agent successfully retrieved comprehensive lender details for CUSTOMER_NO_1 by intelligently querying the Knowledge Base with metadata filtering, demonstrating precise document search capabilities. Our RAG system seamlessly combined Strands Agents’ natural language processing with AWS Bedrock’s vector search to extract specific loan information, financial covenants, and reporting requirements from indexed documents. This implementation showcases how modern RAG architecture can transform complex document repositories into intelligent, queryable knowledge systems for enterprise applications.

References

AWS Services Documentation

Amazon Bedrock Knowledge Base: Create a knowledge base by connecting to a data source Amazon Bedrock Model Access: Access Amazon Bedrock foundation models Amazon S3 Vectors: Using S3 Vectors with other AWS services Amazon OpenSearch Serverless: Amazon OpenSearch Serverless

Additional Resources

Strands Agents: Official Documentation AWS CLI Configuration: Configuring the AWS CLI

This post is licensed under CC BY 4.0 by the author.