Class HibernateContentRetriever
java.lang.Object
dev.langchain4j.rag.content.retriever.hibernate.HibernateContentRetriever
- All Implemented Interfaces:
ContentRetriever
Using the
The generated HQL is guaranteed to be a SELECT statement, as Hibernate's own query parser will reject any non-SELECT HQL. Furthermore, only data contained in tables mapped by entities within the provided
WARNING! All mapped data can be accessed by the content retriever. Make sure the provided
The entity model structure is automatically extracted from Hibernate's metamodel and provided to the LLM to generate valid HQL queries.
Optionally,
The default prompt template is not highly optimized, so it is advised to experiment with it and see what works best for your use case.
SessionFactory and the ChatModel, this ContentRetriever
attempts to generate and execute Hibernate queries for given natural language queries.
The generated HQL is guaranteed to be a SELECT statement, as Hibernate's own query parser will reject any non-SELECT HQL. Furthermore, only data contained in tables mapped by entities within the provided
SessionFactory can be accessed, and existing filters/restrictions will apply.
WARNING! All mapped data can be accessed by the content retriever. Make sure the provided
SessionFactory
only has access to entities that do not contain sensitive information.
The entity model structure is automatically extracted from Hibernate's metamodel and provided to the LLM to generate valid HQL queries.
Optionally,
databaseStructure, promptTemplate, and maxRetries can be specified
to customize the behavior. See the javadoc of the constructor for more details.
Most methods can be overridden to customize the behavior further.
The default prompt template is not highly optimized, so it is advised to experiment with it and see what works best for your use case.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic class -
Constructor Summary
ConstructorsConstructorDescriptionHibernateContentRetriever(org.hibernate.SessionFactory sessionFactory, ChatModel chatModel, String databaseStructure, PromptTemplate promptTemplate, Integer maxRetries) Creates an instance of aHibernateContentRetriever. -
Method Summary
Modifier and TypeMethodDescriptionbuilder()protected Stringprotected Promptprotected static StringdefaultDatabaseStructure(org.hibernate.SessionFactory sessionFactory) protected Stringprotected StringgenerateHqlQuery(Query naturalLanguageQuery, String previousHqlQuery, String previousErrorMessage) Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface ContentRetriever
addListener, addListeners
-
Constructor Details
-
HibernateContentRetriever
@Experimental public HibernateContentRetriever(org.hibernate.SessionFactory sessionFactory, ChatModel chatModel, String databaseStructure, PromptTemplate promptTemplate, Integer maxRetries) Creates an instance of aHibernateContentRetriever.- Parameters:
sessionFactory- TheSessionFactoryto be used for executing HQL queries and extracting the metamodel. This is a mandatory parameter.chatModel- TheChatModelto be used for generating HQL queries. This is a mandatory parameter.databaseStructure- The structure of the entity model, which will be provided to the LLM in the system message. The LLM should be familiar with available entities, attributes, relationships, etc. in order to generate valid HQL queries. This is an optional parameter. If not specified, it will be generated from theSessionFactory's runtime metamodel automatically (recommended).promptTemplate- ThePromptTemplateto be used for creating the system message. This is an optional parameter. Default:DEFAULT_PROMPT_TEMPLATE.maxRetries- The maximum number of retries to perform if Hibernate cannot execute the generated HQL query. An error message will be sent back to the LLM to try correcting the query. This is an optional parameter. Default: 0.
-
-
Method Details
-
builder
-
defaultDatabaseStructure
-
retrieve
Description copied from interface:ContentRetrieverRetrieves relevantContents using a givenQuery. TheContents are sorted by relevance, with the most relevantContents appearing at the beginning of the returnedList<Content>.- Specified by:
retrievein interfaceContentRetriever- Parameters:
naturalLanguageQuery- TheQueryto use for retrieval.- Returns:
- A list of retrieved
Contents.
-
generateHqlQuery
-
createSystemPrompt
-
clean
-
execute
-