Skip to content

Commit 2a612cc

Browse files
committed
HBX-2996 New module containing natural language building blocks
1 parent 8a02715 commit 2a612cc

16 files changed

+3439
-0
lines changed

language/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!--
2+
~ Copyright 2010 - 2025 Red Hat, Inc.
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ http://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" basis,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
17+
[![Hibernate](https://static.jboss.org/hibernate/images/hibernate_200x150.png)](https://tools.hibernate.org)
18+
19+
# Hibernate Tools fpr Natural Language
20+
21+
This project contains the `HibernateAssistant` interface, which is aimed at providing a natural language interface to Hibernate ORM's persistence capabilities, through the capabilities of modern LLMs. Different providers can implement this interface with their own logic to interact with the underlying Generative AI model.
22+
23+
To be able to interact with different model providers, the `MetamodelSerializer` and `ResultsSerializer` SPIs can be used to generate a textual (JSON) representation of Hibernate's mapping model and data. This text can be easily fed to an LLM that will enable interacting with your database through simple natural language interactions.
24+

language/pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2010 - 2025 Red Hat, Inc.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ http://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" basis,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
<project xmlns="http://maven.apache.org/POM/4.0.0"
18+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
20+
<modelVersion>4.0.0</modelVersion>
21+
<parent>
22+
<groupId>org.hibernate.tool</groupId>
23+
<artifactId>hibernate-tools-parent</artifactId>
24+
<version>7.0.1-SNAPSHOT</version>
25+
</parent>
26+
27+
<artifactId>hibernate-tools-language</artifactId>
28+
29+
<name>Hibernate Tools Natural Language</name>
30+
<description>
31+
Tools to aid Hibernate developers through natural language,
32+
leveraging LLMs and generative AI functionalities.
33+
</description>
34+
<packaging>jar</packaging>
35+
36+
<properties>
37+
<version.org.assertj.assertj-core>3.27.1</version.org.assertj.assertj-core>
38+
<version.com.fasterxml.jackson.core>2.19.0</version.com.fasterxml.jackson.core>
39+
</properties>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>org.hibernate.orm</groupId>
44+
<artifactId>hibernate-core</artifactId>
45+
</dependency>
46+
47+
<!-- test only dependencies -->
48+
<dependency>
49+
<groupId>org.hibernate.orm</groupId>
50+
<artifactId>hibernate-testing</artifactId>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.h2database</groupId>
55+
<artifactId>h2</artifactId>
56+
<scope>test</scope>
57+
</dependency>
58+
<dependency>
59+
<groupId>org.junit.jupiter</groupId>
60+
<artifactId>junit-jupiter-engine</artifactId>
61+
<scope>test</scope>
62+
</dependency>
63+
<dependency>
64+
<groupId>org.assertj</groupId>
65+
<artifactId>assertj-core</artifactId>
66+
<version>${version.org.assertj.assertj-core}</version>
67+
<scope>test</scope>
68+
</dependency>
69+
<dependency>
70+
<groupId>com.fasterxml.jackson.core</groupId>
71+
<artifactId>jackson-core</artifactId>
72+
<version>${version.com.fasterxml.jackson.core}</version>
73+
<scope>test</scope>
74+
</dependency>
75+
<dependency>
76+
<groupId>com.fasterxml.jackson.core</groupId>
77+
<artifactId>jackson-databind</artifactId>
78+
<version>${version.com.fasterxml.jackson.core}</version>
79+
</dependency>
80+
</dependencies>
81+
82+
</project>
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* Hibernate Tools, Tooling for your Hibernate Projects
3+
*
4+
* Copyright 2023-2025 Red Hat, Inc.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" basis,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.hibernate.tool.language;
19+
20+
import org.hibernate.SessionFactory;
21+
import org.hibernate.SharedSessionContract;
22+
import org.hibernate.query.SelectionQuery;
23+
24+
/**
25+
* Hibernate Assistant allows interacting with an underlying LLM to help you retrieve persistent data.
26+
* It leverages Hibernate ORM's mapping models, query language, cross-platform support and
27+
* enhanced security features to make access to information stored in relational databases
28+
* as easy as a natural language prompt.
29+
*/
30+
public interface HibernateAssistant {
31+
/**
32+
* Creates a {@link SelectionQuery} by providing the specified natural language {@code message} to the LLM
33+
* and interpreting the obtained response.
34+
*
35+
* @param message the natural language prompt
36+
* @param session Hibernate session
37+
*
38+
* @return the {@link SelectionQuery} generated by the LLM
39+
*/
40+
default SelectionQuery<?> createAiQuery(String message, SharedSessionContract session) {
41+
return createAiQuery( message, session, null );
42+
}
43+
44+
/**
45+
* Creates a {@link SelectionQuery} by providing the specified natural language {@code message} to the LLM
46+
* and interpreting the obtained response.
47+
*
48+
* @param message the natural language prompt
49+
* @param session Hibernate session
50+
* @param resultType The {@link Class} representing the expected query result type
51+
*
52+
* @return the {@link SelectionQuery} generated by the LLM
53+
*/
54+
<T> SelectionQuery<T> createAiQuery(String message, SharedSessionContract session, Class<T> resultType);
55+
56+
/**
57+
* Prompts the underlying LLM with the provided natural language message and tries to answer it with
58+
* data extracted from the database through the persistence model.
59+
*
60+
* @param message the natural language request
61+
* @param session Hibernate session
62+
*
63+
* @return a natural language response based on the results of the query
64+
*/
65+
String executeQuery(String message, SharedSessionContract session);
66+
67+
/**
68+
* Executes the given {@link SelectionQuery}, and provides a natural language
69+
* response by passing the resulting data back to the underlying LLM.
70+
* <p>
71+
* To directly obtain a natural language response from a natural language prompt,
72+
* you can use {@link #executeQuery(String, SharedSessionContract)} instead.
73+
* <p>
74+
* If you wish to execute the query manually and obtain the structured results yourself,
75+
* you should use {@link SelectionQuery}'s direct execution methods, e.g. {@link SelectionQuery#getResultList()}
76+
* or {@link SelectionQuery#getSingleResult()}.
77+
*
78+
* @param query the AI query to execute
79+
* @param session the session in which to execute the query
80+
*
81+
* @return a natural language response based on the results of the query
82+
*/
83+
String executeQuery(SelectionQuery<?> query, SharedSessionContract session);
84+
85+
/**
86+
* Reset the assistant's current chat context. This can be helpful when
87+
* creating a new {@link SelectionQuery} that should not rely on the context
88+
* of previous requests.
89+
*/
90+
void clear();
91+
}

0 commit comments

Comments
 (0)