|
此版本仍在开发中,尚未被视为稳定版。为了获取最新的快照版本,请使用Spring AI 1.1.3! |
S3 向量存储
本节将引导您完成配置S3VectorStore以存储文档嵌入并执行相似性搜索的过程。
AWS S3 向量存储 是一种无服务器对象存储,支持大规模存储和查询向量。
S3 向量存储 API 扩展了 AWS S3 存储桶的核心功能,允许您将 S3 用作向量数据库:
-
将向量及其相关的元数据存储在哈希表或JSON文档中。
-
获取向量
-
执行向量搜索
自动配置
Spring AI 为 S3 向量存储提供了 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven pom.xml 文件中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-s3</artifactId>
</dependency>
或者添加到您的Gradle 构建脚本文件中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-s3'
}
| 参考以下依赖管理部分,添加Spring AI BOM到你的构建文件中。 |
| 引用 Artifact 仓库 部分,以在构建文件中添加 Maven Central 和/或 Snapshot 仓库。 |
请查看以下向量存储的配置参数列表,了解默认值和配置选项。这些配置参数的类型为C0。
此外,您还需要配置一个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());
元数据过滤
您也可以将通用的、可移植的 元数据过滤器 与 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]
手动配置
您可以手动配置S3向量存储,而不使用Spring Boot的自动配置。为此,您需要将 spring-ai-s3-vector-store 添加到您的项目中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-s3-vector-store</artifactId>
</dependency>
或者添加到您的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")));
}
访问原生客户端
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
}
本地客户端赋予您访问S3向量存储特定功能和操作的能力,这些功能和操作可能无法通过VectorStore接口直接访问。