-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1083 from mariofusco/input-rewrite
Allow rewriting of user messages from input guardrails
- Loading branch information
Showing
8 changed files
with
157 additions
and
32 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...src/test/java/io/quarkiverse/langchain4j/test/guardrails/InputGuardrailRewritingTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.quarkiverse.langchain4j.test.guardrails; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.util.List; | ||
import java.util.function.Supplier; | ||
|
||
import jakarta.enterprise.context.RequestScoped; | ||
import jakarta.enterprise.context.control.ActivateRequestContext; | ||
import jakarta.inject.Inject; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import dev.langchain4j.data.message.AiMessage; | ||
import dev.langchain4j.data.message.ChatMessage; | ||
import dev.langchain4j.memory.ChatMemory; | ||
import dev.langchain4j.memory.chat.ChatMemoryProvider; | ||
import dev.langchain4j.model.chat.ChatLanguageModel; | ||
import dev.langchain4j.model.output.Response; | ||
import dev.langchain4j.service.UserMessage; | ||
import io.quarkiverse.langchain4j.RegisterAiService; | ||
import io.quarkiverse.langchain4j.guardrails.InputGuardrail; | ||
import io.quarkiverse.langchain4j.guardrails.InputGuardrailResult; | ||
import io.quarkiverse.langchain4j.guardrails.InputGuardrails; | ||
import io.quarkiverse.langchain4j.runtime.aiservice.NoopChatMemory; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
|
||
public class InputGuardrailRewritingTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest unitTest = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class) | ||
.addClasses(MyAiService.class, MessageTruncatingGuardrail.class, EchoChatModel.class, | ||
MyChatModelSupplier.class, MyMemoryProviderSupplier.class)); | ||
|
||
@Inject | ||
MyAiService aiService; | ||
|
||
@Test | ||
@ActivateRequestContext | ||
void testRewriting() { | ||
assertEquals(MessageTruncatingGuardrail.MAX_LENGTH, aiService.test("first prompt", "second prompt").length()); | ||
} | ||
|
||
@RegisterAiService(chatLanguageModelSupplier = MyChatModelSupplier.class, chatMemoryProviderSupplier = MyMemoryProviderSupplier.class) | ||
public interface MyAiService { | ||
|
||
@UserMessage("Given {first} and {second} do something") | ||
@InputGuardrails(MessageTruncatingGuardrail.class) | ||
String test(String first, String second); | ||
|
||
} | ||
|
||
@RequestScoped | ||
public static class MessageTruncatingGuardrail implements InputGuardrail { | ||
|
||
static final int MAX_LENGTH = 20; | ||
|
||
@Override | ||
public InputGuardrailResult validate(dev.langchain4j.data.message.UserMessage um) { | ||
String text = um.singleText(); | ||
return successWith(text.substring(0, MAX_LENGTH)); | ||
} | ||
} | ||
|
||
public static class MyChatModelSupplier implements Supplier<ChatLanguageModel> { | ||
|
||
@Override | ||
public ChatLanguageModel get() { | ||
return new EchoChatModel(); | ||
} | ||
} | ||
|
||
public static class EchoChatModel implements ChatLanguageModel { | ||
|
||
@Override | ||
public Response<AiMessage> generate(List<ChatMessage> messages) { | ||
return new Response<>(new AiMessage(((dev.langchain4j.data.message.UserMessage) messages.get(0)).singleText())); | ||
} | ||
} | ||
|
||
public static class MyMemoryProviderSupplier implements Supplier<ChatMemoryProvider> { | ||
@Override | ||
public ChatMemoryProvider get() { | ||
return new ChatMemoryProvider() { | ||
@Override | ||
public ChatMemory get(Object memoryId) { | ||
return new NoopChatMemory(); | ||
} | ||
}; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters