Skip to content

Commit

Permalink
946 - User message template and variables cannot be null.
Browse files Browse the repository at this point in the history
  • Loading branch information
dennysfredericci committed Oct 31, 2024
1 parent 5f55595 commit 54116ee
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ void shouldWorkWithMemoryIdAndOneItemFromList() {
void shouldWorkWithNoUserMessage() {
// UserMessage annotation is not provided, then no user message template should be available
aiService.saySomething("Is this a parameter or a prompt?");
assertThat(guardrailValidation.spyUserMessageTemplate()).isNull();
assertThat(guardrailValidation.spyUserMessageTemplate()).isEmpty();
assertThat(guardrailValidation.spyVariables()).isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void shouldWorkWithMemoryIdAndOneItemFromList() {
void shouldWorkWithNoUserMessage() {
// UserMessage annotation is not provided, then no user message template should be available
aiService.saySomething("Is this a parameter or a prompt?");
assertThat(guardrailValidation.spyUserMessageTemplate()).isNull();
assertThat(guardrailValidation.spyUserMessageTemplate()).isEmpty();
assertThat(guardrailValidation.spyVariables()).isEmpty();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* @param userMessage the user message, cannot be {@code null}
* @param memory the memory, can be {@code null} or empty
* @param augmentationResult the augmentation result, can be {@code null}
* @param userMessageTemplate the user message template, can be {@code null} when @UserMessage is not provided.
* @param variables the variable to be used with userMessageTemplate, can be {@code null} or empty
* @param userMessageTemplate the user message template, cannot be {@code null}
* @param variables the variable to be used with userMessageTemplate, cannot be {@code null}
*/
public record InputGuardrailParams(UserMessage userMessage, ChatMemory memory,
AugmentationResult augmentationResult, String userMessageTemplate,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* @param responseFromLLM the response from the LLM
* @param memory the memory, can be {@code null} or empty
* @param augmentationResult the augmentation result, can be {@code null}
* @param userMessageTemplate the user message template, can be {@code null} when @UserMessage is not provided.
* @param variables the variable to be used with userMessageTemplate, can be {@code null} or empty
* @param userMessageTemplate the user message template, cannot be {@code null}
* @param variables the variable to be used with userMessageTemplate, cannot be {@code null}
*/
public record OutputGuardrailParams(AiMessage responseFromLLM, ChatMemory memory,
AugmentationResult augmentationResult, String userMessageTemplate,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkiverse.langchain4j.runtime.aiservice;

import static org.apache.commons.lang3.StringUtils.EMPTY;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -197,7 +199,7 @@ public String getUserMessageTemplate() {
Optional<String> userMessageTemplateOpt = this.getUserMessageInfo().template()
.flatMap(AiServiceMethodCreateInfo.TemplateInfo::text);

return userMessageTemplateOpt.orElse(null);
return userMessageTemplateOpt.orElse(EMPTY);
}

public record UserMessageInfo(Optional<TemplateInfo> template,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ private List<ChatMessage> messagesToSend(ChatMessage augmentedUserMessage,
try {
result = GuardrailsSupport.invokeOutputGuardrailsForStream(methodCreateInfo,
new OutputGuardrailParams(AiMessage.from(chunk), chatMemory, actualAugmentationResult,
methodCreateInfo.getUserMessageTemplate(), templateVariables));
methodCreateInfo.getUserMessageTemplate(),
Collections.unmodifiableMap(templateVariables)));
} catch (Exception e) {
throw new GuardrailException(e.getMessage(), e);
}
Expand Down Expand Up @@ -365,7 +366,7 @@ private List<ChatMessage> messagesToSend(ChatMessage augmentedUserMessage,
response = GuardrailsSupport.invokeOutputGuardrails(methodCreateInfo, chatMemory, context.chatModel, response,
toolSpecifications,
new OutputGuardrailParams(response.content(), chatMemory, augmentationResult, userMessageTemplate,
templateVariables));
Collections.unmodifiableMap(templateVariables)));

// everything worked as expected so let's commit the messages
chatMemory.commit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public static void invokeInputGuardrails(AiServiceMethodCreateInfo methodCreateI

result = invokeInputGuardRails(methodCreateInfo,
new InputGuardrailParams(userMessage, chatMemory, augmentationResult, userMessageTemplate,
templateVariables));
Collections.unmodifiableMap(templateVariables)));
} catch (Exception e) {
throw new GuardrailException(e.getMessage(), e);
}
Expand Down

0 comments on commit 54116ee

Please sign in to comment.