|
此版本仍在开发中,尚未被视为稳定版。如需最新的快照版本,请使用 Spring AI 1.1.3! |
S3 向量存储
本节将指导您设置 S3VectorStore 以存储文档嵌入并执行相似性搜索。
AWS S3 向量存储 是一种支持大规模存储和查询向量的无服务器对象存储。
S3 向量存储 API 扩展了 AWS S3 存储桶的核心功能,并允许您将 S3 用作向量数据库:
-
将向量及相关元数据存储在哈希或 JSON 文档中
-
检索向量
-
执行向量搜索
前置条件
-
S3 向量存储桶
-
EmbeddingModel实例用于计算文档嵌入。有几种选项可供选择:-
如果需要,为EmbeddingModel提供一个API密钥,用于生成由
S3VectorStore存储的嵌入向量。
-
Auto-configuration
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 中央仓库和/或快照仓库添加到您的构建文件中。 |
请查看 向量存储的配置参数 列表,以了解默认值和配置选项。
此外,您还需要一个已配置的 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,该 S3VectorsClient 需要提供正确的凭证和区域。
以 spring.ai.vectorstore.s3.* 开头的属性用于配置 S3VectorStore:
| <property> </property> | <description> </description> | 默认值 |
|---|---|---|
|
用于存储向量的索引名称 |
|
|
存储向量的存储桶名称 |
|
元数据过滤
您也可以利用通用、可移植的 元数据过滤器 与 S3 向量存储一起使用。
例如,您可以使用文本表达式语言:
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 过滤文档对象。 |
例如,此可移植的过滤器表达式:
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(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}
访问原生客户端
The 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 向量存储特有的功能和操作,这些功能和操作可能无法通过 VectorStore 接口暴露。