Skip to content

Commit 54116ee

Browse files
946 - User message template and variables cannot be null.
1 parent 5f55595 commit 54116ee

File tree

7 files changed

+13
-10
lines changed

7 files changed

+13
-10
lines changed

core/deployment/src/test/java/io/quarkiverse/langchain4j/test/guardrails/InputGuardrailPromptTemplateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ void shouldWorkWithMemoryIdAndOneItemFromList() {
142142
void shouldWorkWithNoUserMessage() {
143143
// UserMessage annotation is not provided, then no user message template should be available
144144
aiService.saySomething("Is this a parameter or a prompt?");
145-
assertThat(guardrailValidation.spyUserMessageTemplate()).isNull();
145+
assertThat(guardrailValidation.spyUserMessageTemplate()).isEmpty();
146146
assertThat(guardrailValidation.spyVariables()).isEmpty();
147147
}
148148

core/deployment/src/test/java/io/quarkiverse/langchain4j/test/guardrails/OutputGuardrailPromptTemplateTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ void shouldWorkWithMemoryIdAndOneItemFromList() {
139139
void shouldWorkWithNoUserMessage() {
140140
// UserMessage annotation is not provided, then no user message template should be available
141141
aiService.saySomething("Is this a parameter or a prompt?");
142-
assertThat(guardrailValidation.spyUserMessageTemplate()).isNull();
142+
assertThat(guardrailValidation.spyUserMessageTemplate()).isEmpty();
143143
assertThat(guardrailValidation.spyVariables()).isEmpty();
144144
}
145145

core/runtime/src/main/java/io/quarkiverse/langchain4j/guardrails/InputGuardrailParams.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
* @param userMessage the user message, cannot be {@code null}
1313
* @param memory the memory, can be {@code null} or empty
1414
* @param augmentationResult the augmentation result, can be {@code null}
15-
* @param userMessageTemplate the user message template, can be {@code null} when @UserMessage is not provided.
16-
* @param variables the variable to be used with userMessageTemplate, can be {@code null} or empty
15+
* @param userMessageTemplate the user message template, cannot be {@code null}
16+
* @param variables the variable to be used with userMessageTemplate, cannot be {@code null}
1717
*/
1818
public record InputGuardrailParams(UserMessage userMessage, ChatMemory memory,
1919
AugmentationResult augmentationResult, String userMessageTemplate,

core/runtime/src/main/java/io/quarkiverse/langchain4j/guardrails/OutputGuardrailParams.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
* @param responseFromLLM the response from the LLM
1313
* @param memory the memory, can be {@code null} or empty
1414
* @param augmentationResult the augmentation result, can be {@code null}
15-
* @param userMessageTemplate the user message template, can be {@code null} when @UserMessage is not provided.
16-
* @param variables the variable to be used with userMessageTemplate, can be {@code null} or empty
15+
* @param userMessageTemplate the user message template, cannot be {@code null}
16+
* @param variables the variable to be used with userMessageTemplate, cannot be {@code null}
1717
*/
1818
public record OutputGuardrailParams(AiMessage responseFromLLM, ChatMemory memory,
1919
AugmentationResult augmentationResult, String userMessageTemplate,

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/aiservice/AiServiceMethodCreateInfo.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.quarkiverse.langchain4j.runtime.aiservice;
22

3+
import static org.apache.commons.lang3.StringUtils.EMPTY;
4+
35
import java.lang.reflect.Type;
46
import java.util.List;
57
import java.util.Map;
@@ -197,7 +199,7 @@ public String getUserMessageTemplate() {
197199
Optional<String> userMessageTemplateOpt = this.getUserMessageInfo().template()
198200
.flatMap(AiServiceMethodCreateInfo.TemplateInfo::text);
199201

200-
return userMessageTemplateOpt.orElse(null);
202+
return userMessageTemplateOpt.orElse(EMPTY);
201203
}
202204

203205
public record UserMessageInfo(Optional<TemplateInfo> template,

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/aiservice/AiServiceMethodImplementationSupport.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ private List<ChatMessage> messagesToSend(ChatMessage augmentedUserMessage,
273273
try {
274274
result = GuardrailsSupport.invokeOutputGuardrailsForStream(methodCreateInfo,
275275
new OutputGuardrailParams(AiMessage.from(chunk), chatMemory, actualAugmentationResult,
276-
methodCreateInfo.getUserMessageTemplate(), templateVariables));
276+
methodCreateInfo.getUserMessageTemplate(),
277+
Collections.unmodifiableMap(templateVariables)));
277278
} catch (Exception e) {
278279
throw new GuardrailException(e.getMessage(), e);
279280
}
@@ -365,7 +366,7 @@ private List<ChatMessage> messagesToSend(ChatMessage augmentedUserMessage,
365366
response = GuardrailsSupport.invokeOutputGuardrails(methodCreateInfo, chatMemory, context.chatModel, response,
366367
toolSpecifications,
367368
new OutputGuardrailParams(response.content(), chatMemory, augmentationResult, userMessageTemplate,
368-
templateVariables));
369+
Collections.unmodifiableMap(templateVariables)));
369370

370371
// everything worked as expected so let's commit the messages
371372
chatMemory.commit();

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/aiservice/GuardrailsSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void invokeInputGuardrails(AiServiceMethodCreateInfo methodCreateI
3737

3838
result = invokeInputGuardRails(methodCreateInfo,
3939
new InputGuardrailParams(userMessage, chatMemory, augmentationResult, userMessageTemplate,
40-
templateVariables));
40+
Collections.unmodifiableMap(templateVariables)));
4141
} catch (Exception e) {
4242
throw new GuardrailException(e.getMessage(), e);
4343
}

0 commit comments

Comments
 (0)