Qdrant
本節將引導您設定 Qdrant VectorStore
,以儲存文件嵌入並執行相似性搜尋。
Qdrant 是一個開源、高效能的向量搜尋引擎/資料庫。
先決條件
-
Qdrant 執行個體:依照 Qdrant 文件中的 安裝指示 設定 Qdrant 執行個體。
-
如果需要,EmbeddingModel 的 API 金鑰,用於產生
QdrantVectorStore
儲存的嵌入。
若要設定 QdrantVectorStore
,您需要以下來自 Qdrant 執行個體的資訊:Host
、GRPC Port
、Collection Name
和 API Key
(如果需要)。
建議預先建立 Qdrant 集合,並設定適當的維度和組態。如果未建立集合,QdrantVectorStore 將嘗試使用 Cosine 相似度和已配置 EmbeddingModel 的維度來建立一個。 |
自動配置
然後將 Qdrant 啟動器相依性新增至您的專案
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-qdrant-store-spring-boot-starter</artifactId>
</dependency>
或新增至您的 Gradle build.gradle
建置檔案。
dependencies {
implementation 'org.springframework.ai:spring-ai-qdrant-store-spring-boot-starter'
}
向量儲存實作可以為您初始化必要的結構描述,但您必須透過在適當的建構函式中指定 initializeSchema
布林值,或在 application.properties
檔案中設定 …initialize-schema=true
來選擇加入。
這是一個重大變更!在 Spring AI 的早期版本中,此結構描述初始化預設會發生。 |
向量儲存也需要 EmbeddingModel
執行個體來計算文件的嵌入。您可以選擇其中一個可用的 EmbeddingModel 實作。
例如,若要使用 OpenAI EmbeddingModel,請將以下相依性新增至您的專案
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>
或新增至您的 Gradle build.gradle
建置檔案。
dependencies {
implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter'
}
若要連線至 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 向量儲存並使用它
@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 伺服器的主機。 |
localhost |
|
Qdrant 伺服器的 gRPC 埠。 |
6334 |
|
用於 Qdrant 伺服器身分驗證的 API 金鑰。 |
- |
|
要在 Qdrant 中使用的集合名稱。 |
- |
|
是否使用 TLS(HTTPS)。 |
false |
|
是否初始化後端結構描述 |
false |
元數據篩選
您可以搭配 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>
或新增至您的 Gradle build.gradle
建置檔案。
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 啟動器新增至您的專案,與 OpenAI 的嵌入整合。這為您提供 Embeddings 客户端的實作
@Bean
public QdrantVectorStore vectorStore(EmbeddingModel embeddingModel, QdrantClient qdrantClient) {
return new QdrantVectorStore(qdrantClient, "<QDRANT_COLLECTION_NAME>", embeddingModel);
}