Skip to content

Commit d21c17b

Browse files
authored
Merge pull request #1940 from quarkiverse/#1939
Refactor multi-modality handling
2 parents ab2b900 + 579d69b commit d21c17b

File tree

3 files changed

+181
-148
lines changed

3 files changed

+181
-148
lines changed

core/deployment/src/main/java/io/quarkiverse/langchain4j/deployment/AiServicesProcessor.java

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1703,10 +1703,19 @@ private AiServiceMethodCreateInfo gatherMethodMetadata(
17031703
boolean switchToWorkerThreadForToolExecution = detectIfToolExecutionRequiresAWorkerThread(method, tools,
17041704
methodToolClassInfo.keySet());
17051705

1706-
var methodReturnTypeSignature = returnTypeSignature(method.returnType(),
1707-
new TypeArgMapper(method.declaringClass(), index));
1706+
TypeArgMapper typeArgMapper = new TypeArgMapper(method.declaringClass(), index);
1707+
var methodReturnTypeSignature = typeSignature(method.returnType(), typeArgMapper);
17081708

1709-
return new AiServiceMethodCreateInfo(method.declaringClass().name().toString(), method.name(), systemMessageInfo,
1709+
List<AiServiceMethodCreateInfo.ParameterInfo> parameterInfoList = new ArrayList<>();
1710+
for (MethodParameterInfo p : method.parameters()) {
1711+
parameterInfoList.add(new AiServiceMethodCreateInfo.ParameterInfo(p.name(),
1712+
typeSignature(p.type(), typeArgMapper),
1713+
p.declaredAnnotations().stream().map(an -> an.name().toString()).collect(
1714+
Collectors.toSet())));
1715+
}
1716+
1717+
return new AiServiceMethodCreateInfo(method.declaringClass().name().toString(), method.name(), parameterInfoList,
1718+
systemMessageInfo,
17101719
userMessageInfo, memoryIdParamPosition, requiresModeration, methodReturnTypeSignature,
17111720
overrideChatModelParamPosition, metricsTimedInfo, metricsCountedInfo, spanInfo, responseSchemaInfo,
17121721
methodToolClassInfo, methodMcpClientNames, switchToWorkerThreadForToolExecution,
@@ -1817,7 +1826,7 @@ private java.lang.reflect.Type javaLangReturnType(MethodInfo method) {
18171826
}
18181827
}
18191828

1820-
private String returnTypeSignature(Type returnType, TypeArgMapper typeArgMapper) {
1829+
private String typeSignature(Type returnType, TypeArgMapper typeArgMapper) {
18211830
return AsmUtil.getSignature(returnType, typeArgMapper);
18221831
}
18231832

@@ -1979,7 +1988,7 @@ private AiServiceMethodCreateInfo.UserMessageInfo gatherUserMessageInfo(MethodIn
19791988
return AiServiceMethodCreateInfo.UserMessageInfo.fromTemplate(
19801989
AiServiceMethodCreateInfo.TemplateInfo.fromText(userMessageTemplate,
19811990
TemplateParameterInfo.toNameToArgsPositionMap(templateParams)),
1982-
userNameParamPosition, imageParamPosition, audioParamPosition, pdfParamPosition, videoParamPosition);
1991+
userNameParamPosition);
19831992
} else {
19841993
Optional<AnnotationInstance> userMessageOnMethodParam = method.annotations(LangChain4jDotNames.USER_MESSAGE)
19851994
.stream()
@@ -1992,13 +2001,11 @@ private AiServiceMethodCreateInfo.UserMessageInfo gatherUserMessageInfo(MethodIn
19922001
Short.valueOf(userMessageOnMethodParam.get().target().asMethodParameter().position())
19932002
.intValue(),
19942003
TemplateParameterInfo.toNameToArgsPositionMap(templateParams)),
1995-
userNameParamPosition, imageParamPosition, audioParamPosition, pdfParamPosition,
1996-
videoParamPosition);
2004+
userNameParamPosition);
19972005
} else {
19982006
return AiServiceMethodCreateInfo.UserMessageInfo.fromMethodParam(
19992007
userMessageOnMethodParam.get().target().asMethodParameter().position(),
2000-
userNameParamPosition, imageParamPosition, audioParamPosition, pdfParamPosition,
2001-
videoParamPosition);
2008+
userNameParamPosition);
20022009
}
20032010
} else {
20042011
Set<String> templateParamNames = Collections.EMPTY_SET;
@@ -2031,9 +2038,7 @@ private AiServiceMethodCreateInfo.UserMessageInfo gatherUserMessageInfo(MethodIn
20312038
if (undefinedParams > 1) {
20322039
if (fallbackToDummyUserMesage.test(method)) {
20332040
return AiServiceMethodCreateInfo.UserMessageInfo.fromTemplate(
2034-
AiServiceMethodCreateInfo.TemplateInfo.fromText("", Map.of()), Optional.empty(),
2035-
Optional.empty(),
2036-
Optional.empty(), Optional.empty(), Optional.empty());
2041+
AiServiceMethodCreateInfo.TemplateInfo.fromText("", Map.of()), Optional.empty());
20372042
}
20382043

20392044
throw illegalConfigurationForMethod(
@@ -2044,12 +2049,10 @@ private AiServiceMethodCreateInfo.UserMessageInfo gatherUserMessageInfo(MethodIn
20442049
}
20452050
if (userMessageParamPosition == -1) {
20462051
// There is no user message
2047-
return new AiServiceMethodCreateInfo.UserMessageInfo(Optional.empty(), Optional.empty(), Optional.empty(),
2048-
Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
2052+
return new AiServiceMethodCreateInfo.UserMessageInfo(Optional.empty(), Optional.empty(), Optional.empty());
20492053
} else {
20502054
return AiServiceMethodCreateInfo.UserMessageInfo.fromMethodParam(userMessageParamPosition,
2051-
userNameParamPosition,
2052-
imageParamPosition, audioParamPosition, pdfParamPosition, videoParamPosition);
2055+
userNameParamPosition);
20532056

20542057
}
20552058
}

core/runtime/src/main/java/io/quarkiverse/langchain4j/runtime/aiservice/AiServiceMethodCreateInfo.java

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55
import java.util.Map;
66
import java.util.Optional;
7+
import java.util.Set;
78
import java.util.concurrent.ConcurrentHashMap;
89
import java.util.concurrent.CopyOnWriteArrayList;
910
import java.util.function.Supplier;
@@ -29,6 +30,7 @@
2930
public final class AiServiceMethodCreateInfo {
3031
private final String interfaceName;
3132
private final String methodName;
33+
private final List<ParameterInfo> parameterInfo;
3234
private final Optional<TemplateInfo> systemMessageInfo;
3335
private final UserMessageInfo userMessageInfo;
3436
private final Optional<Integer> memoryIdParamPosition;
@@ -65,7 +67,9 @@ public final class AiServiceMethodCreateInfo {
6567
private final boolean switchToWorkerThreadForToolExecution;
6668

6769
@RecordableConstructor
68-
public AiServiceMethodCreateInfo(String interfaceName, String methodName,
70+
public AiServiceMethodCreateInfo(String interfaceName,
71+
String methodName,
72+
List<ParameterInfo> parameterInfo,
6973
Optional<TemplateInfo> systemMessageInfo,
7074
UserMessageInfo userMessageInfo,
7175
Optional<Integer> memoryIdParamPosition,
@@ -85,6 +89,7 @@ public AiServiceMethodCreateInfo(String interfaceName, String methodName,
8589
OutputGuardrailsLiteral outputGuardrails) {
8690
this.interfaceName = interfaceName;
8791
this.methodName = methodName;
92+
this.parameterInfo = parameterInfo;
8893
this.systemMessageInfo = systemMessageInfo;
8994
this.userMessageInfo = userMessageInfo;
9095
this.memoryIdParamPosition = memoryIdParamPosition;
@@ -126,6 +131,10 @@ public String getMethodName() {
126131
return methodName;
127132
}
128133

134+
public List<ParameterInfo> getParameterInfo() {
135+
return parameterInfo;
136+
}
137+
129138
public Optional<TemplateInfo> getSystemMessageInfo() {
130139
return systemMessageInfo;
131140
}
@@ -254,27 +263,15 @@ public void setResponseAugmenter(Class<? extends AiResponseAugmenter<?>> augment
254263

255264
public record UserMessageInfo(Optional<TemplateInfo> template,
256265
Optional<Integer> paramPosition,
257-
Optional<Integer> userNameParamPosition,
258-
Optional<Integer> imageParamPosition,
259-
Optional<Integer> audioParamPosition,
260-
Optional<Integer> pdfParamPosition,
261-
Optional<Integer> videoParamPosition) {
262-
263-
public static UserMessageInfo fromMethodParam(int paramPosition, Optional<Integer> userNameParamPosition,
264-
Optional<Integer> imageParamPosition, Optional<Integer> audioParamPosition,
265-
Optional<Integer> pdfParamPosition,
266-
Optional<Integer> videoParamPosition) {
266+
Optional<Integer> userNameParamPosition) {
267+
268+
public static UserMessageInfo fromMethodParam(int paramPosition, Optional<Integer> userNameParamPosition) {
267269
return new UserMessageInfo(Optional.empty(), Optional.of(paramPosition),
268-
userNameParamPosition, imageParamPosition, audioParamPosition, pdfParamPosition, videoParamPosition);
270+
userNameParamPosition);
269271
}
270272

271-
public static UserMessageInfo fromTemplate(TemplateInfo templateInfo, Optional<Integer> userNameParamPosition,
272-
Optional<Integer> imageUrlParamPosition,
273-
Optional<Integer> audioParamPosition,
274-
Optional<Integer> pdfParamPosition,
275-
Optional<Integer> videoParamPosition) {
276-
return new UserMessageInfo(Optional.of(templateInfo), Optional.empty(), userNameParamPosition,
277-
imageUrlParamPosition, audioParamPosition, pdfParamPosition, videoParamPosition);
273+
public static UserMessageInfo fromTemplate(TemplateInfo templateInfo, Optional<Integer> userNameParamPosition) {
274+
return new UserMessageInfo(Optional.of(templateInfo), Optional.empty(), userNameParamPosition);
278275
}
279276
}
280277

@@ -405,4 +402,8 @@ public static ResponseSchemaInfo of(boolean enabled, Optional<TemplateInfo> syst
405402
structuredOutputSchema);
406403
}
407404
}
405+
406+
public record ParameterInfo(String name, String typeDescriptor, Set<String> annotationTypes) {
407+
408+
}
408409
}

0 commit comments

Comments
 (0)