fixed a bug where in Spring Boot setting, AI Service method parameter is ignored without @V annotation
This commit is contained in:
parent
10176556c6
commit
3a6cb3dc2d
|
@ -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];
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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() {
|
||||
|
||||
|
|
Loading…
Reference in New Issue