このバージョンはまだ開発中であり、まだ安定しているとは見なされていません。最新の安定バージョンについては、Spring AI 1.1.7 を使用してください!

S3 ベクトルストア

このセクションでは、ドキュメントの埋め込みを保存し、類似性検索を実行するための S3VectorStore のセットアップについて説明します。

AWS S3 Vector Store [Amazon] は、大規模なベクトルの保存とクエリをサポートするサーバーレスオブジェクトストレージです。

S3 Vector Store API [Amazon] は AWS S3 バケットのコア機能を継承し、S3 をベクトルデータベースとして使用できるようにします。

  • ベクトルと関連するメタデータをハッシュまたは JSON ドキュメント内に保存します

  • ベクトルの取得

  • ベクトル検索を実行する

前提条件

  1. S3 ベクトルストアバケット

  2. EmbeddingModel インスタンスを使用してドキュメントの埋め込みを計算します。いくつかのオプションが利用可能です:

    • 必要に応じて、EmbeddingModel が S3VectorStore によって保存される埋め込みを生成するための API キー。

自動構成

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

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

または、Gradle build.gradle ビルドファイルに保存します。

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-vector-store-s3'
}
Spring AI BOM をビルドファイルに追加するには、"依存関係管理" セクションを参照してください。
アーティファクトリポジトリセクションを参照して、ビルドファイルに Maven Central リポジトリやスナップショットリポジトリを追加します。

デフォルト値と構成オプションについては、ベクトルストアの構成パラメーターのリストを参照してください。

さらに、設定済みの EmbeddingModel Bean が必要です。詳細については、"EmbeddingModel" セクションを参照してください。

これで、S3VectorStore をアプリケーション内のベクトルストアとして自動接続できるようになりました。

@Autowired VectorStore vectorStore;

// ...

List <Document> documents = List.of(
    new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")),
    new Document("The World is Big and Salvation Lurks Around the Corner"),
    new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2")));

// Add the documents to S3 Vector Store Bucket
vectorStore.add(documents);

// Retrieve documents similar to a query
List<Document> results = this.vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());

プロパティの構成

AWS S3 ベクトルストアに接続して S3VectorStore を使用するには、正しい認証情報とリージョンを指定して Bean または S3VectorsClient を作成する必要があります。

spring.ai.vectorstore.s3.* で始まるプロパティは、S3VectorStore を構成するために使用されます。

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

spring.ai.vectorstore.s3.index-name

ベクトルを格納するインデックスの名前

spring-ai-index

spring.ai.vectorstore.s3.vector-bucket-name

ベクトルが格納されているバケットの名前

my-vector-bucket-on-aws

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

You can leverage the generic, portable metadata filters with S3 Vector Store as well.

例: 次のいずれかのテキスト式言語を使用できます。

vectorStore.similaritySearch(SearchRequest.builder()
        .query("The World")
        .topK(TOP_K)
        .similarityThreshold(SIMILARITY_THRESHOLD)
        .filterExpression("country in ['UK', 'NL'] && year >= 2020").build());

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

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.similaritySearch(SearchRequest.builder()
        .query("The World")
        .topK(TOP_K)
        .similarityThreshold(SIMILARITY_THRESHOLD)
        .filterExpression(b.and(
                b.in("country", "UK", "NL"),
                b.gte("year", 2020)).build()).build());
これらの (ポータブル) フィルター式は自動的に AWS SDK Java V2 Filter Document object (英語) に変換されます。

例: この移植可能なフィルター式:

country in ['UK', 'NL'] && year >= 2020

独自の S3 ベクトルストアフィルター形式に変換されます。

@country:{UK | NL} @year:[2020 inf]

手動構成

Spring Boot の自動構成を使用する代わりに、S3 ベクトルストアを手動で構成することもできます。そのためには、spring-ai-s3-vector-store をプロジェクトに追加する必要があります。

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-s3-vector-store</artifactId>
</dependency>

または、Gradle build.gradle ビルドファイルに保存します。

dependencies {
    implementation 'org.springframework.ai:spring-ai-s3-vector-store'
}

次に、ビルダーパターンを使用して S3VectorStore Bean を作成します。

@Bean
VectorStore s3VectorStore(S3VectorsClient s3VectorsClient, EmbeddingModel embeddingModel) {
    S3VectorStore.Builder builder = new S3VectorStore.Builder(s3VectorsClient, embeddingModel); // Required a must
    builder.indexName(properties.getIndexName()) // Required indexName must be specified
            .vectorBucketName(properties.getVectorBucketName()) // Required vectorBucketName must be specified
            .filterExpressionConverter(yourConverter);  // Optional if you want to override default filterConverter
    return builder.build();
	}

// This can be any EmbeddingModel implementation
@Bean
public EmbeddingModel embeddingModel() {
    return new OpenAiEmbeddingModel(OpenAiEmbeddingOptions.builder().apiKey(System.getenv("OPENAI_API_KEY")).build());
}

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

S3 ベクトルストアの実装では、基盤となるネイティブ S3VectorsClient クライアントへのアクセスが提供されます。

S3VectorStore vectorStore = context.getBean(S3VectorStore.class);
Optional<S3VectorsClient> nativeClient = vectorStore.getNativeClient();

if (nativeClient.isPresent()) {
    S3VectorsClient s3Client = nativeClient.get();
    // Use the native client for S3-Vector-Store-specific operations
}

ネイティブクライアントを使用すると、VectorStore インターフェースでは公開されていない可能性のある、S3-Vector-Store 固有の機能や操作にアクセスできます。