Qdrant
本节将引导您完成 Qdrant 的设置VectorStore
来存储文档嵌入并执行相似性搜索。
Qdrant 是一个开源、高性能的矢量搜索引擎/数据库。
先决条件
-
Qdrant 实例:按照 Qdrant 文档中的安装说明设置 Qdrant 实例。
-
如果需要,EmbeddingModel 的 API 密钥,用于生成由
QdrantVectorStore
.
设置QdrantVectorStore
,您将需要 Qdrant 实例中的以下信息:Host
,GRPC Port
,Collection Name
和API Key
(如果需要)。
建议提前创建具有适当尺寸和配置的 Qdrant 集合。
如果未创建集合,则QdrantVectorStore 将尝试使用Cosine similarity 和配置的EmbeddingModel . |
自动配置
然后将 Qdrant 启动Starters依赖项添加到您的项目中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-qdrant-store-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-qdrant-store-spring-boot-starter'
}
矢量存储实现可以为您初始化必要的架构,但您必须通过指定initializeSchema
boolean 或设置…initialize-schema=true
在application.properties
文件。
这是一个突破性的变化!在早期版本的 Spring AI 中,默认情况下会进行此架构初始化。 |
Vector Store 还需要一个EmbeddingModel
实例来计算文档的嵌入。
您可以选择一个可用的 EmbeddingModel Implementations。
例如,要使用 OpenAI EmbeddingModel,请将以下依赖项添加到您的项目中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。 请参阅 Repositories 部分,将 Milestone 和/或 Snapshot Repositories 添加到您的构建文件中。 |
要连接到 Qdrant 并使用QdrantVectorStore
,您需要提供实例的访问详细信息。
可以通过 Spring Boot 的 application.properties 提供简单的配置,
spring.ai.vectorstore.qdrant.host=<host of your qdrant instance>
spring.ai.vectorstore.qdrant.port=<the GRPC port of your qdrant instance>
spring.ai.vectorstore.qdrant.api-key=<your api key>
spring.ai.vectorstore.qdrant.collection-name=<The name of the collection to use in Qdrant>
# API key if needed, e.g. OpenAI
spring.ai.openai.api.key=<api-key>
检查配置参数列表以了解默认值和配置选项。 |
现在,您可以在应用程序中自动连接 Qdrant Vector Store 并使用它
@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 = this.vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));
配置属性
您可以在 Spring Boot 配置中使用以下属性来自定义 Qdrant 向量存储。
财产 | 描述 | 默认值 |
---|---|---|
|
Qdrant 服务器的主机。 |
本地主机 |
|
Qdrant 服务器的 gRPC 端口。 |
6334 |
|
用于 Qdrant 服务器身份验证的 API 密钥。 |
- |
|
要在 Qdrant 中使用的集合的名称。 |
- |
|
是否使用 TLS(HTTPS)。 |
假 |
|
是否初始化后端 schema |
假 |
元数据筛选
您可以将通用的可移植元数据过滤器与 Qdrant 矢量存储结合使用。
例如,您可以使用文本表达式语言:
vectorStore.similaritySearch(
SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));
或者以编程方式使用Filter.Expression
DSL:
FilterExpressionBuilder b = new FilterExpressionBuilder();
vectorStore.similaritySearch(SearchRequest.defaults()
.withQuery("The World")
.withTopK(TOP_K)
.withSimilarityThreshold(SIMILARITY_THRESHOLD)
.withFilterExpression(b.and(
b.in("author", "john", "jill"),
b.eq("article_type", "blog")).build()));
这些筛选条件表达式将转换为等效的 Qdrant 筛选条件。 |
手动配置
除了使用 Spring Boot 自动配置,您还可以手动配置QdrantVectorStore
.为此,您需要添加spring-ai-qdrant-store
依赖项添加到项目中:
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-qdrant-store</artifactId>
</dependency>
或发送到您的 Gradlebuild.gradle
build 文件。
dependencies {
implementation 'org.springframework.ai:spring-ai-qdrant'
}
要在应用程序中配置 Qdrant,您可以创建一个 QdrantClient:
@Bean
public QdrantClient qdrantClient() {
QdrantGrpcClient.Builder grpcClientBuilder =
QdrantGrpcClient.newBuilder(
"<QDRANT_HOSTNAME>",
<QDRANT_GRPC_PORT>,
<IS_TSL>);
grpcClientBuilder.withApiKey("<QDRANT_API_KEY>");
return new QdrantClient(grpcClientBuilder.build());
}
通过将 Spring Boot OpenAI Starters添加到您的项目,与 OpenAI 的嵌入集成。 这为您提供了 Embeddings 客户端的实现:
@Bean
public QdrantVectorStore vectorStore(EmbeddingModel embeddingModel, QdrantClient qdrantClient) {
return new QdrantVectorStore(qdrantClient, "<QDRANT_COLLECTION_NAME>", embeddingModel);
}