Building Related Posts with Bedrock Knowledgebase and S3 Vectors
Finding related content that interests your readers can be challenging. The AWS Fundamentals Blog has over 100 posts, but readers need to know what they are looking for. A common way to solve this problem is to show other posts related to the current one. Today, we will explore exactly how to do this using AWS Bedrock Knowledgebase and S3 Vectors.
Architecture: Bedrock and S3 Vectors
The overall architecture consists of just two services:
- Bedrock: For building a Knowledge Base.
- S3: For Storage and Vectors.
Bedrock for Building a Knowledge Base
Bedrock is the main service used for building AI products on AWS. It gives access to foundational models and developer tools. Initially, it might seem complex, but it is described as "The easiest way to build and scale generative AI applications".
S3 for Storing Content and Vectors
Besides classical S3 storage, we store our vector index in the newly announced S3 Vectors service. This is one of the most exciting "AI" launches from AWS recently.
Step 1 - Preparing and Uploading Content to S3
The first thing we need is content. We use 100+ blog posts as an example. All posts are in Markdown format, and each has a "frontmatter" with metadata (title, date, SEO description, slug, tags, etc.).
We upload this content to a normal S3 bucket. We add this information to the metadata of each S3 object (like Cover, Date Published, Service, Tags, Title). This metadata will be very useful later for filtering results.
Step 2 - Creating a Knowledge Base in Bedrock
Now, we will create a Knowledge Base in Bedrock. Since we want to use S3 as a vector store, we do this manually in the console (Terraform and Pulumi might not fully support it yet).
- Go to Knowledge Bases in the Bedrock console.
- Click "Create" and then "Knowledge Base with vector store".
- Give the Knowledge Base a name (e.g.,
kb-blog-posts). - Select S3 as the data source.
Important: Don't forget to enable "Log delivery" to CloudWatch. Otherwise, you won't get information about sync errors.
Step 3 - Data Source and Chunking
In vector stores, a numerical representation (vector) of the content is stored. Since a post is too large to fit into a single number, we need to split the post into chunks.
For example, instead of taking the whole post, we take 2000-character chunks, "embed" them with an AI model, and put them into the vector store.
In Bedrock settings:
- Chunking Strategy: We choose "Fixed-size chunking" for simplicity.
- Max Tokens: 2000
- Overlap Percentage: 10%
Step 4 - Select Model and Vector Store
We need to select a model. We use the Titan Text Embeddings V2 model.
We select S3 as the vector store.
Note: The S3 Vectors service might still be in preview. Be cautious for production workloads.
Step 5 - Sync Your Content
Once the Knowledge Base is created, go to the data source and start the Sync process. You can follow the progress via CloudWatch.
Step 6 - Test Your Knowledge Base
You can test your system using the "Test Knowledge Base" feature in the Bedrock console. By selecting "Retrieval only", you can see only the chunks coming from your data source.
For example, if you ask "How expensive is CloudFront?", chunks from the relevant pricing article should return.
Step 7 - Generate Related Posts and Store in JSON
The final step is to actually generate the related posts. Our approach is to use a script that runs at build time to find related content for all posts and store it in a JSON file (generated-posts.json).
This approach is cost-effective because we only call the Bedrock API and pay when files change. Also, it increases page load speed because the data is pre-prepared.
Example JSON output:
{
"aws-lambda-guide": [
{
"slug": "serverless-architecture",
"title": "Introduction to Serverless Architecture",
"similarity": 0.58,
"service": "lambda"
},
{
"slug": "aws-cost-optimization",
"title": "AWS Cost Optimization",
"similarity": 0.55,
"service": "cost"
}
]
}
Improvement Opportunities
This system can be improved:
- Better chunking: Using hierarchical chunking.
- Better data preparation: Creating a good summary for each post.
- Better queries: Improving the query.
- Metadata filtering: First finding the service, then searching within chunks of that service.
Frequently Asked Questions (FAQ)
1. What is the cost of using Bedrock Knowledge Base?
The cost depends on the selected model, the amount of data stored, and the API calls made. Using S3 Vectors can optimize storage costs.
2. Is S3 Vectors ready for production?
At the time of writing, S3 Vectors is in preview, so caution should be exercised for critical workloads.
3. Which embedding models can I use?
Bedrock offers various models like Titan Text Embeddings. You can choose the one that best fits your needs and budget.
For more information, you can check out our AWS Consultancy and Kubernetes Consultancy services.
Source: https://awsfundamentals.com/blog/building-related-posts-with-bedrock-knowledgebase-and-s3-vectors