Qdrant
本節將引導您設定 Qdrant VectorStore
來儲存文件嵌入並執行相似性搜尋。
Qdrant 是一個開源、高效能的向量搜尋引擎/資料庫。它使用 HNSW (Hierarchical Navigable Small World) 演算法進行高效的 k-NN 搜尋操作,並提供高階過濾功能用於基於元資料的查詢。
前提條件
-
Qdrant 例項:按照 Qdrant 文件中的安裝說明設定 Qdrant 例項。
-
如果需要,為 EmbeddingModel 提供一個 API 金鑰,用於生成由
QdrantVectorStore
儲存的嵌入。
建議提前建立好 Qdrant 集合,並配置適當的維度。如果未建立集合,QdrantVectorStore 將嘗試使用配置的 EmbeddingModel 的維度和 Cosine 相似度來建立一個集合。 |
自動配置
Spring AI 自動配置、starter 模組的 artifact 名稱發生了重大變化。更多資訊請參閱升級說明。 |
Spring AI 為 Qdrant Vector Store 提供了 Spring Boot 自動配置。要啟用它,請將以下依賴項新增到您的專案 Maven pom.xml
檔案中
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-vector-store-qdrant</artifactId>
</dependency>
或新增到您的 Gradle build.gradle
構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-starter-vector-store-qdrant'
}
請參閱依賴管理部分,瞭解如何將 Spring AI BOM 新增到您的構建檔案中。 |
請查看向量儲存的配置引數列表,瞭解預設值和配置選項。
請參閱倉庫部分,瞭解如何將 Maven Central 和/或 Snapshot 倉庫新增到您的構建檔案中。 |
向量儲存實現可以為您初始化所需的 schema,但您必須透過在 builder 中指定 initializeSchema
布林值或在 application.properties
檔案中設定 …initialize-schema=true
來選擇啟用此功能。
這是個破壞性變更!在早期版本的 Spring AI 中,此 schema 初始化是預設啟用的。 |
此外,您還需要配置一個 EmbeddingModel
bean。更多資訊請參閱EmbeddingModel 部分。
現在您可以在應用程式中自動注入 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) |
|
|
是否初始化 schema |
|
手動配置
除了使用 Spring Boot 自動配置,您還可以手動配置 Qdrant 向量儲存。為此,您需要將 spring-ai-qdrant-store
新增到您的專案
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-qdrant-store</artifactId>
</dependency>
或新增到您的 Gradle build.gradle
構建檔案中。
dependencies {
implementation 'org.springframework.ai:spring-ai-qdrant-store'
}
請參閱依賴管理部分,瞭解如何將 Spring AI BOM 新增到您的構建檔案中。 |
建立一個 Qdrant 客戶端 bean
@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());
}
然後使用 builder 模式建立 QdrantVectorStore
bean
@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());
或使用 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());
這些(可移植的)過濾器表示式會自動轉換為 Qdrant 專有的過濾器表示式。 |
訪問原生客戶端
Qdrant Vector Store 實現透過 getNativeClient()
方法提供了對底層原生 Qdrant 客戶端 (QdrantClient
) 的訪問
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
}
原生客戶端使您能夠訪問透過 VectorStore
介面可能未公開的 Qdrant 特定功能和操作。