此版本仍在开发中,尚未被视为稳定版本。最新的快照版本请使用 Spring AI 1.0.0-SNAPSHOT!spring-doc.cadn.net.cn

Qdrant

本节将引导您完成 Qdrant 的设置VectorStore来存储文档嵌入并执行相似性搜索。spring-doc.cadn.net.cn

Qdrant 是一个开源、高性能的矢量搜索引擎/数据库。它使用 HNSW (Hierarchical Navigable Small World) 算法进行高效的 k-NN 搜索作,并为基于元数据的查询提供高级过滤功能。spring-doc.cadn.net.cn

先决条件

建议提前创建具有适当尺寸和配置的 Qdrant 集合。 如果未创建集合,则QdrantVectorStore将尝试使用Cosinesimilarity 和配置的EmbeddingModel.

自动配置

Spring AI 为 Qdrant Vector Store 提供 Spring Boot 自动配置。 要启用它,请将以下依赖项添加到项目的 Maven 中pom.xml文件:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-qdrant-store-spring-boot-starter</artifactId>
</dependency>

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-qdrant-store-spring-boot-starter'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。

请查看 vector store 的配置参数列表,了解默认值和配置选项。spring-doc.cadn.net.cn

请参阅 Repositories 部分,将 Maven Central 和/或 Snapshot Repositories 添加到您的构建文件中。

矢量存储实现可以为您初始化必要的架构,但您必须通过指定initializeSchemaboolean 或设置…​initialize-schema=trueapplication.properties文件。spring-doc.cadn.net.cn

这是一个突破性的变化!在早期版本的 Spring AI 中,默认情况下会进行此架构初始化。

此外,您还需要配置一个EmbeddingModel豆。请参阅 EmbeddingModel 部分以了解更多信息。spring-doc.cadn.net.cn

现在,您可以自动连接QdrantVectorStore作为应用程序中的向量存储。spring-doc.cadn.net.cn

@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-doc.cadn.net.cn

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
        batching-strategy: TOKEN_COUNT # Optional: Controls how documents are batched for embedding

属性以spring.ai.vectorstore.qdrant.*用于配置QdrantVectorStore:spring-doc.cadn.net.cn

财产 描述 默认值

spring.ai.vectorstore.qdrant.hostspring-doc.cadn.net.cn

Qdrant 服务器的主机spring-doc.cadn.net.cn

localhostspring-doc.cadn.net.cn

spring.ai.vectorstore.qdrant.portspring-doc.cadn.net.cn

Qdrant 服务器的 gRPC 端口spring-doc.cadn.net.cn

6334spring-doc.cadn.net.cn

spring.ai.vectorstore.qdrant.api-keyspring-doc.cadn.net.cn

用于身份验证的 API 密钥spring-doc.cadn.net.cn

-spring-doc.cadn.net.cn

spring.ai.vectorstore.qdrant.collection-namespring-doc.cadn.net.cn

要使用的集合的名称spring-doc.cadn.net.cn

vector_storespring-doc.cadn.net.cn

spring.ai.vectorstore.qdrant.use-tlsspring-doc.cadn.net.cn

是否使用 TLS(HTTPS)spring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.vectorstore.qdrant.initialize-schemaspring-doc.cadn.net.cn

是否初始化 Schemaspring-doc.cadn.net.cn

falsespring-doc.cadn.net.cn

spring.ai.vectorstore.qdrant.batching-strategyspring-doc.cadn.net.cn

计算嵌入时对文档进行批处理的策略。选项包括TOKEN_COUNTFIXED_SIZEspring-doc.cadn.net.cn

TOKEN_COUNTspring-doc.cadn.net.cn

手动配置

您可以手动配置 Qdrant 向量存储,而不是使用 Spring Boot 自动配置。为此,您需要添加spring-ai-qdrant-store到您的项目:spring-doc.cadn.net.cn

<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-qdrant-store</artifactId>
</dependency>

或发送到您的 Gradlebuild.gradlebuild 文件。spring-doc.cadn.net.cn

dependencies {
    implementation 'org.springframework.ai:spring-ai-qdrant-store'
}
请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。

创建 Qdrant 客户端 Bean:spring-doc.cadn.net.cn

@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使用 Builder 模式的 Bean:spring-doc.cadn.net.cn

@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 存储结合使用。spring-doc.cadn.net.cn

例如,您可以使用文本表达式语言:spring-doc.cadn.net.cn

vectorStore.similaritySearch(
    SearchRequest.builder()
        .query("The World")
        .topK(TOP_K)
        .similarityThreshold(SIMILARITY_THRESHOLD)
        .filterExpression("author in ['john', 'jill'] && article_type == 'blog'").build());

或者以编程方式使用Filter.ExpressionDSL:spring-doc.cadn.net.cn

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());
这些(可移植的)筛选表达式会自动转换为专有的 Qdrant 筛选表达式

访问 Native Client

Qdrant Vector Store 实现提供对底层原生 Qdrant 客户端 (QdrantClient) 通过getNativeClient()方法:spring-doc.cadn.net.cn

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接口。spring-doc.cadn.net.cn