Skip to content

Commit

Permalink
Merge pull request #435 from quarkiverse/#434
Browse files Browse the repository at this point in the history
  • Loading branch information
cescoffier authored Apr 2, 2024
2 parents 529877a + 43b11d1 commit 4357633
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -906,13 +906,7 @@ private Optional<AiServiceMethodCreateInfo.TemplateInfo> gatherSystemMessageInfo
instance = method.declaringClass().declaredAnnotation(LangChain4jDotNames.SYSTEM_MESSAGE);
}
if (instance != null) {
String systemMessageTemplate = "";
AnnotationValue delimiterValue = instance.value("delimiter");
String delimiter = delimiterValue != null ? delimiterValue.asString() : DEFAULT_DELIMITER;
AnnotationValue value = instance.value();
if (value != null) {
systemMessageTemplate = String.join(delimiter, value.asStringArray());
}
String systemMessageTemplate = getTemplateFromAnnotationInstance(instance);
if (systemMessageTemplate.isEmpty()) {
throw illegalConfigurationForMethod("@SystemMessage's template parameter cannot be empty", method);
}
Expand Down Expand Up @@ -944,9 +938,7 @@ private AiServiceMethodCreateInfo.UserMessageInfo gatherUserMessageInfo(MethodIn

AnnotationInstance userMessageInstance = method.declaredAnnotation(LangChain4jDotNames.USER_MESSAGE);
if (userMessageInstance != null) {
AnnotationValue delimiterValue = userMessageInstance.value("delimiter");
String delimiter = delimiterValue != null ? delimiterValue.asString() : DEFAULT_DELIMITER;
String userMessageTemplate = String.join(delimiter, userMessageInstance.value().asStringArray());
String userMessageTemplate = getTemplateFromAnnotationInstance(userMessageInstance);

if (userMessageTemplate.contains("{{it}}")) {
if (method.parametersCount() != 1) {
Expand Down Expand Up @@ -986,6 +978,39 @@ private AiServiceMethodCreateInfo.UserMessageInfo gatherUserMessageInfo(MethodIn
}
}

/**
* Meant to be called with instances of {@link dev.langchain4j.service.SystemMessage} or
* {@link dev.langchain4j.service.UserMessage}
*
* @return the String value of the template or an empty string if not specified
*/
private String getTemplateFromAnnotationInstance(AnnotationInstance instance) {
AnnotationValue fromResourceValue = instance.value("fromResource");
if (fromResourceValue != null) {
String fromResource = fromResourceValue.asString();
if (!fromResource.startsWith("/")) {
fromResource = "/" + fromResource;

}
try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fromResource)) {
if (is != null) {
return new String(is.readAllBytes());
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else {
AnnotationValue valueValue = instance.value();
if (valueValue != null) {
AnnotationValue delimiterValue = instance.value("delimiter");
String delimiter = delimiterValue != null ? delimiterValue.asString() : DEFAULT_DELIMITER;
return String.join(delimiter, valueValue.asStringArray());
}

}
return "";
}

private Optional<AiServiceMethodCreateInfo.MetricsTimedInfo> gatherMetricsTimedInfo(MethodInfo method,
boolean addMicrometerMetrics) {
if (!addMicrometerMetrics) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public class AiServicesTest {
@RegisterExtension
static final QuarkusUnitTest unitTest = new QuarkusUnitTest()
.setArchiveProducer(
() -> ShrinkWrap.create(JavaArchive.class).addClasses(WiremockUtils.class, MessageAssertUtils.class));
() -> ShrinkWrap.create(JavaArchive.class)
.addClasses(WiremockUtils.class, MessageAssertUtils.class)
.addAsResource("messages/recipe-user.txt")
.addAsResource("messages/translate-user.txt")
.addAsResource("messages/translate-system"));

static WireMockServer wireMockServer;

Expand Down Expand Up @@ -282,7 +286,7 @@ public List<String> getIngredients() {

interface Chef {

@UserMessage("Create recipe using only {{it}}")
@UserMessage(fromResource = "messages/recipe-user.txt")
Recipe createRecipeFrom(String... ingredients);

Recipe createRecipeFrom(CreateRecipePrompt prompt);
Expand Down Expand Up @@ -404,8 +408,8 @@ void test_with_system_message_of_second_method() throws IOException {

interface Translator {

@SystemMessage("You are a professional translator into {{lang}}")
@UserMessage("Translate the following text: {{text}}")
@SystemMessage(fromResource = "messages/translate-system")
@UserMessage(fromResource = "/messages/translate-user.txt")
String translate(@V("text") String text, @V("lang") String language);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Create recipe using only {{it}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
You are a professional translator into {{lang}}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Translate the following text: {{text}}

0 comments on commit 4357633

Please sign in to comment.