|
最新快照版本请使用Spring AI 1.1.0! |
Qdrant
本节将带你了解如何设置Qdrant。VectorStore用于存储文档嵌入并进行相似性搜索。
Qdrant 是一个开源的高性能矢量搜索引擎/数据库。它采用HNSW(分层可导航小世界)算法,实现高效的k-NN搜索作,并为基于元数据的查询提供先进的过滤功能。
前提条件
-
Qdrant 实例:按照 Qdrant 文档中的安装说明设置 Qdrant 实例。
-
如有需要,需为 EmbeddingModel 提供 API 密钥,以生成存储于
QdrantVectorStore.
建议提前建立Qdrant集合,并采用适当的尺寸和配置。
如果集合未被创建,则QdrantVectorStore将尝试使用以下条件创建一个余弦相似度与配置的维度嵌入模型. |
自动配置
|
春季AI自动配置、起始模块的工件名称发生了重大变化。 更多信息请参阅升级说明。 |
Spring AI 为 Qdrant 向量存储提供了 Spring Boot 自动配置。
要启用它,请在项目的 Maven 中添加以下依赖pom.xml文件:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-qdrant</artifactId>
</dependency>
或者去你的Gradlebuild.gradle构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-qdrant'
}
| 请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。 |
请查看矢量存储的配置参数列表,了解默认值和配置选项。
| 请参阅“遗物仓库”部分,将Maven Central和/或快照仓库添加到你的构建文件中。 |
向量存储实现可以帮你初始化所需的模式,但你必须通过指定初始化模式构建器中的布尔值或设置…initialize-schema=true在application.properties文件。
| 这是一个颠覆性的变革!在早期版本的 Spring AI 中,这种模式初始化是默认的。 |
此外,你还需要一个配置嵌入模型豆。更多信息请参阅嵌入模型部分。
现在你可以自动接线QdrantVectorStore作为你的应用中的矢量存储。
@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 Qdrant
vectorStore.add(documents);
// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.builder().query("Spring").topK(5).build());
配置属性
连接到Qdrant并使用QdrantVectorStore你需要为你的实例提供访问权限。
可以通过 Spring Boot 提供一个简单的配置application.yml:
spring:
ai:
vectorstore:
qdrant:
host: <qdrant host>
port: <qdrant grpc port>
api-key: <qdrant api key>
collection-name: <collection name>
use-tls: false
initialize-schema: true
性质以spring.ai.vectorstore.qdrant.*用于配置QdrantVectorStore:
| 属性 | 描述 | 默认值 |
|---|---|---|
|
Qdrant 服务器的主机 |
|
|
Qdrant服务器的gRPC端口 |
|
|
用于认证的API密钥 |
- |
|
所用收藏名称 |
|
|
是否使用 TLS(HTTPS) |
|
|
是否初始化模式 |
|
手动配置
你可以手动配置 Qdrant 向量存储,而不是用 Spring Boot 的自动配置。为此你需要添加Spring-ai-qdrant-store致你的项目:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-qdrant-store</artifactId>
</dependency>
或者去你的Gradlebuild.gradle构建文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-qdrant-store'
}
| 请参考依赖管理部分,将Spring AI的物料清单添加到你的构建文件中。 |
创建Qdrant客户豆:
@Bean
public QdrantClient qdrantClient() {
QdrantGrpcClient.Builder grpcClientBuilder =
QdrantGrpcClient.newBuilder(
"<QDRANT_HOSTNAME>",
<QDRANT_GRPC_PORT>,
<IS_TLS>);
grpcClientBuilder.withApiKey("<QDRANT_API_KEY>");
return new QdrantClient(grpcClientBuilder.build());
}
然后创建QdrantVectorStore使用构建图纸的豆子:
@Bean
public VectorStore vectorStore(QdrantClient qdrantClient, EmbeddingModel embeddingModel) {
return QdrantVectorStore.builder(qdrantClient, embeddingModel)
.collectionName("custom-collection") // Optional: defaults to "vector_store"
.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")));
}
元数据过滤
你也可以利用通用的、可移植的元数据过滤器配合 Qdrant 存储。
例如,你可以使用以下文本表达式语言:
vectorStore.similaritySearch(
SearchRequest.builder()
.query("The World")
.topK(TOP_K)
.similarityThreshold(SIMILARITY_THRESHOLD)
.filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());
或者程序化地使用滤波。表达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());
| 这些(可携带的)滤波表达式会自动转换为专有的Qdran滤波表达式。 |
访问本地客户端
Qdrant 向量存储实现提供了对底层原生 Qdrant 客户端的访问(Qdrant客户端)通过getNativeClient()方法:
QdrantVectorStore vectorStore = context.getBean(QdrantVectorStore.class);
Optional<QdrantClient> nativeClient = vectorStore.getNativeClient();
if (nativeClient.isPresent()) {
QdrantClient client = nativeClient.get();
// Use the native client for Qdrant-specific operations
}
本地客户端允许你访问 Qdrant 特定的功能和作,这些可能无法通过VectorStore接口。