跳到主要内容
版本:2.0.1

S3 向量存储

QWen 3.8 中英对照 S3 Vector Store

本节将指导您配置 S3VectorStore,以存储文档嵌入并执行相似度搜索。

AWS S3 Vector Store 是一种支持大规模存储和查询向量的无服务器对象存储服务。

S3 向量存储 API 扩展了 AWS S3 存储桶的核心功能,允许您将 S3 用作向量数据库:

  • 在哈希表或 JSON 文档中存储向量及其关联的元数据
  • 检索向量
  • 执行向量搜索

前提条件​

  1. 一个 S3 向量存储桶

  2. 用于计算文档嵌入的 EmbeddingModel 实例。提供多种可选方案:

    • 如需要,为 EmbeddingModel 提供 API 密钥,以生成由 S3VectorStore 存储的嵌入向量。

自动配置​

Spring AI 为 S3 Vector Store 提供了 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 添加到您的构建文件中。

提示

请参见 Artifact Repositories 部分,将 Maven Central 和/或 Snapshot Repositories 添加到你的构建文件中。

请查看向量存储器的配置参数列表,以了解默认值和配置选项。

此外,您需要一个已配置的 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 Vector Store 并使用 S3VectorStore,您需要创建一个 S3VectorsClient 的 Bean,该 Bean 需要提供正确的 Credentials 和 Region。

以 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

元数据过滤​

你也可以在 S3 Vector Store 中使用通用且可移植的元数据过滤器。

例如,你可以使用文本表达式语言:

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 对象。

例如,此便携式过滤表达式:

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

会被转换为 S3 向量存储的过滤格式:

{"$and": [{"country": {"$in": ["UK", "NL"]}}, {"year": {"$gte": 2020}}]}

基于过滤器的删除​

你还可以通过过滤表达式删除文档:

vectorStore.delete("country in ['UK', 'NL'] && year >= 2020");

或者通过编程方式:

FilterExpressionBuilder b = new FilterExpressionBuilder();

vectorStore.delete(b.and(
b.in("country", "UK", "NL"),
b.gte("year", 2020)).build());
备注

S3 Vectors 不提供服务端过滤删除 API。基于过滤的删除操作会列出索引中的所有向量(分页,每页 500 条),并在本地评估过滤表达式。对于大型索引,此操作的时间复杂度与存储的向量总数成正比。

手动配置​

除了使用 Spring Boot 自动配置,你还可以手动配置 S3 Vector Store。为此,你需要在项目中添加 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());
}

访问 Native Client​

S3 Vector Store 实现提供了对底层原生 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
}

原生客户端可为您提供访问 S3-Vector-Store 特定功能和操作的权限,而这些功能与操作可能未通过 VectorStore 接口暴露。