|
| 1 | += Qdrant |
| 2 | + |
| 3 | +This section walks you through setting up the Qdrant `VectorStore` to store document embeddings and perform similarity searches. |
| 4 | + |
| 5 | +link:https://www.qdrant.tech/[Qdrant] is an open-source, high-performance vector search engine/database. |
| 6 | + |
| 7 | +== Prerequisites |
| 8 | + |
| 9 | +* Qdrant Instance: Set up a Qdrant instance by following the link:https://qdrant.tech/documentation/guides/installation/[installation instructions] in the Qdrant documentation. |
| 10 | +* If required, an API key for the xref:api/embeddings.adoc#available-implementations[EmbeddingClient] to generate the embeddings stored by the `QdrantVectorStore`. |
| 11 | + |
| 12 | +== Configuration |
| 13 | + |
| 14 | +To set up `QdrantVectorStore`, you'll need the following information from your Qdrant instance: |
| 15 | + |
| 16 | +* Qdrant Host |
| 17 | +* Qdrant GRPC Port |
| 18 | +* Qdrant Collection Name |
| 19 | +* Optional Qdrant API Key (not required for local development) |
| 20 | + |
| 21 | +[NOTE] |
| 22 | +==== |
| 23 | +A Qdrant collection has to be link:https://qdrant.tech/documentation/concepts/collections/#create-a-collection[created] in advance with the appropriate dimensions and configurations. |
| 24 | +
|
| 25 | +For example if using the OpenAI `text-embedding-ada-002` embedding model, create a collection with a vector size of `1536`. |
| 26 | +==== |
| 27 | + |
| 28 | +== Dependencies |
| 29 | + |
| 30 | +* The Vector Store requires an `EmbeddingClient` instance to calculate embeddings for the documents. |
| 31 | +You can pick one of the available xref:api/embeddings.adoc#available-implementations[EmbeddingClient Implementations]. For example ou can use the OpenAI boot starter: |
| 32 | + |
| 33 | +[source,xml] |
| 34 | +---- |
| 35 | +<dependency> |
| 36 | + <groupId>org.springframework.ai</groupId> |
| 37 | + <artifactId>spring-ai-openai-spring-boot-starter</artifactId> |
| 38 | +</dependency> |
| 39 | +---- |
| 40 | + |
| 41 | +or to your Gradle `build.gradle` build file. |
| 42 | + |
| 43 | +[source,groovy] |
| 44 | +---- |
| 45 | +dependencies { |
| 46 | + implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter' |
| 47 | +} |
| 48 | +---- |
| 49 | + |
| 50 | +TIP: Additionally, you'll need to provide your OpenAI API Key. Set it as an environment variable like so: |
| 51 | +`export SPRING_AI_OPENAI_API_KEY='Your_OpenAI_API_Key` |
| 52 | + |
| 53 | + |
| 54 | +* Add the Qdrant Boot Starter dependency to your project: |
| 55 | + |
| 56 | +[source,xml] |
| 57 | +---- |
| 58 | +<dependency> |
| 59 | + <groupId>org.springframework.ai</groupId> |
| 60 | + <artifactId>spring-ai-qdrant-store-spring-boot-starter</artifactId> |
| 61 | +</dependency> |
| 62 | +---- |
| 63 | + |
| 64 | +or to your Gradle `build.gradle` build file. |
| 65 | + |
| 66 | +[source,groovy] |
| 67 | +---- |
| 68 | +dependencies { |
| 69 | + implementation 'org.springframework.ai:spring-ai-qdrant-store-spring-boot-starter' |
| 70 | +} |
| 71 | +---- |
| 72 | + |
| 73 | +TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file. |
| 74 | + |
| 75 | +Please have a look at the list of xref:#qdrant-vectorstore-properties[configuration parameters] for the vector store to learn about the default values and configuration options. |
| 76 | + |
| 77 | +TIP: Refer to the xref:getting-started.adoc#repositories[Repositories] section to add Milestone and/or Snapshot Repositories to your build file. |
| 78 | + |
| 79 | + |
| 80 | +Now you can Auto-wire the Qdrant Vector Store in your application and use it |
| 81 | + |
| 82 | +[source,java] |
| 83 | +---- |
| 84 | +@Autowired |
| 85 | +VectorStore vectorStore; |
| 86 | +
|
| 87 | +... |
| 88 | +List <Document> documents = List.of( |
| 89 | + new Document("Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!! Spring AI rocks!!", Map.of("meta1", "meta1")), |
| 90 | + new Document("The World is Big and Salvation Lurks Around the Corner"), |
| 91 | + new Document("You walk forward facing the past and you turn back toward the future.", Map.of("meta2", "meta2"))); |
| 92 | +
|
| 93 | +// Add the documents to Qdrant |
| 94 | +vectorStore.add(List.of(document)); |
| 95 | +
|
| 96 | +// Retrieve documents similar to a query |
| 97 | +List<Document> results = vectorStore.similaritySearch(SearchRequest.query("Spring").withTopK(5)); |
| 98 | +---- |
| 99 | + |
| 100 | +== Configuration |
| 101 | + |
| 102 | +To connect to Qdrant and use the `QdrantVectorStore`, you need to provide access details for your instance. |
| 103 | +A simple configuration can either be provided via Spring Boot's _application.properties_, |
| 104 | + |
| 105 | +[source,properties] |
| 106 | +---- |
| 107 | +spring.ai.vectorstore.qdrant.host=<host of your qdrant instance> |
| 108 | +spring.ai.vectorstore.qdrant.port=<port of your qdrant instance> |
| 109 | +spring.ai.vectorstore.qdrant.api-key=<your api key> |
| 110 | +spring.ai.vectorstore.qdrant.collection-name=<The name of the collection to use in Qdrant> |
| 111 | +
|
| 112 | +# API key if needed, e.g. OpenAI |
| 113 | +spring.ai.openai.api.key=<api-key> |
| 114 | +---- |
| 115 | + |
| 116 | + |
| 117 | +== Manual Configuration |
| 118 | + |
| 119 | +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: |
| 120 | + |
| 121 | +[source,xml] |
| 122 | +---- |
| 123 | +<dependency> |
| 124 | + <groupId>org.springframework.ai</groupId> |
| 125 | + <artifactId>spring-ai-qdrant</artifactId> |
| 126 | +</dependency> |
| 127 | +---- |
| 128 | + |
| 129 | +or to your Gradle `build.gradle` build file. |
| 130 | + |
| 131 | +[source,groovy] |
| 132 | +---- |
| 133 | +dependencies { |
| 134 | + implementation 'org.springframework.ai:spring-ai-qdrant' |
| 135 | +} |
| 136 | +---- |
| 137 | + |
| 138 | +To configure Qdrant in your application, you can use the following setup: |
| 139 | + |
| 140 | +[source,java] |
| 141 | +---- |
| 142 | +@Bean |
| 143 | +public QdrantVectorStoreConfig qdrantVectorStoreConfig() { |
| 144 | +
|
| 145 | + return QdrantVectorStoreConfig.builder() |
| 146 | + .withHost("<QDRANT_HOSTNAME>") |
| 147 | + .withPort(<QDRANT_GRPC_PORT>) |
| 148 | + .withCollectionName("<QDRANT_COLLECTION_NAME>") |
| 149 | + .withApiKey("<QDRANT_API_KEY>") |
| 150 | + .build(); |
| 151 | +} |
| 152 | +---- |
| 153 | + |
| 154 | +Integrate with OpenAI's embeddings by adding the Spring Boot OpenAI starter to your project. |
| 155 | +This provides you with an implementation of the Embeddings client: |
| 156 | + |
| 157 | +[source,java] |
| 158 | +---- |
| 159 | +@Bean |
| 160 | +public VectorStore vectorStore(QdrantVectorStoreConfig config, EmbeddingClient embeddingClient) { |
| 161 | + return new QdrantVectorStore(config, embeddingClient); |
| 162 | +} |
| 163 | +---- |
| 164 | + |
| 165 | +=== Metadata filtering |
| 166 | + |
| 167 | +You can leverage the generic, portable link:https://docs.spring.io/spring-ai/reference/api/vectordbs.html#_metadata_filters[metadata filters] with the Qdrant vector store. |
| 168 | + |
| 169 | +For example, you can use either the text expression language: |
| 170 | + |
| 171 | +[source,java] |
| 172 | +---- |
| 173 | +vectorStore.similaritySearch( |
| 174 | + SearchRequest.defaults() |
| 175 | + .withQuery("The World") |
| 176 | + .withTopK(TOP_K) |
| 177 | + .withSimilarityThreshold(SIMILARITY_THRESHOLD) |
| 178 | + .withFilterExpression("author in ['john', 'jill'] && article_type == 'blog'")); |
| 179 | +---- |
| 180 | + |
| 181 | +or programmatically using the `Filter.Expression` DSL: |
| 182 | + |
| 183 | +[source,java] |
| 184 | +---- |
| 185 | +FilterExpressionBuilder b = new FilterExpressionBuilder(); |
| 186 | +
|
| 187 | +vectorStore.similaritySearch(SearchRequest.defaults() |
| 188 | + .withQuery("The World") |
| 189 | + .withTopK(TOP_K) |
| 190 | + .withSimilarityThreshold(SIMILARITY_THRESHOLD) |
| 191 | + .withFilterExpression(b.and( |
| 192 | + b.in("john", "jill"), |
| 193 | + b.eq("article_type", "blog")).build())); |
| 194 | +---- |
| 195 | + |
| 196 | +NOTE: These filter expressions are converted into the equivalent Qdrant link:https://qdrant.tech/documentation/concepts/filtering/[filters]. |
| 197 | + |
| 198 | + |
| 199 | +[[qdrant-vectorstore-properties]] |
| 200 | +== Qdrant VectorStore properties |
| 201 | + |
| 202 | +You can use the following properties in your Spring Boot configuration to customize the Qdrant vector store. |
| 203 | + |
| 204 | +|=== |
| 205 | +|Property| Description | Default value |
| 206 | + |
| 207 | +|`spring.ai.vectorstore.qdrant.host`| The host of the Qdrant server. | localhost |
| 208 | +|`spring.ai.vectorstore.qdrant.port`| The port of the Qdrant server. | 6334 |
| 209 | +|`spring.ai.vectorstore.qdrant.api-key`| The API key to use for authentication with the Qdrant server. | - |
| 210 | +|`spring.ai.vectorstore.qdrant.collection-name`| The name of the collection to use in Qdrant. | - |
| 211 | +|`spring.ai.vectorstore.qdrant.use-tls`| Whether to use TLS(HTTPS). Defaults to false. | false |
| 212 | +|=== |
0 commit comments