From ed401640d1bff6b8799d70ea1b36173e040bdb55 Mon Sep 17 00:00:00 2001 From: Alexandre Gallice Date: Thu, 9 Jan 2025 17:03:49 +0100 Subject: [PATCH] langchain4j: add support for AI service resolution by bean name #6866 (#6899) --- .../reference/extensions/langchain4j.adoc | 31 ++++++++++++++++++- .../runtime/src/main/doc/configuration.adoc | 30 +++++++++++++++++- .../langchain4jit/LangChain4jTest.java | 2 -- 3 files changed, 59 insertions(+), 4 deletions(-) diff --git a/docs/modules/ROOT/pages/reference/extensions/langchain4j.adoc b/docs/modules/ROOT/pages/reference/extensions/langchain4j.adoc index b16974fde90..c3338642012 100644 --- a/docs/modules/ROOT/pages/reference/extensions/langchain4j.adoc +++ b/docs/modules/ROOT/pages/reference/extensions/langchain4j.adoc @@ -91,8 +91,8 @@ With the `camel-quarkus-langchain4j` extension, the AI services are resolvable b For instance, let's define an AI service below: ``` -@RegisterAiService @ApplicationScoped +@RegisterAiService public interface MyAiService { @UserMessage("My Prompt") @@ -111,3 +111,32 @@ public void configure() { } ``` +[id="extensions-langchain4j-configuration-resolving-ai-services-by-bean-name"] +=== Resolving AI services by bean name + +The AI services are also resolvable by bean name when called from a `bean` statement. + +For instance, let's define an AI service below: + +``` +@ApplicationScoped +@Named("aiServiceResolvedByName") +@RegisterAiService +public interface AiServiceResolvedByName { + + @UserMessage("My prompt") + @Handler + String chatByName(String input); +} +``` + +The AI service could then be invoked from a Camel route as below: + +``` +@Override +public void configure() { + from("...") + .bean("aiServiceResolvedByName"); +} +``` + diff --git a/extensions/langchain4j/runtime/src/main/doc/configuration.adoc b/extensions/langchain4j/runtime/src/main/doc/configuration.adoc index 3259dfa6464..384884433da 100644 --- a/extensions/langchain4j/runtime/src/main/doc/configuration.adoc +++ b/extensions/langchain4j/runtime/src/main/doc/configuration.adoc @@ -24,8 +24,8 @@ With the `camel-quarkus-langchain4j` extension, the AI services are resolvable b For instance, let's define an AI service below: ``` -@RegisterAiService @ApplicationScoped +@RegisterAiService public interface MyAiService { @UserMessage("My Prompt") @@ -43,3 +43,31 @@ public void configure() { .bean(MyAiService.class); } ``` + +=== Resolving AI services by bean name + +The AI services are also resolvable by bean name when called from a `bean` statement. + +For instance, let's define an AI service below: + +``` +@ApplicationScoped +@Named("aiServiceResolvedByName") +@RegisterAiService +public interface AiServiceResolvedByName { + + @UserMessage("My prompt") + @Handler + String chatByName(String input); +} +``` + +The AI service could then be invoked from a Camel route as below: + +``` +@Override +public void configure() { + from("...") + .bean("aiServiceResolvedByName"); +} +``` diff --git a/integration-tests/langchain4j/src/test/java/org/apache/camel/quarkus/component/langchain4jit/LangChain4jTest.java b/integration-tests/langchain4j/src/test/java/org/apache/camel/quarkus/component/langchain4jit/LangChain4jTest.java index 995b92bc21e..ddaba2ca9b1 100644 --- a/integration-tests/langchain4j/src/test/java/org/apache/camel/quarkus/component/langchain4jit/LangChain4jTest.java +++ b/integration-tests/langchain4j/src/test/java/org/apache/camel/quarkus/component/langchain4jit/LangChain4jTest.java @@ -18,7 +18,6 @@ import io.quarkus.test.junit.QuarkusTest; import io.restassured.RestAssured; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import static org.hamcrest.Matchers.is; @@ -47,7 +46,6 @@ void aiServiceShouldBeResolvedByInterface() { } @Test - @Disabled("https://github.com/apache/camel-quarkus/issues/6866") void aiServiceShouldBeResolvedByName() { RestAssured.given() .get("/langchain4j/ai-service-should-be-resolvable-by-name")