Class HibernateContentRetriever

java.lang.Object
dev.langchain4j.rag.content.retriever.hibernate.HibernateContentRetriever
All Implemented Interfaces:
ContentRetriever

@Experimental public class HibernateContentRetriever extends Object implements ContentRetriever
Using the 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.
  • Constructor Details

    • HibernateContentRetriever

      @Experimental public HibernateContentRetriever(org.hibernate.SessionFactory sessionFactory, ChatModel chatModel, String databaseStructure, PromptTemplate promptTemplate, Integer maxRetries)
      Creates an instance of a HibernateContentRetriever.
      Parameters:
      sessionFactory - The SessionFactory to be used for executing HQL queries and extracting the metamodel. This is a mandatory parameter.
      chatModel - The ChatModel to 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 the SessionFactory's runtime metamodel automatically (recommended).
      promptTemplate - The PromptTemplate to 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