fixed a bug where in Spring Boot setting, AI Service method parameter is ignored without @V annotation

This commit is contained in:
LangChain4j 2024-09-20 16:27:11 +02:00
parent 10176556c6
commit 3a6cb3dc2d
3 changed files with 31 additions and 7 deletions

View File

@ -366,13 +366,10 @@ class DefaultAiServices<T> extends AiServices<T> {
Map<String, Object> variables = new HashMap<>();
for (int i = 0; i < parameters.length; i++) {
V annotation = parameters[i].getAnnotation(V.class);
if (annotation != null) {
String variableName = annotation.value();
String variableName = getVariableName(parameters[i]);
Object variableValue = args[i];
variables.put(variableName, variableValue);
}
}
if (template.contains("{{it}}") && !variables.containsKey("it")) {
String itValue = getValueOfVariableIt(parameters, args);
@ -382,6 +379,15 @@ class DefaultAiServices<T> extends AiServices<T> {
return variables;
}
private static String getVariableName(Parameter parameter) {
V annotation = parameter.getAnnotation(V.class);
if (annotation != null) {
return annotation.value();
} else {
return parameter.getName();
}
}
private static String getValueOfVariableIt(Parameter[] parameters, Object[] args) {
if (parameters.length == 1) {
Parameter parameter = parameters[0];

View File

@ -29,7 +29,7 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME;
* String chat(@V String name, @V int age);
* </pre>
* <p>
* When using Spring Boot, defining the value of this annotation is not required.
* When using LangChain4j with Quarkus or Spring Boot, using this annotation is not necessary.
*
* @see UserMessage
* @see SystemMessage

View File

@ -47,6 +47,9 @@ class AiServicesUserMessageConfigTest {
@UserMessage("What is the {{it}} of {{country}}?")
String chat7(@V("it") String it, @V("country") String country);
@UserMessage("What is the capital of {{arg0}}?")
String chat8(String country);
// illegal configuration
String illegalChat1();
@ -172,6 +175,21 @@ class AiServicesUserMessageConfigTest {
verify(chatLanguageModel).supportedCapabilities();
}
@Test
void test_user_message_configuration_8() {
// given
AiService aiService = AiServices.builder(AiService.class)
.chatLanguageModel(chatLanguageModel)
.build();
// when-then
assertThat(aiService.chat8("Germany"))
.containsIgnoringCase("Berlin");
verify(chatLanguageModel).generate(singletonList(userMessage("What is the capital of Germany?")));
verify(chatLanguageModel).supportedCapabilities();
}
@Test
void test_illegal_user_message_configuration_1() {