Class HibernateEmbeddingStore<E>

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

public class HibernateEmbeddingStore<E> extends Object implements dev.langchain4j.store.embedding.EmbeddingStore<dev.langchain4j.data.segment.TextSegment>
Hibernate ORM EmbeddingStore Implementation
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
    static class 
     
    static class 
     
    static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected final boolean
     
    protected final DatabaseKind
     
    protected final DistanceFunction
     
    protected final org.hibernate.metamodel.mapping.AttributeMapping
     
    protected final org.hibernate.metamodel.mapping.AttributeMapping
     
    protected final Class<E>
     
    protected final org.hibernate.persister.entity.EntityPersister
     
    protected final org.hibernate.metamodel.mapping.AttributeMapping
     
    protected final org.hibernate.generator.Generator
     
    protected final org.hibernate.type.descriptor.java.JavaType<Object>
     
    protected final boolean
     
    protected final Map<String, org.hibernate.metamodel.mapping.AttributeMapping>
     
    protected final org.hibernate.SessionFactory
     
    protected final org.hibernate.metamodel.mapping.AttributeMapping
     
    protected final jakarta.persistence.metamodel.Type<Map<?,?>>
     
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
     
     
    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
  • 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.
    addAll(List<dev.langchain4j.data.embedding.Embedding> embeddings, List<dev.langchain4j.data.segment.TextSegment> embedded)
     
    void
    addAll(List<String> idStrings, List<dev.langchain4j.data.embedding.Embedding> embeddings, List<dev.langchain4j.data.segment.TextSegment> embedded)
     
    void
    addAllEntities(List<?> entities)
     
    void
    applyEmbeddings(List<? extends E> entities, dev.langchain4j.model.embedding.EmbeddingModel embeddingModel)
     
    builder(Class<E> entityClass)
    A builder for creating a Hibernate based EmbeddingStore for an existing SessionFactory and entity classes.
    void
     
    List<dev.langchain4j.data.segment.TextSegment>
    createTextSegments(List<? extends E> entities)
     
    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)
     
    A builder for creating a Hibernate based EmbeddingStore when no SessionFactory or entity classes exist.
    A builder for creating a Hibernate based EmbeddingStore when no SessionFactory or entity classes exist and a datasource shall be used.
    generateIds(int n)
     
    query(dev.langchain4j.data.embedding.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.
    query(dev.langchain4j.data.embedding.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.
    query(dev.langchain4j.data.embedding.Embedding embedding, org.hibernate.query.restriction.Restriction<E> restriction)
    Searches for the most similar (closest in the embedding space) entities based on Embeddings.
    void
     
    void
    removeAll(dev.langchain4j.store.embedding.filter.Filter filter)
     
    void
     
    protected jakarta.persistence.criteria.Expression<Double>
    score(DistanceFunction distanceFunction, jakarta.persistence.criteria.Expression<Double> distance, jakarta.persistence.criteria.CriteriaBuilder criteriaBuilder)
     
    dev.langchain4j.store.embedding.EmbeddingSearchResult<dev.langchain4j.data.segment.TextSegment>
    search(dev.langchain4j.data.embedding.Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction)
    Searches for the most similar (closest in the embedding space) Embeddings.
    dev.langchain4j.store.embedding.EmbeddingSearchResult<dev.langchain4j.data.segment.TextSegment>
    search(dev.langchain4j.data.embedding.Embedding embedding, double minScore, org.hibernate.query.restriction.Restriction<E> restriction, int maxResults)
    Searches for the most similar (closest in the embedding space) Embeddings.
    dev.langchain4j.store.embedding.EmbeddingSearchResult<dev.langchain4j.data.segment.TextSegment>
    search(dev.langchain4j.data.embedding.Embedding embedding, org.hibernate.query.restriction.Restriction<E> restriction)
    Searches for the most similar (closest in the embedding space) Embeddings.
    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 Object

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

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

    addListener, addListeners, remove
  • 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(dev.langchain4j.data.embedding.Embedding embedding)
      Adds a given embedding to the store.
      Specified by:
      add in interface dev.langchain4j.store.embedding.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, dev.langchain4j.data.embedding.Embedding embedding)
      Adds a given embedding to the store.
      Specified by:
      add in interface dev.langchain4j.store.embedding.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(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<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<dev.langchain4j.data.embedding.Embedding> embeddings)
      Adds multiple embeddings to the store.
      Specified by:
      addAll in interface dev.langchain4j.store.embedding.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)
      Specified by:
      removeAll in interface dev.langchain4j.store.embedding.EmbeddingStore<E>
    • removeAll

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

      public void removeAll()
      Specified by:
      removeAll in interface dev.langchain4j.store.embedding.EmbeddingStore<E>
    • query

      public List<E> query(dev.langchain4j.data.embedding.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(dev.langchain4j.data.embedding.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(dev.langchain4j.data.embedding.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 dev.langchain4j.store.embedding.EmbeddingSearchResult<dev.langchain4j.data.segment.TextSegment> search(dev.langchain4j.data.embedding.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 dev.langchain4j.store.embedding.EmbeddingSearchResult<dev.langchain4j.data.segment.TextSegment> search(dev.langchain4j.data.embedding.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 dev.langchain4j.store.embedding.EmbeddingSearchResult<dev.langchain4j.data.segment.TextSegment> search(dev.langchain4j.data.embedding.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

      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 data.
      Specified by:
      search in interface dev.langchain4j.store.embedding.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)
    • applyEmbeddings

      public void applyEmbeddings(List<? extends E> entities, dev.langchain4j.model.embedding.EmbeddingModel embeddingModel)
    • createTextSegments

      public List<dev.langchain4j.data.segment.TextSegment> createTextSegments(List<? extends E> entities)
    • generateIds

      public List<String> generateIds(int n)
      Specified by:
      generateIds in interface dev.langchain4j.store.embedding.EmbeddingStore<E>
    • addAll

      public List<String> addAll(List<dev.langchain4j.data.embedding.Embedding> embeddings, List<dev.langchain4j.data.segment.TextSegment> embedded)
      Specified by:
      addAll in interface dev.langchain4j.store.embedding.EmbeddingStore<E>
    • addAll

      public void addAll(List<String> idStrings, List<dev.langchain4j.data.embedding.Embedding> embeddings, List<dev.langchain4j.data.segment.TextSegment> embedded)
      Specified by:
      addAll in interface dev.langchain4j.store.embedding.EmbeddingStore<E>