Qdrant
本節將引導您設定 Qdrant VectorStore 以儲存文件嵌入並執行相似性搜尋。
Qdrant 是一個開源、高效能的向量搜尋引擎/資料庫。它使用 HNSW(分層可導航小世界)演算法進行高效的 k-NN 搜尋操作,並提供高階過濾功能用於基於元資料的查詢。
先決條件
-
Qdrant 例項:按照 Qdrant 文件中的安裝說明設定 Qdrant 例項。
-
如果需要,需要為 EmbeddingModel 提供 API 金鑰,以生成由
QdrantVectorStore儲存的嵌入。
建議提前建立具有適當維度和配置的 Qdrant 集合。如果未建立集合,QdrantVectorStore 將嘗試使用 Cosine 相似度和配置的 EmbeddingModel 的維度建立一個集合。 |
自動配置
|
Spring AI 自動配置、啟動模組的工件名稱發生了重大變化。請參閱 升級說明 以獲取更多資訊。 |
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 新增到您的構建檔案中。 |
請查看向量儲存的配置引數列表,瞭解預設值和配置選項。
| 請參閱Artifact Repositories(工件倉庫)部分,將 Maven Central 和/或 Snapshot Repositories 新增到您的構建檔案中。 |
向量儲存實現可以為您初始化所需的模式,但您必須透過在構建器中指定 initializeSchema 布林值或在 application.properties 檔案中設定 ...initialize-schema=true 來選擇啟用。
| 這是一個重大更改!在早期版本的 Spring AI 中,此模式初始化是預設發生的。 |
此外,您還需要一個配置的 `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) |
|
|
是否初始化模式 |
|
手動配置
除了使用 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());
}
然後使用構建器模式建立 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 向量儲存實現透過 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 特定功能和操作。