|
| 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.SharedSessionContract; |
| 21 | +import org.hibernate.query.SelectionQuery; |
| 22 | + |
| 23 | +/** |
| 24 | + * Hibernate Assistant allows interacting with an underlying LLM to help you retrieve persistent data. |
| 25 | + * It leverages Hibernate ORM's mapping models, query language, cross-platform support and |
| 26 | + * build-in data restrictions to make access to information stored in relational databases |
| 27 | + * as easy as a natural language prompt. |
| 28 | + */ |
| 29 | +public interface HibernateAssistant { |
| 30 | + /** |
| 31 | + * Creates a {@link SelectionQuery} by providing the specified natural language {@code message} to the LLM |
| 32 | + * and interpreting the obtained response. |
| 33 | + * |
| 34 | + * @param message the natural language prompt |
| 35 | + * @param session Hibernate session |
| 36 | + * |
| 37 | + * @return the {@link SelectionQuery} generated by the LLM |
| 38 | + */ |
| 39 | + default SelectionQuery<?> createAiQuery(String message, SharedSessionContract session) { |
| 40 | + return createAiQuery( message, session, null ); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Creates a {@link SelectionQuery} by providing the specified natural language {@code message} to the LLM |
| 45 | + * and interpreting the obtained response. |
| 46 | + * |
| 47 | + * @param message the natural language prompt |
| 48 | + * @param session Hibernate session |
| 49 | + * @param resultType The {@link Class} representing the expected query result type |
| 50 | + * |
| 51 | + * @return the {@link SelectionQuery} generated by the LLM |
| 52 | + */ |
| 53 | + <T> SelectionQuery<T> createAiQuery(String message, SharedSessionContract session, Class<T> resultType); |
| 54 | + |
| 55 | + /** |
| 56 | + * Prompts the underlying LLM with the provided natural language message and tries to answer it with |
| 57 | + * data extracted from the database through the persistence model. |
| 58 | + * |
| 59 | + * @param message the natural language request |
| 60 | + * @param session Hibernate session |
| 61 | + * |
| 62 | + * @return a natural language response based on the results of the query |
| 63 | + */ |
| 64 | + String executeQuery(String message, SharedSessionContract session); |
| 65 | + |
| 66 | + /** |
| 67 | + * Executes the given {@link SelectionQuery}, and provides a natural language |
| 68 | + * response by passing the resulting data back to the underlying LLM. |
| 69 | + * <p> |
| 70 | + * To directly obtain a natural language response from a natural language prompt, |
| 71 | + * you can use {@link #executeQuery(String, SharedSessionContract)} instead. |
| 72 | + * <p> |
| 73 | + * If you wish to execute the query manually and obtain the structured results yourself, |
| 74 | + * you should use {@link SelectionQuery}'s direct execution methods, e.g. {@link SelectionQuery#getResultList()} |
| 75 | + * or {@link SelectionQuery#getSingleResult()}. |
| 76 | + * |
| 77 | + * @param query the AI query to execute |
| 78 | + * @param session the session in which to execute the query |
| 79 | + * |
| 80 | + * @return a natural language response based on the results of the query |
| 81 | + */ |
| 82 | + String executeQuery(SelectionQuery<?> query, SharedSessionContract session); |
| 83 | + |
| 84 | + /** |
| 85 | + * Reset the assistant's current chat context. This can be helpful when |
| 86 | + * creating a new {@link SelectionQuery} that should not rely on the context |
| 87 | + * of previous requests. |
| 88 | + */ |
| 89 | + void clear(); |
| 90 | +} |
0 commit comments