开放搜索
本节将指导您完成设置OpenSearchVectorStore
来存储文档嵌入并执行相似性搜索。
OpenSearch 是一个开源搜索和分析引擎,最初是从 Elasticsearch 分叉而来的,在 Apache License 2.0 下分发。它通过简化 AI 生成资产的集成和管理来增强 AI 应用程序开发。OpenSearch 支持向量、词法和混合搜索功能,利用高级向量数据库功能来促进低延迟查询和相似性搜索,如向量数据库页面上详述的那样。
OpenSearch k-NN 功能允许用户从大型数据集中查询向量嵌入。嵌入是数据对象(如文本、图像、音频或文档)的数字表示形式。嵌入可以存储在索引中,并使用各种相似性函数进行查询。
先决条件
-
正在运行的 OpenSearch 实例。以下选项可用:
-
如果需要,EmbeddingModel 的 API 密钥,用于生成由
OpenSearchVectorStore
.
自动配置
Spring AI 为 OpenSearch Vector Store 提供 Spring Boot 自动配置。
要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml
文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-opensearch-store-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件:
dependencies {
implementation 'org.springframework.ai:spring-ai-opensearch-store-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
对于 Amazon OpenSearch Service,请改用以下依赖项:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-aws-opensearch-store-spring-boot-starter</artifactId>
</dependency>
或者对于 Gradle:
dependencies {
implementation 'org.springframework.ai:spring-ai-aws-opensearch-store-spring-boot-starter'
}
请查看 vector store 的配置参数列表,了解默认值和配置选项。
此外,您还需要配置一个EmbeddingModel
豆。请参阅 EmbeddingModel 部分以了解更多信息。
现在,您可以自动连接OpenSearchVectorStore
作为应用程序中的 vector store 中:
@Autowired VectorStore vectorStore;
// ...
List<Document> documents = List.of(
new Document("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 OpenSearch
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置属性
要连接到 OpenSearch 并使用OpenSearchVectorStore
,您需要提供实例的访问详细信息。
可以通过 Spring Boot 的application.yml
:
spring:
ai:
vectorstore:
opensearch:
uris: <opensearch instance URIs>
username: <opensearch username>
password: <opensearch password>
index-name: spring-ai-document-index
initialize-schema: true
similarity-function: cosinesimil
batching-strategy: TOKEN_COUNT
aws: # Only for Amazon OpenSearch Service
host: <aws opensearch host>
service-name: <aws service name>
access-key: <aws access key>
secret-key: <aws secret key>
region: <aws region>
属性以spring.ai.vectorstore.opensearch.*
用于配置OpenSearchVectorStore
:
财产 | 描述 | 默认值 |
---|---|---|
|
OpenSearch 集群终端节点的 URI |
- |
|
用于访问 OpenSearch 集群的用户名 |
- |
|
指定用户名的密码 |
- |
|
用于存储向量的索引的名称 |
|
|
是否初始化所需的 schema |
|
|
要使用的相似性函数 |
|
|
计算嵌入时对文档进行批处理的策略。选项包括 |
|
|
OpenSearch 实例的主机名 |
- |
|
AWS 服务名称 |
- |
|
AWS 访问密钥 |
- |
|
AWS 密钥 |
- |
|
AWS 区域 |
- |
可以使用以下相似性函数:
-
cosinesimil
- 默认,适用于大多数用例。测量向量之间的余弦相似性。 -
l1
- 向量之间的曼哈顿距离。 -
l2
- 向量之间的欧几里得距离。 -
linf
- 向量之间的切比雪夫距离。
手动配置
您可以手动配置 OpenSearch 矢量存储,而不是使用 Spring Boot 自动配置。为此,您需要添加spring-ai-opensearch-store
到您的项目:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-opensearch-store</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件:
dependencies {
implementation 'org.springframework.ai:spring-ai-opensearch-store'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 |
创建 OpenSearch 客户端 Bean:
@Bean
public OpenSearchClient openSearchClient() {
RestClient restClient = RestClient.builder(
HttpHost.create("http://localhost:9200"))
.build();
return new OpenSearchClient(new RestClientTransport(
restClient, new JacksonJsonpMapper()));
}
然后创建OpenSearchVectorStore
使用 Builder 模式的 Bean:
@Bean
public VectorStore vectorStore(OpenSearchClient openSearchClient, EmbeddingModel embeddingModel) {
return OpenSearchVectorStore.builder(openSearchClient, embeddingModel)
.index("custom-index") // Optional: defaults to "spring-ai-document-index"
.similarityFunction("l2") // Optional: defaults to "cosinesimil"
.initializeSchema(true) // Optional: defaults to false
.batchingStrategy(new TokenCountBatchingStrategy()) // Optional: defaults to TokenCountBatchingStrategy
.build();
}
// This can be any EmbeddingModel implementation
@Bean
public EmbeddingModel embeddingModel() {
return new OpenAiEmbeddingModel(new OpenAiApi(System.getenv("OPENAI_API_KEY")));
}
元数据筛选
您还可以将通用的可移植元数据筛选器与 OpenSearch 结合使用。
例如,您可以使用文本表达式语言:
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && 'article_type' == 'blog'").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("author", "john", "jill"),
b.eq("article_type", "blog")).build()).build());
这些(可移植的)筛选表达式会自动转换为专有的 OpenSearch 查询字符串查询。 |
例如,此可移植筛选条件表达式:
author in ['john', 'jill'] && 'article_type' == 'blog'
转换为专有的 OpenSearch 筛选条件格式:
(metadata.author:john OR jill) AND metadata.article_type:blog
访问 Native Client
OpenSearch Vector Store 实现提供对底层原生 OpenSearch 客户端 (OpenSearchClient
) 通过getNativeClient()
方法:
OpenSearchVectorStore vectorStore = context.getBean(OpenSearchVectorStore.class);
Optional<OpenSearchClient> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
OpenSearchClient client = nativeClient.get();
// Use the native client for OpenSearch-specific operations
}
本机客户端允许您访问特定于 OpenSearch 的功能和作,这些功能和作可能不会通过VectorStore
接口。