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 ClassesModifier and TypeClassDescriptionstatic classstatic classstatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final booleanprotected final DatabaseKindprotected final DistanceFunctionprotected final org.hibernate.metamodel.mapping.AttributeMappingprotected final org.hibernate.metamodel.mapping.AttributeMappingprotected final org.hibernate.persister.entity.EntityPersisterprotected final org.hibernate.metamodel.mapping.AttributeMappingprotected final org.hibernate.generator.Generatorprotected final org.hibernate.type.descriptor.java.JavaType<Object> protected final booleanprotected final org.hibernate.SessionFactoryprotected final org.hibernate.metamodel.mapping.AttributeMappingprotected final jakarta.persistence.metamodel.Type<Map<?, ?>> -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedHibernateEmbeddingStore(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 TypeMethodDescriptionadd(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.voidAdds a given embedding to the store.Adds multiple embeddings to the store.addAll(List<dev.langchain4j.data.embedding.Embedding> embeddings, List<dev.langchain4j.data.segment.TextSegment> embedded) voidaddAll(List<String> idStrings, List<dev.langchain4j.data.embedding.Embedding> embeddings, List<dev.langchain4j.data.segment.TextSegment> embedded) voidaddAllEntities(List<?> entities) voidapplyEmbeddings(List<? extends E> entities, dev.langchain4j.model.embedding.EmbeddingModel embeddingModel) static <E> HibernateEmbeddingStore.Builder<E> A builder for creating a Hibernate basedEmbeddingStorefor an existingSessionFactoryand entity classes.voidclose()List<dev.langchain4j.data.segment.TextSegment> createTextSegments(List<? extends E> entities) protected jakarta.persistence.criteria.PredicatedistanceFilter(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 basedEmbeddingStorewhen noSessionFactoryor entity classes exist.A builder for creating a Hibernate basedEmbeddingStorewhen noSessionFactoryor 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 onEmbeddings.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 onEmbeddings.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 onEmbeddings.voidvoidremoveAll(dev.langchain4j.store.embedding.filter.Filter filter) voidremoveAll(Collection<String> ids) 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, waitMethods 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
-
entityClass
-
entityPersister
protected final org.hibernate.persister.entity.EntityPersister entityPersister -
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
-
metadataAttributeMappings
-
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 dynamicallysessionFactory- The Hibernate session factory to usedatabaseKind- The database kindentityClass- The Hibernate entity class to useembeddingAttributeName- The name of the entity attribute containing the embedding vectorembeddedTextAttributeName- The name of the entity attribute containing the text from which the embedding vector is derived, or nullunmappedMetadataAttributeName- The name of the entity attribute to store generic metadata inmetadataAttributePaths- The name of the explicit metadata entity attributesdistanceFunction- The distance function to use for vector search
-
HibernateEmbeddingStore
public HibernateEmbeddingStore()
-
-
Method Details
-
builder
A builder for creating a Hibernate basedEmbeddingStorefor an existingSessionFactoryand entity classes.- Returns:
- The builder
-
dynamicBuilder
A builder for creating a Hibernate basedEmbeddingStorewhen noSessionFactoryor entity classes exist.- Returns:
- The builder
-
dynamicDatasourceBuilder
A builder for creating a Hibernate basedEmbeddingStorewhen noSessionFactoryor entity classes exist and a datasource shall be used.- Returns:
- The builder
-
close
public void close() -
add
Adds a given embedding to the store.- Specified by:
addin interfacedev.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
Adds a given embedding to the store.- Specified by:
addin interfacedev.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:
addin interfacedev.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
Adds multiple embeddings to the store.- Specified by:
addAllin interfacedev.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
- Specified by:
removeAllin interfacedev.langchain4j.store.embedding.EmbeddingStore<E>
-
removeAll
public void removeAll(dev.langchain4j.store.embedding.filter.Filter filter) - Specified by:
removeAllin interfacedev.langchain4j.store.embedding.EmbeddingStore<E>
-
removeAll
public void removeAll()- Specified by:
removeAllin interfacedev.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 onEmbeddings.
The passed restriction is used to filter by metadata.- Parameters:
embedding- The embedding for the queryrestriction- 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 onEmbeddings.
The passed restriction is used to filter by metadata.- Parameters:
embedding- The embedding for the queryminScore- The minimum distance scorerestriction- 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 onEmbeddings.
The passed restriction is used to filter by metadata.- Parameters:
embedding- The embedding for the queryminScore- The minimum distance scorerestriction- Filter for metadatamaxResults- 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 searchrestriction- Filter for metadata- Returns:
- An
EmbeddingSearchResultcontaining all foundEmbeddings.
-
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 searchminScore- The minimum distance scorerestriction- Filter for metadata- Returns:
- An
EmbeddingSearchResultcontaining all foundEmbeddings.
-
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 searchminScore- The minimum distance scorerestriction- Filter for metadatamaxResults- The maximum number of results- Returns:
- An
EmbeddingSearchResultcontaining all foundEmbeddings.
-
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 theEmbeddingSearchRequest.
EmbeddingSearchRequest.filter()is used to filter by meta data.- Specified by:
searchin interfacedev.langchain4j.store.embedding.EmbeddingStore<E>- Parameters:
request- A request to search in anEmbeddingStore. Contains all search criteria.- Returns:
- An
EmbeddingSearchResultcontaining all foundEmbeddings.
-
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
-
applyEmbeddings
-
createTextSegments
-
generateIds
-
addAll
-
addAll
-