Class PgVectorEmbeddingStore

java.lang.Object
dev.langchain4j.store.embedding.pgvector.PgVectorEmbeddingStore
All Implemented Interfaces:
EmbeddingStore<TextSegment>

public class PgVectorEmbeddingStore extends Object implements EmbeddingStore<TextSegment>
PGVector EmbeddingStore Implementation

Only cosine similarity is used. Only ivfflat index is used.

  • Field Details

    • datasource

      protected final DataSource datasource
      Datasource used to create the store
    • table

      protected final String table
      Embeddings table name
  • Constructor Details

    • PgVectorEmbeddingStore

      protected PgVectorEmbeddingStore(DataSource datasource, String table, Integer dimension, Boolean useIndex, Integer indexListSize, Boolean createTable, Boolean dropTableFirst, MetadataStorageConfig metadataStorageConfig)
      Constructor for PgVectorEmbeddingStore Class
      Parameters:
      datasource - The datasource to use
      table - The database table
      dimension - The vector dimension
      useIndex - Should use IVFFlat index
      indexListSize - The IVFFlat number of lists
      createTable - Should create table automatically
      dropTableFirst - Should drop table first, usually for testing
      metadataStorageConfig - The MetadataStorageConfig config.
    • PgVectorEmbeddingStore

      protected PgVectorEmbeddingStore(String host, Integer port, String user, String password, String database, String table, Integer dimension, Boolean useIndex, Integer indexListSize, Boolean createTable, Boolean dropTableFirst, MetadataStorageConfig metadataStorageConfig)
      Constructor for PgVectorEmbeddingStore Class Use this builder when you don't have datasource management.
      Parameters:
      host - The database host
      port - The database port
      user - The database user
      password - The database password
      database - The database name
      table - The database table
      dimension - The vector dimension
      useIndex - Should use IVFFlat index
      indexListSize - The IVFFlat number of lists
      createTable - Should create table automatically
      dropTableFirst - Should drop table first, usually for testing
      metadataStorageConfig - The MetadataStorageConfig config.
  • Method Details

    • initTable

      protected void initTable(Boolean dropTableFirst, Boolean createTable, Boolean useIndex, Integer dimension, Integer indexListSize)
      Initialize metadata table following configuration
      Parameters:
      dropTableFirst - Should drop table first, usually for testing
      createTable - Should create table automatically
      useIndex - Should use IVFFlat index
      dimension - The vector dimension
      indexListSize - The IVFFlat number of lists
    • add

      public String add(Embedding embedding)
      Adds a given embedding to the store.
      Specified by:
      add in interface EmbeddingStore<TextSegment>
      Parameters:
      embedding - The embedding to be added to the store.
      Returns:
      The auto-generated ID associated with the added embedding.
    • add

      public void add(String id, Embedding embedding)
      Adds a given embedding to the store.
      Specified by:
      add in interface EmbeddingStore<TextSegment>
      Parameters:
      id - The unique identifier for the embedding to be added.
      embedding - The embedding to be added to the store.
    • add

      public String add(Embedding embedding, TextSegment textSegment)
      Adds a given embedding and the corresponding content that has been embedded to the store.
      Specified by:
      add in interface EmbeddingStore<TextSegment>
      Parameters:
      embedding - The embedding to be added to the store.
      textSegment - Original content that was embedded.
      Returns:
      The auto-generated ID associated with the added embedding.
    • addAll

      public List<String> addAll(List<Embedding> embeddings)
      Adds multiple embeddings to the store.
      Specified by:
      addAll in interface EmbeddingStore<TextSegment>
      Parameters:
      embeddings - A list of embeddings to be added to the store.
      Returns:
      A list of auto-generated IDs associated with the added embeddings.
    • removeAll

      public void removeAll(Collection<String> ids)
      Description copied from interface: EmbeddingStore
      Removes all embeddings that match the specified IDs from the store.
      Specified by:
      removeAll in interface EmbeddingStore<TextSegment>
      Parameters:
      ids - A collection of unique IDs of the embeddings to be removed.
    • removeAll

      public void removeAll(Filter filter)
      Description copied from interface: EmbeddingStore
      Removes all embeddings that match the specified Filter from the store.
      Specified by:
      removeAll in interface EmbeddingStore<TextSegment>
      Parameters:
      filter - The filter to be applied to the Metadata of the TextSegment during removal. Only embeddings whose TextSegment's Metadata match the Filter will be removed.
    • removeAll

      public void removeAll()
      Description copied from interface: EmbeddingStore
      Removes all embeddings from the store.
      Specified by:
      removeAll in interface EmbeddingStore<TextSegment>
    • search

      Searches for the most similar (closest in the embedding space) Embeddings.
      All search criteria are defined inside the EmbeddingSearchRequest.
      EmbeddingSearchRequest.filter() is used to filter by meta dada.
      Specified by:
      search in interface EmbeddingStore<TextSegment>
      Parameters:
      request - A request to search in an EmbeddingStore. Contains all search criteria.
      Returns:
      An EmbeddingSearchResult containing all found Embeddings.
    • addAll

      public void addAll(List<String> ids, List<Embedding> embeddings, List<TextSegment> embedded)
      Description copied from interface: EmbeddingStore
      Adds multiple embeddings and their corresponding contents that have been embedded to the store.
      Specified by:
      addAll in interface EmbeddingStore<TextSegment>
      Parameters:
      ids - A list of IDs associated with the added embeddings.
      embeddings - A list of embeddings to be added to the store.
      embedded - A list of original contents that were embedded.
    • getConnection

      protected Connection getConnection() throws SQLException
      Datasource connection Creates the vector extension and add the vector type if it does not exist. Could be overridden in case extension creation and adding type is done at datasource initialization step.
      Returns:
      Datasource connection
      Throws:
      SQLException - exception