forked from quarkiverse/quarkus-langchain4j
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Fix build + rename file to be more explicit + add unit test on Vari…
…ableHandler
- Loading branch information
Showing
5 changed files
with
76 additions
and
10 deletions.
There are no files selected for viewing
67 changes: 67 additions & 0 deletions
67
core/deployment/src/test/java/io/quarkiverse/langchain4j/test/VariableHandlerTest.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,67 @@ | ||
package io.quarkiverse.langchain4j.test; | ||
|
||
import dev.langchain4j.agent.tool.ToolExecutionRequest; | ||
import dev.langchain4j.data.message.AiMessage; | ||
import dev.langchain4j.model.output.TokenUsage; | ||
import io.quarkiverse.langchain4j.data.AiStatsMessage; | ||
import io.quarkiverse.langchain4j.runtime.aiservice.VariableHandler; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
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 static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class VariableHandlerTest { | ||
|
||
@RegisterExtension | ||
static final QuarkusUnitTest unitTest = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)); | ||
|
||
@Test | ||
void test_substitution_on_arguments() { | ||
VariableHandler variableHandler = new VariableHandler(); | ||
|
||
String arguments = "{\"arg0\": 2.0, \"arg1\": $(id1)}"; | ||
|
||
variableHandler.addVariable("id1", "3.1"); | ||
|
||
ToolExecutionRequest request = ToolExecutionRequest.builder() | ||
.arguments(arguments) | ||
.build(); | ||
|
||
ToolExecutionRequest modifiedRequest = variableHandler.substituteVariables(request); | ||
|
||
assertThat(modifiedRequest.arguments()).isEqualTo("{\"arg0\": 2.0, \"arg1\": 3.1}"); | ||
} | ||
|
||
@Test | ||
void test_substitution_on_ai_stats_message() { | ||
VariableHandler variableHandler = new VariableHandler(); | ||
|
||
String text = "The expected result is $(result1)."; | ||
|
||
variableHandler.addVariable("result1", "3.1"); | ||
|
||
AiMessage aiMessage = AiMessage.aiMessage(text); | ||
AiStatsMessage aiStatsMessage = AiStatsMessage.from(aiMessage, new TokenUsage()); | ||
AiMessage modifiedMessage = variableHandler.substituteVariables(aiStatsMessage); | ||
|
||
assertThat(modifiedMessage.text()).isEqualTo("The expected result is 3.1."); | ||
} | ||
|
||
@Test | ||
void test_substitution_on_ai_message() { | ||
VariableHandler variableHandler = new VariableHandler(); | ||
|
||
String text = "The expected result is $(result1)."; | ||
|
||
variableHandler.addVariable("result1", "3.1"); | ||
|
||
AiMessage aiMessage = AiMessage.aiMessage(text); | ||
AiMessage modifiedMessage = variableHandler.substituteVariables(aiMessage); | ||
|
||
assertThat(modifiedMessage.text()).isEqualTo(text); | ||
} | ||
} |
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