Pgvector Document Store for Retrieval Augmented Generation (RAG)
When implementing Retrieval Augmented Generation (RAG), a capable document store is necessary. This guide will explain how to leverage a pgvector database as the document store.
Leveraging the pgvector Document Store
To utilize the PostgreSQL pgVector document store, you’ll need to include the following dependency:
<dependency>
<groupId>io.quarkiverse.langchain4j</groupId>
<artifactId>quarkus-langchain4j-pgvector</artifactId>
<version>0.21.0</version>
</dependency>
This extension will check for a default datasource, ensure you have defined at least one datasource. For detailed guidance, refer to the CONFIGURE DATA SOURCES IN QUARKUS.
The pgvector store requires the dimension of the vector to be set. Add the quarkus.langchain4j.pgvector.dimension property to your application.properties file and set it to the dimension of the vector. The dimension depends on the embedding model you use.
For example, AllMiniLmL6V2QuantizedEmbeddingModel produces vectors of dimension 384. OpenAI’s text-embedding-ada-002 produces vectors of dimension 1536.
|
Upon installing the extension, you can utilize the pgvector store using 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 io.quarkiverse.langchain4j.pgvector.PgVectorEmbeddingStore;
@ApplicationScoped
public class IngestorExampleWithPgvector {
/**
* The embedding store (the database).
* The bean is provided by the quarkus-langchain4j-pgvector extension.
*/
@Inject
PgVectorEmbeddingStore 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);
}
}
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 |
---|---|---|
The name of the configured Postgres datasource to use for this store. If not set, the default datasource from the Agroal extension will be used. Environment variable: |
string |
|
The table name for storing embeddings Environment variable: |
string |
|
The dimension of the embedding vectors. This has to be the same as the dimension of vectors produced by the embedding model that you use. For example, AllMiniLmL6V2QuantizedEmbeddingModel produces vectors of dimension 384. OpenAI’s text-embedding-ada-002 produces vectors of dimension 1536. Environment variable: |
int |
required |
Use index or not Environment variable: |
boolean |
|
index size Environment variable: |
int |
|
Whether the table should be created if not already existing. Environment variable: |
boolean |
|
Whether the table should be dropped prior to being created. Environment variable: |
boolean |
|
Metadata type:
Default value: COMBINED_JSON Environment variable: |
|
|
Metadata Definition: SQL definition of metadata field(s). By default, "metadata JSON NULL" configured. This is only suitable if using the JSON metadata type. If using JSONB metadata type, this should in most cases be set to If using COLUMNS metadata type, this should be a list of columns, one column for each desired metadata field. Example: condominium_id uuid null, user uuid null Environment variable: |
list of string |
|
Metadata Indexes, list of fields to use as index. For instance:
Environment variable: |
list of string |
|
Index Type:
Environment variable: |
string |
|