Milvus Store for Retrieval Augmented Generation (RAG)
When implementing Retrieval Augmented Generation (RAG), a robust document store is crucial. This guide demonstrates how to leverage a Milvus database as the document store.
Leveraging the Milvus embedding store
To make use of the Milvus embedding store, you’ll need to include the following dependency:
<dependency>
<groupId>io.quarkiverse.langchain4j</groupId>
<artifactId>quarkus-langchain4j-milvus</artifactId>
</dependency>
This extension includes a dev service. Therefore, if you’re operating in a container environment, a Milvus instance will automatically start in dev and test mode.
Upon installing the extension, you can use the Milvus document store with the following code:
package io.quarkiverse.langchain4j.samples;
import static dev.langchain4j.data.document.splitter.DocumentSplitters.recursive;
import java.util.List;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.milvus.MilvusEmbeddingStore;
@ApplicationScoped
public class IngestorExampleWithMilvus {
/**
* The embedding store (the database).
* The bean is provided by the quarkus-langchain4j-milvus extension.
*/
@Inject
MilvusEmbeddingStore store;
/**
* The embedding model (how is computed the vector of a document).
* The bean is provided by the LLM (like openai) extension.
*/
@Inject
EmbeddingModel embeddingModel;
public void ingest(List<Document> documents) {
EmbeddingStoreIngestor ingestor = EmbeddingStoreIngestor.builder()
.embeddingStore(store)
.embeddingModel(embeddingModel)
.documentSplitter(recursive(500, 0))
.build();
// Warning - this can take a long time...
ingestor.ingest(documents);
}
}
To get started, only one configuration property is required to be
set - quarkus.langchain4j.milvus.dimension , which specifies the dimension
of the embeddings that you’re going to store and depends on the embedding
model.
|
To use a remote Milvus instance, you have to also set the host and port, in which case dev-services will not start another instance:
quarkus.langchain4j.milvus.host=localhost
quarkus.langchain4j.milvus.port=19530
Configuration Settings
Customize the behavior of the extension by exploring various configuration options:
Configuration property fixed at build time - All other configuration properties are overridable at runtime
Configuration property |
Type |
Default |
---|---|---|
Whether Dev Services for Milvus are enabled or not. Environment variable: |
boolean |
|
Container image for Milvus. Environment variable: |
string |
|
Optional fixed port the Milvus dev service will listen to. If not defined, the port will be chosen randomly. Environment variable: |
int |
|
Indicates if the Dev Service containers managed by Quarkus for Milvus are shared. Environment variable: |
boolean |
|
Service label to apply to created Dev Services containers. Environment variable: |
string |
|
The URL of the Milvus server. Environment variable: |
string |
required |
The port of the Milvus server. Environment variable: |
int |
required |
The authentication token for the Milvus server. Environment variable: |
string |
|
The username for the Milvus server. Environment variable: |
string |
|
The password for the Milvus server. Environment variable: |
string |
|
The timeout duration for the Milvus client. If not specified, 5 seconds will be used. Environment variable: |
||
Name of the database. Environment variable: |
string |
|
Create the collection if it does not exist yet. Environment variable: |
boolean |
|
Name of the collection. Environment variable: |
string |
|
Dimension of the vectors. Only applicable when the collection yet has to be created. Environment variable: |
int |
|
TODO Environment variable: |
string |
|
Name of the field to store the vector in. Environment variable: |
string |
|
Description of the collection. Environment variable: |
string |
|
The index type to use for the collection. Environment variable: |
|
|
The metric type to use for searching. Environment variable: |
|
|
The consistency level. Environment variable: |
|
|
About the Duration format
To write duration values, use the standard You can also use a simplified format, starting with a number:
In other cases, the simplified format is translated to the
|