Class HibernateEmbeddingStore<E>

java.lang.Object
dev.langchain4j.store.embedding.hibernate.HibernateEmbeddingStore<E>
All Implemented Interfaces:
EmbeddingStore<TextSegment>

public class HibernateEmbeddingStore<E> extends Object implements EmbeddingStore<TextSegment>
Hibernate ORM EmbeddingStore Implementation
  • Field Details

    • isDynamic

      protected final boolean isDynamic
    • sessionFactory

      protected final org.hibernate.SessionFactory sessionFactory
    • databaseKind

      protected final DatabaseKind databaseKind
    • entityClass

      protected final Class<E> entityClass
    • entityPersister

      protected final org.hibernate.persister.entity.EntityPersister entityPersister
    • idType

      protected final org.hibernate.type.descriptor.java.JavaType<Object> idType
    • idGenerator

      protected final org.hibernate.generator.Generator idGenerator
    • allowUuidGeneration

      protected final boolean allowUuidGeneration
    • idAttributeMapping

      protected final org.hibernate.metamodel.mapping.AttributeMapping idAttributeMapping
    • embeddingAttributeMapping

      protected final org.hibernate.metamodel.mapping.AttributeMapping embeddingAttributeMapping
    • embeddedTextAttributeMapping

      protected final org.hibernate.metamodel.mapping.AttributeMapping embeddedTextAttributeMapping
    • unmappedMetadataAttributeMapping

      protected final org.hibernate.metamodel.mapping.AttributeMapping unmappedMetadataAttributeMapping
    • unmappedMetadataAttributeMapType

      protected final jakarta.persistence.metamodel.Type<Map<?,?>> unmappedMetadataAttributeMapType
    • metadataAttributeMappings

      protected final Map<String, org.hibernate.metamodel.mapping.AttributeMapping> metadataAttributeMappings
    • distanceFunction

      protected final DistanceFunction distanceFunction
  • Constructor Details

    • HibernateEmbeddingStore

      protected HibernateEmbeddingStore(boolean isDynamic, org.hibernate.SessionFactory sessionFactory, DatabaseKind databaseKind, Class<E> entityClass, String embeddingAttributeName, String embeddedTextAttributeName, String unmappedMetadataAttributeName, String[] metadataAttributePaths, DistanceFunction distanceFunction)
      Constructor for HibernateEmbeddingStore Class
      Parameters:
      isDynamic - Whether the session factory was created dynamically
      sessionFactory - The Hibernate session factory to use
      databaseKind - The database kind
      entityClass - The Hibernate entity class to use
      embeddingAttributeName - The name of the entity attribute containing the embedding vector
      embeddedTextAttributeName - The name of the entity attribute containing the text from which the embedding vector is derived, or null
      unmappedMetadataAttributeName - The name of the entity attribute to store generic metadata in
      metadataAttributePaths - The name of the explicit metadata entity attributes
      distanceFunction - The distance function to use for vector search
    • HibernateEmbeddingStore

      public HibernateEmbeddingStore()
  • Method Details

    • builder

      public static <E> HibernateEmbeddingStore.Builder<E> builder(Class<E> entityClass)
      A builder for creating a Hibernate based EmbeddingStore for an existing SessionFactory and entity classes.
      Returns:
      The builder
    • dynamicBuilder

      public static HibernateEmbeddingStore.DynamicBuilder dynamicBuilder()
      A builder for creating a Hibernate based EmbeddingStore when no SessionFactory or entity classes exist.
      Returns:
      The builder
    • dynamicDatasourceBuilder

      public static HibernateEmbeddingStore.DynamicDatasourceBuilder dynamicDatasourceBuilder()
      A builder for creating a Hibernate based EmbeddingStore when no SessionFactory or entity classes exist and a datasource shall be used.
      Returns:
      The builder
    • close

      public void close()
    • add

      public String add(Embedding embedding)
      Adds a given embedding to the store.
      Specified by:
      add in interface EmbeddingStore<E>
      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<E>
      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<E>
      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<E>
      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<E>
      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<E>
      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<E>
    • query

      public List<E> query(Embedding embedding, org.hibernate.query.restriction.Restriction<E> restriction)
      Searches for the most similar (closest in the embedding space) entities based on Embeddings.
      The passed restriction is used to filter by metadata.
      Parameters:
      embedding - The embedding for the query
      restriction - Filter for metadata
      Returns:
      The list of all found entities.
    • query

      public List<E> query(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction)
      Searches for the most similar (closest in the embedding space) entities based on Embeddings.
      The passed restriction is used to filter by metadata.
      Parameters:
      embedding - The embedding for the query
      minScore - The minimum distance score
      restriction - Filter for metadata
      Returns:
      The list of all found entities.
    • query

      public List<E> query(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction, int maxResults)
      Searches for the most similar (closest in the embedding space) entities based on Embeddings.
      The passed restriction is used to filter by metadata.
      Parameters:
      embedding - The embedding for the query
      minScore - The minimum distance score
      restriction - Filter for metadata
      maxResults - The maximum number of results
      Returns:
      The list of all found entities.
    • search

      public EmbeddingSearchResult<TextSegment> search(Embedding embedding, org.hibernate.query.restriction.Restriction<E> restriction)
      Searches for the most similar (closest in the embedding space) Embeddings.
      The passed restriction is used to filter by metadata.
      Parameters:
      embedding - The embedding for the search
      restriction - Filter for metadata
      Returns:
      An EmbeddingSearchResult containing all found Embeddings.
    • search

      public EmbeddingSearchResult<TextSegment> search(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction)
      Searches for the most similar (closest in the embedding space) Embeddings.
      The passed restriction is used to filter by metadata.
      Parameters:
      embedding - The embedding for the search
      minScore - The minimum distance score
      restriction - Filter for metadata
      Returns:
      An EmbeddingSearchResult containing all found Embeddings.
    • search

      public EmbeddingSearchResult<TextSegment> search(Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction, int maxResults)
      Searches for the most similar (closest in the embedding space) Embeddings.
      The passed restriction is used to filter by metadata.
      Parameters:
      embedding - The embedding for the search
      minScore - The minimum distance score
      restriction - Filter for metadata
      maxResults - The maximum number of results
      Returns:
      An EmbeddingSearchResult containing all found Embeddings.
    • 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 data.
      Specified by:
      search in interface EmbeddingStore<E>
      Parameters:
      request - A request to search in an EmbeddingStore. Contains all search criteria.
      Returns:
      An EmbeddingSearchResult containing all found Embeddings.
    • score

      protected jakarta.persistence.criteria.Expression<Double> score(DistanceFunction distanceFunction, jakarta.persistence.criteria.Expression<Double> distance, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder)
    • distanceFilter

      protected jakarta.persistence.criteria.Predicate distanceFilter(DistanceFunction distanceFunction, jakarta.persistence.criteria.Expression<Double> distance, jakarta.persistence.criteria.Expression<Double> minScore, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder)
    • addAllEntities

      public void addAllEntities(List<?> entities)
    • generateIds

      public List<String> generateIds(int n)
      Description copied from interface: EmbeddingStore
      Generates list of UUID strings
      Specified by:
      generateIds in interface EmbeddingStore<E>
      Parameters:
      n - - dimension of list
    • addAll

      public List<String> addAll(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<E>
      Parameters:
      embeddings - A list of embeddings to be added to the store.
      embedded - A list of original contents that were embedded.
      Returns:
      A list of auto-generated IDs associated with the added embeddings.
    • addAll

      public void addAll(List<String> idStrings, 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<E>
      Parameters:
      idStrings - 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.