Class PgVectorEmbeddingStore

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

public class PgVectorEmbeddingStore extends Object implements dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.TextSegment>
PGVector EmbeddingStore Implementation

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

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final DataSource
    Datasource used to create the store
    protected final String
    Embeddings table name
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    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.
    protected
    PgVectorEmbeddingStore(DataSource datasource, String table, Integer dimension, Boolean useIndex, Integer indexListSize, Boolean createTable, Boolean dropTableFirst, MetadataStorageConfig metadataStorageConfig)
    Constructor for PgVectorEmbeddingStore Class
  • Method Summary

    Modifier and Type
    Method
    Description
    add(dev.langchain4j.data.embedding.Embedding embedding)
    Adds a given embedding to the store.
    add(dev.langchain4j.data.embedding.Embedding embedding, dev.langchain4j.data.segment.TextSegment textSegment)
    Adds a given embedding and the corresponding content that has been embedded to the store.
    void
    add(String id, dev.langchain4j.data.embedding.Embedding embedding)
    Adds a given embedding to the store.
    addAll(List<dev.langchain4j.data.embedding.Embedding> embeddings)
    Adds multiple embeddings to the store.
    void
    addAll(List<String> ids, List<dev.langchain4j.data.embedding.Embedding> embeddings, List<dev.langchain4j.data.segment.TextSegment> embedded)
     
    protected Connection
    Datasource connection Creates the vector extension and add the vector type if it does not exist.
    protected void
    initTable(Boolean dropTableFirst, Boolean createTable, Boolean useIndex, Integer dimension, Integer indexListSize)
    Initialize metadata table following configuration
    void
     
    void
    removeAll(dev.langchain4j.store.embedding.filter.Filter filter)
     
    void
     
    dev.langchain4j.store.embedding.EmbeddingSearchResult<dev.langchain4j.data.segment.TextSegment>
    search(dev.langchain4j.store.embedding.EmbeddingSearchRequest request)
    Searches for the most similar (closest in the embedding space) Embeddings.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface dev.langchain4j.store.embedding.EmbeddingStore

    addAll, findRelevant, findRelevant, findRelevant, findRelevant, generateIds, remove
  • 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(dev.langchain4j.data.embedding.Embedding embedding)
      Adds a given embedding to the store.
      Specified by:
      add in interface dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.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, dev.langchain4j.data.embedding.Embedding embedding)
      Adds a given embedding to the store.
      Specified by:
      add in interface dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.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(dev.langchain4j.data.embedding.Embedding embedding, dev.langchain4j.data.segment.TextSegment textSegment)
      Adds a given embedding and the corresponding content that has been embedded to the store.
      Specified by:
      add in interface dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.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<dev.langchain4j.data.embedding.Embedding> embeddings)
      Adds multiple embeddings to the store.
      Specified by:
      addAll in interface dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.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)
      Specified by:
      removeAll in interface dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.TextSegment>
    • removeAll

      public void removeAll(dev.langchain4j.store.embedding.filter.Filter filter)
      Specified by:
      removeAll in interface dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.TextSegment>
    • removeAll

      public void removeAll()
      Specified by:
      removeAll in interface dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.TextSegment>
    • search

      public dev.langchain4j.store.embedding.EmbeddingSearchResult<dev.langchain4j.data.segment.TextSegment> search(dev.langchain4j.store.embedding.EmbeddingSearchRequest request)
      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 dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.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<dev.langchain4j.data.embedding.Embedding> embeddings, List<dev.langchain4j.data.segment.TextSegment> embedded)
      Specified by:
      addAll in interface dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.TextSegment>
    • 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