このバージョンはまだ開発中であり、まだ安定しているとは考えられていません。最新のスナップショットバージョンについては、Spring AI 1.1.5 を使用してください。

Amazon Bedrock ナレッジベース

このセクションでは、事前設定されたナレッジベースに対して類似性検索を実行するために、Amazon Bedrock ナレッジベース VectorStore を設定する手順について説明します。

Amazon Bedrock Knowledge Bases は、基盤モデルをデータソースに接続できるフルマネージド RAG(Retrieval-Augmented Generation)機能です。他のベクトルストアとは異なり、Bedrock ナレッジベースはドキュメントの取り込み、チャンク化、埋め込みを内部で処理します。

前提条件

  1. Bedrock へのアクセスが有効になっている AWS アカウント

  2. 少なくとも 1 つのデータソースが同期された構成済みの Bedrock ナレッジベース

  3. AWS 認証情報が設定されました (via environment variables, AWS config file, or IAM role)

このベクトルストアは読み取り専用です。ドキュメントは、add() または delete() 方式ではなく、ナレッジベースのデータソース同期プロセスを通じて管理されます。

自動構成

Spring AI は、Bedrock ナレッジベースベクトルストアの Spring Boot 自動構成機能を提供します。これを有効にするには、プロジェクトの Maven pom.xml ファイルに次の依存関係を追加してください。

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

または、Gradle build.gradle ビルドファイルに次の内容を追加します。

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-bedrock-knowledgebase'
}
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。

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

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

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:

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:

@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());

プロパティの構成

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

プロパティ 説明 デフォルト値

spring.ai.vectorstore.bedrock-knowledge-base.knowledge-base-id

The ID of the Bedrock Knowledge Base to query

-

spring.ai.vectorstore.bedrock-knowledge-base.region

AWS region for the Bedrock service

SDK デフォルト

spring.ai.vectorstore.bedrock-knowledge-base.top-k

Number of results to return

5

spring.ai.vectorstore.bedrock-knowledge-base.similarity-threshold

Minimum similarity score (0.0 1.0 へ)

0.0

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

検索タイプ: SEMANTIC or HYBRID

null (KB default)

spring.ai.vectorstore.bedrock-knowledge-base.reranking-model-arn

ARN of Bedrock reranking model

null (無効)

検索タイプ

Bedrock Knowledge Base supports two search types:

  • SEMANTIC - Vector similarity search only (default)

  • HYBRID - Combines semantic search with keyword search

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

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

Reranking

You can improve search relevance by enabling a Bedrock reranking model:

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

Available reranking models:

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

  • Cohere Rerank 3.5 - Requires AWS Marketplace subscription

メタデータフィルタリング

You can leverage the generic, portable metadata filters with the Bedrock Knowledge Base store.

For example, you can use the text expression language:

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

または、Filter.Expression DSL を使用してプログラム的に次のようにします。

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 AIBedrock 説明

EQ

等しい

次の値に等しい

NE

notEquals

次の値に等しくない

GT

greaterThan

次の値より大きい

GTE

greaterThanOrEquals

以上

LT

lessThan

次の値より小さい

LTE

lessThanOrEquals

次の値以下

入力

Value in list

NIN

notIn

Value not in list

AND

andAll

Logical AND

OR

orAll

Logical OR

NOT

(negation)

Logical NOT

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

手動構成

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

Add this dependency to your project:

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-bedrock-knowledgebase-store</artifactId>
</dependency>
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。

サンプルコード

@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:

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"));
}

ネイティブクライアントへのアクセス

The Bedrock Knowledge Base Vector Store provides access to the underlying native client through the getNativeClient() method:

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
}

制限

  • 読み取り専用 : The add() and delete() methods throw UnsupportedOperationException. Documents are managed through the Knowledge Base’s data source sync process.

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

  • Reranking availability : Model availability varies by AWS region.

Supported Data Sources

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

データソース Metadata Field サンプル

S3

source

s3://bucket/path/document.pdf

Confluence

source

confluence.example.com/page/123 (英語)

SharePoint

source

sharepoint.example.com/doc/456 (英語)

Salesforce

source

salesforce.example.com/record/789 (英語)

Web Crawler

source

example.com/page (英語)

カスタム

source

Custom document ID