Skip to content

Commit

Permalink
Fix named AiService handling
Browse files Browse the repository at this point in the history
  • Loading branch information
geoand committed Dec 11, 2024
1 parent dc1d4ae commit 00d1c53
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -718,8 +718,6 @@ public void handleDeclarativeServices(AiServicesRecorder recorder,
allToolProviders.add(toolProvider);
}

bi.getBeanName().ifPresent(beanName -> configurator.named(beanName));

configurator
.addInjectionPoint(ParameterizedType.create(DotNames.CDI_INSTANCE,
new Type[] { ClassType.create(OutputGuardrail.class) }, null))
Expand Down Expand Up @@ -1057,12 +1055,16 @@ public void handleAiServices(
try (ClassCreator classCreator = classCreatorBuilder.build()) {
if (isRegisteredService) {
// we need to make this a bean, so we need to add the proper scope annotation
DotName scopeInfo = declarativeAiServiceItems.stream()
DeclarativeAiServiceBuildItem matchingBI = declarativeAiServiceItems.stream()
.filter(bi -> bi.getServiceClassInfo().equals(iface))
.findFirst().orElseThrow(() -> new IllegalStateException(
"Unable to determine the CDI scope of " + iface))
.getCdiScope();
"Unable to determine the CDI scope of " + iface));
DotName scopeInfo = matchingBI.getCdiScope();
classCreator.addAnnotation(scopeInfo.toString());
if (matchingBI.getBeanName().isPresent()) {
classCreator.addAnnotation(
AnnotationInstance.builder(NAMED).add("value", matchingBI.getBeanName().get()).build());
}
}

FieldDescriptor contextField = classCreator.getFieldCreator("context", QuarkusAiServiceContext.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.control.ActivateRequestContext;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.inject.Singleton;

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

@RegisterAiService
@Named("namedAssistant")
@ApplicationScoped
interface NamedAssistant extends AssistantBase {

String chat(String message);
}

@Test
public void test_named_assistant() throws Exception {
Object namedAssistant = Arc.container().instance("namedAssistant").get();
assertThat(namedAssistant).isInstanceOf(NamedAssistant.class);
}

private void doTestImageDescriber(Supplier<String> describerSupplier) throws IOException {
wiremock().register(post(urlEqualTo("/v1/chat/completions"))
.withRequestBody(matchingJsonPath("$.model", equalTo("gpt-4o-mini")))
Expand Down

0 comments on commit 00d1c53

Please sign in to comment.