This version is still in development and is not considered stable yet. For the latest snapshot version, please use Spring AI 1.1.3!spring-doc.cn

Amazon Bedrock Knowledge Base

This section walks you through setting up the Amazon Bedrock Knowledge Base VectorStore to perform similarity searches against a pre-configured Knowledge Base.spring-doc.cn

Amazon Bedrock Knowledge Bases is a fully managed RAG (Retrieval-Augmented Generation) capability that allows you to connect foundation models to your data sources. Unlike other vector stores, Bedrock Knowledge Base handles document ingestion, chunking, and embedding internally.spring-doc.cn

Prerequisites

  1. AWS Account with Bedrock access enabledspring-doc.cn

  2. A configured Bedrock Knowledge Base with at least one data source syncedspring-doc.cn

  3. AWS credentials configured (via environment variables, AWS config file, or IAM role)spring-doc.cn

This vector store is read-only. Documents are managed through the Knowledge Base’s data source sync process, not through the add() or delete() methods.spring-doc.cn

Auto-configuration

Spring AI provides Spring Boot auto-configuration for the Bedrock Knowledge Base Vector Store. To enable it, add the following dependency to your project’s Maven pom.xml file:spring-doc.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-starter-vector-store-bedrock-knowledgebase</artifactId>
</dependency>

or to your Gradle build.gradle build file:spring-doc.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-bedrock-knowledgebase'
}
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Unlike other vector stores, Bedrock Knowledge Base does not require an EmbeddingModel bean. The Knowledge Base handles embeddings internally during data source synchronization.spring-doc.cn

To connect to your Knowledge Base, provide the Knowledge Base ID via Spring Boot’s application.properties:spring-doc.cn

spring.ai.vectorstore.bedrock-knowledge-base.knowledge-base-id=YOUR_KNOWLEDGE_BASE_ID
spring.ai.vectorstore.bedrock-knowledge-base.region=us-east-1

Or via environment variables:spring-doc.cn

export SPRING_AI_VECTORSTORE_BEDROCK_KNOWLEDGE_BASE_KNOWLEDGE_BASE_ID=YOUR_KNOWLEDGE_BASE_ID

Now you can auto-wire the Vector Store in your application:spring-doc.cn

@Autowired VectorStore vectorStore;

// ...

// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(
    SearchRequest.builder()
        .query("What is the return policy?")
        .topK(5)
        .build());

Configuration Properties

You can use the following properties in your Spring Boot configuration to customize the Bedrock Knowledge Base vector store.spring-doc.cn

Property Description Default value

spring.ai.vectorstore.bedrock-knowledge-base.knowledge-base-idspring-doc.cn

The ID of the Bedrock Knowledge Base to queryspring-doc.cn

-spring-doc.cn

spring.ai.vectorstore.bedrock-knowledge-base.regionspring-doc.cn

AWS region for the Bedrock servicespring-doc.cn

SDK defaultspring-doc.cn

spring.ai.vectorstore.bedrock-knowledge-base.top-kspring-doc.cn

Number of results to returnspring-doc.cn

5spring-doc.cn

spring.ai.vectorstore.bedrock-knowledge-base.similarity-thresholdspring-doc.cn

Minimum similarity score (0.0 to 1.0)spring-doc.cn

0.0spring-doc.cn

spring.ai.vectorstore.bedrock-knowledge-base.search-typespring-doc.cn

Search type: SEMANTIC or HYBRIDspring-doc.cn

null (KB default)spring-doc.cn

spring.ai.vectorstore.bedrock-knowledge-base.reranking-model-arnspring-doc.cn

ARN of Bedrock reranking modelspring-doc.cn

null (disabled)spring-doc.cn

Search Types

Bedrock Knowledge Base supports two search types:spring-doc.cn

  • SEMANTIC - Vector similarity search only (default)spring-doc.cn

  • HYBRID - Combines semantic search with keyword searchspring-doc.cn

HYBRID search is only available with OpenSearch-based vector stores. S3 Vectors, Aurora PostgreSQL, and other vector store types only support SEMANTIC search.spring-doc.cn

spring.ai.vectorstore.bedrock-knowledge-base.search-type=HYBRID

Reranking

You can improve search relevance by enabling a Bedrock reranking model:spring-doc.cn

spring.ai.vectorstore.bedrock-knowledge-base.reranking-model-arn=arn:aws:bedrock:us-west-2::foundation-model/amazon.rerank-v1:0

Available reranking models:spring-doc.cn

  • Amazon Rerank 1.0 - Available in us-west-2, ap-northeast-1, ca-central-1, eu-central-1spring-doc.cn

  • Cohere Rerank 3.5 - Requires AWS Marketplace subscriptionspring-doc.cn

Metadata Filtering

You can leverage the generic, portable metadata filters with the Bedrock Knowledge Base store.spring-doc.cn

For example, you can use the text expression language:spring-doc.cn

vectorStore.similaritySearch(
    SearchRequest.builder()
        .query("travel policy")
        .topK(5)
        .similarityThreshold(0.5)
        .filterExpression("department == 'HR' && year >= 2024")
        .build());

or programmatically using the Filter.Expression DSL:spring-doc.cn

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(
    SearchRequest.builder()
        .query("travel policy")
        .topK(5)
        .filterExpression(b.and(
            b.eq("department", "HR"),
            b.gte("year", 2024)).build())
        .build());

Supported Filter Operators

Spring AI Bedrock Description

EQspring-doc.cn

equalsspring-doc.cn

Equal tospring-doc.cn

NEspring-doc.cn

notEqualsspring-doc.cn

Not equal tospring-doc.cn

GTspring-doc.cn

greaterThanspring-doc.cn

Greater thanspring-doc.cn

GTEspring-doc.cn

greaterThanOrEqualsspring-doc.cn

Greater than or equalspring-doc.cn

LTspring-doc.cn

lessThanspring-doc.cn

Less thanspring-doc.cn

LTEspring-doc.cn

lessThanOrEqualsspring-doc.cn

Less than or equalspring-doc.cn

INspring-doc.cn

inspring-doc.cn

Value in listspring-doc.cn

NINspring-doc.cn

notInspring-doc.cn

Value not in listspring-doc.cn

ANDspring-doc.cn

andAllspring-doc.cn

Logical ANDspring-doc.cn

ORspring-doc.cn

orAllspring-doc.cn

Logical ORspring-doc.cn

NOTspring-doc.cn

(negation)spring-doc.cn

Logical NOTspring-doc.cn

Metadata filtering requires documents in your Knowledge Base to have metadata attributes. For S3 data sources, create .metadata.json files alongside your documents.spring-doc.cn

Manual Configuration

If you prefer to configure the vector store manually, you can do so by creating the beans directly.spring-doc.cn

Add this dependency to your project:spring-doc.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-bedrock-knowledgebase-store</artifactId>
</dependency>
Refer to the Dependency Management section to add the Spring AI BOM to your build file.

Sample Code

@Bean
public BedrockAgentRuntimeClient bedrockAgentRuntimeClient() {
    return BedrockAgentRuntimeClient.builder()
        .region(Region.US_EAST_1)
        .build();
}

@Bean
public VectorStore vectorStore(BedrockAgentRuntimeClient client) {
    return BedrockKnowledgeBaseVectorStore.builder(client, "YOUR_KNOWLEDGE_BASE_ID")
        .topK(10)
        .similarityThreshold(0.5)
        .searchType(SearchType.SEMANTIC)
        .build();
}

Then use the vector store:spring-doc.cn

List<Document> results = vectorStore.similaritySearch(
    SearchRequest.builder()
        .query("What are the company holidays?")
        .topK(3)
        .build());

for (Document doc : results) {
    System.out.println("Content: " + doc.getText());
    System.out.println("Score: " + doc.getScore());
    System.out.println("Source: " + doc.getMetadata().get("source"));
}

Accessing the Native Client

The Bedrock Knowledge Base Vector Store provides access to the underlying native client through the getNativeClient() method:spring-doc.cn

BedrockKnowledgeBaseVectorStore vectorStore = context.getBean(BedrockKnowledgeBaseVectorStore.class);
Optional<BedrockAgentRuntimeClient> nativeClient = vectorStore.getNativeClient();

if (nativeClient.isPresent()) {
    BedrockAgentRuntimeClient client = nativeClient.get();
    // Use the native client for Bedrock-specific operations
}

Limitations

  • Read-only: The add() and delete() methods throw UnsupportedOperationException. Documents are managed through the Knowledge Base’s data source sync process.spring-doc.cn

  • HYBRID search: Only available with OpenSearch-based vector stores.spring-doc.cn

  • Reranking availability: Model availability varies by AWS region.spring-doc.cn

Supported Data Sources

Bedrock Knowledge Base supports multiple data source types. The source location is included in document metadata:spring-doc.cn

Data Source Metadata Field Example

S3spring-doc.cn

sourcespring-doc.cn

s3://bucket/path/document.pdfspring-doc.cn

Confluencespring-doc.cn

sourcespring-doc.cn

confluence.example.com/page/123spring-doc.cn

SharePointspring-doc.cn

sourcespring-doc.cn

sharepoint.example.com/doc/456spring-doc.cn

Salesforcespring-doc.cn

sourcespring-doc.cn

salesforce.example.com/record/789spring-doc.cn

Web Crawlerspring-doc.cn

sourcespring-doc.cn

example.com/pagespring-doc.cn

Customspring-doc.cn

sourcespring-doc.cn

Custom document IDspring-doc.cn