Qdrant

本节将指导您设置 Qdrant VectorStore 以存储文档嵌入,并执行相似度搜索。

This section walks you through setting up the Qdrant VectorStore to store document embeddings and perform similarity searches.

Qdrant是开源高性能向量搜索引擎/数据库。

Qdrant is an open-source, high-performance vector search engine/database.

Prerequisites

  • Qdrant Instance: Set up a Qdrant instance by following the installation instructions in the Qdrant documentation.

  • If required, an API key for the EmbeddingClient to generate the embeddings stored by the QdrantVectorStore.

若要设置 QdrantVectorStore,您需要以下来自 Qdrant 实例的信息:HostGRPC PortCollection NameAPI Key(如果需要)。

To set up QdrantVectorStore, you’ll need the following information from your Qdrant instance: Host, GRPC Port, Collection Name, and API Key (if required).

建议 Qdrant 集合使用适当的维度和配置提前 created。如果未创建集合,则 QdrantVectorStore 将尝试使用 Cosine 相似性和已配置 EmbeddingClient 的维度来创建一个集合。

It is recommended that the Qdrant collection is created in advance with the appropriate dimensions and configurations. If the collection is not created, the QdrantVectorStore will attempt to create one using the Cosine similarity and the dimension of the configured EmbeddingClient.

Dependencies

然后向您的项目添加 Qdrant 引导启动器依赖关系:

Then add the Qdrant boot starter dependency to your project:

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

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-qdrant-store-spring-boot-starter'
}

向量存储还要求 EmbeddingClient 实例为文档计算嵌入。您可以选择一个可用的 EmbeddingClient Implementations

The Vector Store, also requires an EmbeddingClient instance to calculate embeddings for the documents. You can pick one of the available EmbeddingClient Implementations.

例如,要使用 OpenAI EmbeddingClient,请将以下依赖项添加到你的项目中:

For example to use the OpenAI EmbeddingClient add the following dependency to your project:

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

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}

请参阅 Dependency Management 部分,将 Spring AI BOM 添加到您的构建文件中。请参阅 Repositories 部分,将 Milestone 和/或快照存储库添加到您的构建文件中。

Refer to the Dependency Management section to add the Spring AI BOM to your build file. Refer to the Repositories section to add Milestone and/or Snapshot Repositories to your build file.

若要连接到 Qdrant 并使用 QdrantVectorStore,您需要为您的实例提供访问详细信息。可以通过 Spring Boot 的 application.properties 提供简单的配置,

To connect to Qdrant and use the QdrantVectorStore, you need to provide access details for your instance. A simple configuration can either be provided via Spring Boot’s 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>

查看 configuration parameters 列表,了解默认值和配置选项。

Check the list of qdrant-vectorstore-properties to learn about the default values and configuration options.

现在,您可以在应用程序中自动连接 Qdrant Vector Store 并使用它

Now you can Auto-wire the Qdrant Vector Store in your application and use it

@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(List.of(document));

// Retrieve documents similar to a query
List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5));

Manual Configuration

您无需使用 Spring Boot 自动配置,而是可以手动配置 QdrantVectorStore。为此,您需要向您的项目添加 spring-ai-qdrant 依赖关系:

Instead of using the Spring Boot auto-configuration, you can manually configure the QdrantVectorStore. For this you need to add the spring-ai-qdrant dependency to your project:

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

或添加到 Gradle build.gradle 构建文件中。

or to your Gradle build.gradle build file.

dependencies {
    implementation 'org.springframework.ai:spring-ai-qdrant'
}

若要在应用程序中配置 Qdrant,您可以使用以下设置:

To configure Qdrant in your application, you can use the following setup:

@Bean
public QdrantVectorStoreConfig qdrantVectorStoreConfig() {

    return QdrantVectorStoreConfig.builder()
        .withHost("<QDRANT_HOSTNAME>")
        .withPort(<QDRANT_GRPC_PORT>)
        .withCollectionName("<QDRANT_COLLECTION_NAME>")
        .withApiKey("<QDRANT_API_KEY>")
        .build();
}

通过向你的项目中添加 Spring Boot OpenAI 启动器来与 OpenAI 的嵌入集成。这为你提供了嵌入客户端的实现:

Integrate with OpenAI’s embeddings by adding the Spring Boot OpenAI starter to your project. This provides you with an implementation of the Embeddings client:

@Bean
public VectorStore vectorStore(QdrantVectorStoreConfig config, EmbeddingClient embeddingClient) {
    return new QdrantVectorStore(config, embeddingClient);
}

Metadata filtering

您可以利用 Ocient 矢量存储中通用的可移植过滤器 metadata filters

You can leverage the generic, portable metadata filters with the Qdrant vector store.

例如,你可以使用文本表达式语言:

For example, you can use either the text expression language:

vectorStore.similaritySearch(
    SearchRequest.defaults()
    .withQuery("The World")
    .withTopK(TOP_K)
    .withSimilarityThreshold(SIMILARITY_THRESHOLD)
    .withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'"));

或使用 Filter.Expression DSL 以编程方式:

or programmatically using the 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("john", "jill"),
        b.eq("article_type", "blog")).build()));

这些筛选表达式将转换为等效的 Qdrant filters

These filter expressions are converted into the equivalent Qdrant filters.

Configuration properties

您可以在 Spring Boot 配置中使用以下属性来自定义 Qdrant 向量存储。

You can use the following properties in your Spring Boot configuration to customize the Qdrant vector store.

Property Description Default value

spring.ai.vectorstore.qdrant.host

The host of the Qdrant server.

localhost

spring.ai.vectorstore.qdrant.port

The gRPC port of the Qdrant server.

6334

spring.ai.vectorstore.qdrant.api-key

The API key to use for authentication with the Qdrant server.

-

spring.ai.vectorstore.qdrant.collection-name

The name of the collection to use in Qdrant.

-

spring.ai.vectorstore.qdrant.use-tls

Whether to use TLS(HTTPS).

false