|
| 1 | +package io.quarkiverse.langchain4j.test.guardrails; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | +import java.util.Map; |
| 7 | +import java.util.function.Supplier; |
| 8 | + |
| 9 | +import jakarta.enterprise.context.RequestScoped; |
| 10 | +import jakarta.enterprise.context.control.ActivateRequestContext; |
| 11 | +import jakarta.inject.Inject; |
| 12 | + |
| 13 | +import org.jboss.shrinkwrap.api.ShrinkWrap; |
| 14 | +import org.jboss.shrinkwrap.api.spec.JavaArchive; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 17 | + |
| 18 | +import dev.langchain4j.data.message.AiMessage; |
| 19 | +import dev.langchain4j.data.message.ChatMessage; |
| 20 | +import dev.langchain4j.memory.chat.ChatMemoryProvider; |
| 21 | +import dev.langchain4j.model.chat.ChatLanguageModel; |
| 22 | +import dev.langchain4j.model.output.Response; |
| 23 | +import dev.langchain4j.service.MemoryId; |
| 24 | +import dev.langchain4j.service.UserMessage; |
| 25 | +import io.quarkiverse.langchain4j.RegisterAiService; |
| 26 | +import io.quarkiverse.langchain4j.guardrails.InputGuardrail; |
| 27 | +import io.quarkiverse.langchain4j.guardrails.InputGuardrailResult; |
| 28 | +import io.quarkiverse.langchain4j.guardrails.InputGuardrails; |
| 29 | +import io.quarkiverse.langchain4j.runtime.aiservice.NoopChatMemory; |
| 30 | +import io.quarkus.test.QuarkusUnitTest; |
| 31 | + |
| 32 | +public class InputGuardrailPromptTemplateTest { |
| 33 | + |
| 34 | + @RegisterExtension |
| 35 | + static final QuarkusUnitTest unitTest = new QuarkusUnitTest() |
| 36 | + .setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) |
| 37 | + .addClasses(MyAiService.class, MyAiService.class, GuardrailValidation.class, |
| 38 | + MyChatModel.class, MyChatModelSupplier.class, MyMemoryProviderSupplier.class)); |
| 39 | + @Inject |
| 40 | + MyAiService aiService; |
| 41 | + |
| 42 | + @Inject |
| 43 | + GuardrailValidation guardrailValidation; |
| 44 | + |
| 45 | + @Test |
| 46 | + @ActivateRequestContext |
| 47 | + void shouldWorkNoParameters() { |
| 48 | + aiService.getJoke(); |
| 49 | + assertThat(guardrailValidation.spyUserMessageTemplate()).isEqualTo("Tell me a joke"); |
| 50 | + assertThat(guardrailValidation.spyVariables()).isEmpty(); |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + @ActivateRequestContext |
| 55 | + void shouldWorkWithMemoryId() { |
| 56 | + aiService.getAnotherJoke("memory-id-001"); |
| 57 | + assertThat(guardrailValidation.spyUserMessageTemplate()).isEqualTo("Tell me another joke"); |
| 58 | + assertThat(guardrailValidation.spyVariables()).containsExactlyInAnyOrderEntriesOf(Map.of( |
| 59 | + "memoryId", "memory-id-001", |
| 60 | + "it", "memory-id-001" // is this correct? |
| 61 | + )); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + @ActivateRequestContext |
| 66 | + void shouldWorkWithNoMemoryIdAndOneParameter() { |
| 67 | + aiService.sayHiToMyFriendNoMemory("Rambo"); |
| 68 | + assertThat(guardrailValidation.spyUserMessageTemplate()).isEqualTo("Say hi to my friend {friend}!"); |
| 69 | + assertThat(guardrailValidation.spyVariables()) |
| 70 | + .containsExactlyInAnyOrderEntriesOf(Map.of( |
| 71 | + "friend", "Rambo", |
| 72 | + "it", "Rambo")); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + @ActivateRequestContext |
| 77 | + void shouldWorkWithMemoryIdAndOneParameter() { |
| 78 | + aiService.sayHiToMyFriend("1", "Chuck Norris"); |
| 79 | + assertThat(guardrailValidation.spyUserMessageTemplate()).isEqualTo("Say hi to my friend {friend}!"); |
| 80 | + assertThat(guardrailValidation.spyVariables()) |
| 81 | + .containsExactlyInAnyOrderEntriesOf(Map.of( |
| 82 | + "friend", "Chuck Norris", |
| 83 | + "mem", "1")); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + @ActivateRequestContext |
| 88 | + void shouldWorkWithNoMemoryIdAndThreeParameters() { |
| 89 | + aiService.sayHiToMyFriends("Chuck Norris", "Jean-Claude Van Damme", "Silvester Stallone"); |
| 90 | + assertThat(guardrailValidation.spyUserMessageTemplate()) |
| 91 | + .isEqualTo("Tell me something about {topic1}, {topic2}, {topic3}!"); |
| 92 | + assertThat(guardrailValidation.spyVariables()) |
| 93 | + .containsExactlyInAnyOrderEntriesOf(Map.of( |
| 94 | + "topic1", "Chuck Norris", |
| 95 | + "topic2", "Jean-Claude Van Damme", |
| 96 | + "topic3", "Silvester Stallone")); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + @ActivateRequestContext |
| 101 | + void shouldWorkWithNoMemoryIdAndList() { |
| 102 | + aiService.sayHiToMyFriends(List.of("Chuck Norris", "Jean-Claude Van Damme", "Silvester Stallone")); |
| 103 | + assertThat(guardrailValidation.spyUserMessageText()) |
| 104 | + .isEqualTo("Tell me something about [Chuck Norris, Jean-Claude Van Damme, Silvester Stallone]!"); |
| 105 | + assertThat(guardrailValidation.spyUserMessageTemplate()).isEqualTo("Tell me something about {topics}!"); |
| 106 | + assertThat(guardrailValidation.spyVariables()) |
| 107 | + .containsExactlyInAnyOrderEntriesOf(Map.of( |
| 108 | + "topics", List.of("Chuck Norris", "Jean-Claude Van Damme", "Silvester Stallone"), |
| 109 | + "it", List.of("Chuck Norris", "Jean-Claude Van Damme", "Silvester Stallone"))); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + @ActivateRequestContext |
| 114 | + void shouldWorkWithMemoryIdAndList() { |
| 115 | + aiService.sayHiToMyFriends("memory-id-007", List.of("Chuck Norris", "Jean-Claude Van Damme", "Silvester Stallone")); |
| 116 | + assertThat(guardrailValidation.spyUserMessageText()).isEqualTo( |
| 117 | + "Tell me something about [Chuck Norris, Jean-Claude Van Damme, Silvester Stallone]! This is my memory id: memory-id-007"); |
| 118 | + assertThat(guardrailValidation.spyUserMessageTemplate()) |
| 119 | + .isEqualTo("Tell me something about {topics}! This is my memory id: {memoryId}"); |
| 120 | + assertThat(guardrailValidation.spyVariables()) |
| 121 | + .containsExactlyInAnyOrderEntriesOf(Map.of( |
| 122 | + "topics", List.of("Chuck Norris", "Jean-Claude Van Damme", "Silvester Stallone"), |
| 123 | + "memoryId", "memory-id-007")); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + @ActivateRequestContext |
| 128 | + void shouldWorkWithMemoryIdAndOneItemFromList() { |
| 129 | + aiService.sayHiToMyFriend("memory-id-007", List.of("Chuck Norris", "Jean-Claude Van Damme", "Silvester Stallone")); |
| 130 | + assertThat(guardrailValidation.spyUserMessageText()) |
| 131 | + .isEqualTo("Tell me something about Chuck Norris! This is my memory id: memory-id-007"); |
| 132 | + assertThat(guardrailValidation.spyUserMessageTemplate()) |
| 133 | + .isEqualTo("Tell me something about {topics[0]}! This is my memory id: {memoryId}"); |
| 134 | + assertThat(guardrailValidation.spyVariables()) |
| 135 | + .containsExactlyInAnyOrderEntriesOf(Map.of( |
| 136 | + "topics", List.of("Chuck Norris", "Jean-Claude Van Damme", "Silvester Stallone"), |
| 137 | + "memoryId", "memory-id-007")); |
| 138 | + } |
| 139 | + |
| 140 | + @Test |
| 141 | + @ActivateRequestContext |
| 142 | + void shouldWorkWithNoUserMessage() { |
| 143 | + // This is a special case where the UserMessage annotation is not present |
| 144 | + // The prompt template doesn't exist in this case |
| 145 | + // But the current implementation use the parameter name as prompt template |
| 146 | + // Not sure if this is the correct behavior, should we always have @UserMessage? |
| 147 | + // I need some thoughts on this case |
| 148 | + aiService.saySomething("Is this a parameter or a prompt?"); |
| 149 | + assertThat(guardrailValidation.spyUserMessageTemplate()).isNull(); |
| 150 | + assertThat(guardrailValidation.spyVariables()).isEmpty(); |
| 151 | + } |
| 152 | + |
| 153 | + @RegisterAiService(chatLanguageModelSupplier = MyChatModelSupplier.class, chatMemoryProviderSupplier = MyMemoryProviderSupplier.class) |
| 154 | + public interface MyAiService { |
| 155 | + |
| 156 | + @InputGuardrails(GuardrailValidation.class) |
| 157 | + @UserMessage("Tell me a joke") |
| 158 | + String getJoke(); |
| 159 | + |
| 160 | + @UserMessage("Tell me another joke") |
| 161 | + @InputGuardrails(GuardrailValidation.class) |
| 162 | + String getAnotherJoke(@MemoryId String memoryId); |
| 163 | + |
| 164 | + @UserMessage("Say hi to my friend {friend}!") |
| 165 | + @InputGuardrails(GuardrailValidation.class) |
| 166 | + String sayHiToMyFriendNoMemory(String friend); |
| 167 | + |
| 168 | + @UserMessage("Say hi to my friend {friend}!") |
| 169 | + @InputGuardrails(GuardrailValidation.class) |
| 170 | + String sayHiToMyFriend(@MemoryId String mem, String friend); |
| 171 | + |
| 172 | + @UserMessage("Tell me something about {topic1}, {topic2}, {topic3}!") |
| 173 | + @InputGuardrails(GuardrailValidation.class) |
| 174 | + String sayHiToMyFriends(String topic1, String topic2, String topic3); |
| 175 | + |
| 176 | + @UserMessage("Tell me something about {topics}!") |
| 177 | + @InputGuardrails(GuardrailValidation.class) |
| 178 | + String sayHiToMyFriends(List<String> topics); |
| 179 | + |
| 180 | + @UserMessage("Tell me something about {topics}! This is my memory id: {memoryId}") |
| 181 | + @InputGuardrails(GuardrailValidation.class) |
| 182 | + String sayHiToMyFriends(@MemoryId String memoryId, List<String> topics); |
| 183 | + |
| 184 | + @UserMessage("Tell me something about {topics[0]}! This is my memory id: {memoryId}") |
| 185 | + @InputGuardrails(GuardrailValidation.class) |
| 186 | + String sayHiToMyFriend(@MemoryId String memoryId, List<String> topics); |
| 187 | + |
| 188 | + @InputGuardrails(GuardrailValidation.class) |
| 189 | + String saySomething(String isThisAPromptOrAParameter); |
| 190 | + |
| 191 | + } |
| 192 | + |
| 193 | + @RequestScoped |
| 194 | + public static class GuardrailValidation implements InputGuardrail { |
| 195 | + |
| 196 | + InputGuardrailParams params; |
| 197 | + |
| 198 | + public InputGuardrailResult validate(InputGuardrailParams params) { |
| 199 | + this.params = params; |
| 200 | + return success(); |
| 201 | + } |
| 202 | + |
| 203 | + public String spyUserMessageTemplate() { |
| 204 | + return params.userMessageTemplate(); |
| 205 | + } |
| 206 | + |
| 207 | + public String spyUserMessageText() { |
| 208 | + return params.userMessage().singleText(); |
| 209 | + } |
| 210 | + |
| 211 | + public Map<String, Object> spyVariables() { |
| 212 | + return params.variables(); |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + public static class MyChatModelSupplier implements Supplier<ChatLanguageModel> { |
| 217 | + |
| 218 | + @Override |
| 219 | + public ChatLanguageModel get() { |
| 220 | + return new MyChatModel(); |
| 221 | + } |
| 222 | + } |
| 223 | + |
| 224 | + public static class MyChatModel implements ChatLanguageModel { |
| 225 | + |
| 226 | + @Override |
| 227 | + public Response<AiMessage> generate(List<ChatMessage> messages) { |
| 228 | + return new Response<>(new AiMessage("Hi!")); |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + public static class MyMemoryProviderSupplier implements Supplier<ChatMemoryProvider> { |
| 233 | + @Override |
| 234 | + public ChatMemoryProvider get() { |
| 235 | + return memoryId -> new NoopChatMemory(); |
| 236 | + } |
| 237 | + } |
| 238 | +} |
0 commit comments