Skip to content

Commit 00d1c53

Browse files
committed
Fix named AiService handling
1 parent dc1d4ae commit 00d1c53

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,6 @@ public void handleDeclarativeServices(AiServicesRecorder recorder,
718718
allToolProviders.add(toolProvider);
719719
}
720720

721-
bi.getBeanName().ifPresent(beanName -> configurator.named(beanName));
722-
723721
configurator
724722
.addInjectionPoint(ParameterizedType.create(DotNames.CDI_INSTANCE,
725723
new Type[] { ClassType.create(OutputGuardrail.class) }, null))
@@ -1057,12 +1055,16 @@ public void handleAiServices(
10571055
try (ClassCreator classCreator = classCreatorBuilder.build()) {
10581056
if (isRegisteredService) {
10591057
// we need to make this a bean, so we need to add the proper scope annotation
1060-
DotName scopeInfo = declarativeAiServiceItems.stream()
1058+
DeclarativeAiServiceBuildItem matchingBI = declarativeAiServiceItems.stream()
10611059
.filter(bi -> bi.getServiceClassInfo().equals(iface))
10621060
.findFirst().orElseThrow(() -> new IllegalStateException(
1063-
"Unable to determine the CDI scope of " + iface))
1064-
.getCdiScope();
1061+
"Unable to determine the CDI scope of " + iface));
1062+
DotName scopeInfo = matchingBI.getCdiScope();
10651063
classCreator.addAnnotation(scopeInfo.toString());
1064+
if (matchingBI.getBeanName().isPresent()) {
1065+
classCreator.addAnnotation(
1066+
AnnotationInstance.builder(NAMED).add("value", matchingBI.getBeanName().get()).build());
1067+
}
10661068
}
10671069

10681070
FieldDescriptor contextField = classCreator.getFieldCreator("context", QuarkusAiServiceContext.class)

model-providers/openai/openai-vanilla/deployment/src/test/java/org/acme/examples/aiservices/DeclarativeAiServicesTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import jakarta.enterprise.context.ApplicationScoped;
2323
import jakarta.enterprise.context.control.ActivateRequestContext;
2424
import jakarta.inject.Inject;
25+
import jakarta.inject.Named;
2526
import jakarta.inject.Singleton;
2627

2728
import org.eclipse.microprofile.config.inject.ConfigProperty;
@@ -495,6 +496,20 @@ public void test_image_describer() throws Exception {
495496
unwrapped.getClass().getConstructor(QuarkusAiServiceContext.class).getAnnotation(Inject.class);
496497
}
497498

499+
@RegisterAiService
500+
@Named("namedAssistant")
501+
@ApplicationScoped
502+
interface NamedAssistant extends AssistantBase {
503+
504+
String chat(String message);
505+
}
506+
507+
@Test
508+
public void test_named_assistant() throws Exception {
509+
Object namedAssistant = Arc.container().instance("namedAssistant").get();
510+
assertThat(namedAssistant).isInstanceOf(NamedAssistant.class);
511+
}
512+
498513
private void doTestImageDescriber(Supplier<String> describerSupplier) throws IOException {
499514
wiremock().register(post(urlEqualTo("/v1/chat/completions"))
500515
.withRequestBody(matchingJsonPath("$.model", equalTo("gpt-4o-mini")))

0 commit comments

Comments
 (0)