Skip to content

Commit 042b767

Browse files
Merge pull request #50551 from phillip-kruger/assistant-hibernate
Hibernate assistant: use new response type
2 parents 44ec5f4 + 7ca05d6 commit 042b767

File tree

1 file changed

+29
-14
lines changed

1 file changed

+29
-14
lines changed

extensions/hibernate-orm/runtime-dev/src/main/java/io/quarkus/hibernate/orm/dev/ui/HibernateOrmDevJsonRpcService.java

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,25 +77,38 @@ You are an expert in writing Hibernate Query Language (HQL) queries.
7777
7878
{{metamodel}}
7979
80-
If a user asks a question that can be answered by querying this model, generate an HQL SELECT query.
81-
The query must not include any input parameters.
82-
Your response must only contain one field called `hql` containing the HQL query, nothing else, no explanation, and do not put the query in backticks.
83-
Example response: {"hql": "select e from Entity e where e.property = :value"}
80+
8481
""";
8582

86-
private static final String INTERACTIVE_PROMPT = """
83+
private static final String ENGLISH_TO_HQL_USER_PROMPT = """
84+
If a user asks a question that can be answered by querying this model, generate an HQL SELECT query.
85+
The query must not include any input parameters.
86+
The field called `hql` should contain the HQL query, nothing else, no explanation, and do not put the query in backticks.
87+
Example hql field value: "select e from Entity e where e.property = :value"
88+
89+
Here is the user input:
90+
""";
91+
92+
static final record EnglishToHQLResponse(String hql) {
93+
}
94+
95+
private static final String INTERACTIVE_USER_PROMPT = """
8796
The following HQL query:
8897
{{query}}
8998
returned the following data (in JSON format):
9099
{{data}}
91100
92101
Based on the data above, answer this request in natural language:
102+
93103
{{user_request}}
94-
Your response must only contain one field called `answer` containing the natural language response.
104+
105+
The `naturalLanguageResponse` field should contain the natural language response.
95106
Do not include any HQL query in your response, nor suggest any further steps to take.
96-
Example response: {"answer": "..."}
97107
""";
98108

109+
static final record InteractiveResponse(String naturalLanguageResponse) {
110+
}
111+
99112
/**
100113
* Execute an arbitrary {@code hql} query in the given {@code persistence unit}. The query might be both a selection or a
101114
* mutation statement. For selection queries, the result count is retrieved though a count query and the results, paginated
@@ -160,14 +173,15 @@ public CompletionStage<Map<String, String>> executeHQL(
160173

161174
String metamodel = MetamodelJsonSerializerImpl.INSTANCE.toString(sf.getMetamodel());
162175

163-
CompletionStage<Map<String, String>> queryCompletionStage = a.assistBuilder()
176+
CompletionStage<EnglishToHQLResponse> queryCompletionStage = a.assistBuilder()
164177
.systemMessage(SYSTEM_MESSAGE)
165-
.userMessage(query)
178+
.userMessage(ENGLISH_TO_HQL_USER_PROMPT + query)
166179
.addVariable("metamodel", metamodel)
180+
.responseType(EnglishToHQLResponse.class)
167181
.assist();
168182

169183
CompletionStage<DataSet> dataSetCompletionStage = queryCompletionStage.thenApply(response -> {
170-
String hql = response.get("hql");
184+
String hql = response.hql();
171185
if (hql == null || hql.isBlank()) {
172186
return new DataSet(null, null, -1, null, "The assistant did not return a valid HQL query.");
173187
}
@@ -180,17 +194,18 @@ public CompletionStage<Map<String, String>> executeHQL(
180194
// If there was an error executing the query, return it directly
181195
return CompletableFuture.completedStage(toMap(dataSet));
182196
}
183-
CompletionStage<Map<String, String>> interactiveCompletionStage = a.assistBuilder()
197+
CompletionStage<InteractiveResponse> interactiveCompletionStage = a.assistBuilder()
184198
.systemMessage(SYSTEM_MESSAGE)
185199
.addVariable("metamodel", metamodel)
186-
.userMessage(INTERACTIVE_PROMPT)
200+
.userMessage(INTERACTIVE_USER_PROMPT)
187201
.addVariable("query", dataSet.query())
188202
.addVariable("data", dataSet.data())
189203
.addVariable("user_request", query)
204+
.responseType(InteractiveResponse.class)
190205
.assist();
191206
return interactiveCompletionStage.thenApply(response -> {
192-
String answer = response.get("answer");
193-
return messageDataset(dataSet.query(), answer, dataSet.resultCount());
207+
String naturalLanguageResponse = response.naturalLanguageResponse();
208+
return messageDataset(dataSet.query(), naturalLanguageResponse, dataSet.resultCount());
194209
});
195210
});
196211
} else {

0 commit comments

Comments
 (0)