From 971dfa7458f60e27e46f5ce74326efd05eade8e8 Mon Sep 17 00:00:00 2001 From: aldettinger Date: Thu, 12 Dec 2024 12:15:56 +0100 Subject: [PATCH] Add test for AI service resolution by name #6866 --- .../langchain/it/AiServiceResolvedByName.java | 47 +++++++++++++++++++ .../langchain/it/LangChain4jResource.java | 7 +++ .../langchain/it/LangChain4jRoute.java | 3 ++ .../langchain4jit/LangChain4jTest.java | 11 +++++ 4 files changed, 68 insertions(+) create mode 100644 integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/AiServiceResolvedByName.java diff --git a/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/AiServiceResolvedByName.java b/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/AiServiceResolvedByName.java new file mode 100644 index 000000000000..3a318483061b --- /dev/null +++ b/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/AiServiceResolvedByName.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.quarkus.component.langchain.it; + +import java.util.function.Supplier; + +import dev.langchain4j.data.message.AiMessage; +import dev.langchain4j.model.chat.ChatLanguageModel; +import dev.langchain4j.model.output.Response; +import dev.langchain4j.service.UserMessage; +import io.quarkiverse.langchain4j.RegisterAiService; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Named; +import org.apache.camel.Handler; + +@ApplicationScoped +@Named("aiServiceResolvedByName") +@RegisterAiService(chatLanguageModelSupplier = AiServiceResolvedByName.AiServiceResolvedByNameModelSupplier.class) +public interface AiServiceResolvedByName { + + public static class AiServiceResolvedByNameModelSupplier implements Supplier { + @Override + public ChatLanguageModel get() { + return (messages) -> { + return new Response<>(new AiMessage("AiServiceResolvedByName has been resolved")); + }; + } + } + + @UserMessage("Any prompt") + @Handler + String chatByName(String input); +} diff --git a/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/LangChain4jResource.java b/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/LangChain4jResource.java index bbe77035a44c..16e0d6171a27 100644 --- a/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/LangChain4jResource.java +++ b/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/LangChain4jResource.java @@ -46,4 +46,11 @@ public String aiServiceShouldBeResolvableByInterface() { return producerTemplate.requestBody("direct:ai-service-should-be-resolvable-by-interface", "dummy-body", String.class); } + @Path("/ai-service-should-be-resolvable-by-name") + @GET + @Produces(MediaType.TEXT_PLAIN) + public String aiServiceShouldBeResolvableByName() { + return producerTemplate.requestBody("direct:ai-service-should-be-resolvable-by-name", "dummy-body", String.class); + } + } diff --git a/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/LangChain4jRoute.java b/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/LangChain4jRoute.java index c7aa1a8df6cf..2535daafc2a4 100644 --- a/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/LangChain4jRoute.java +++ b/integration-tests/langchain4j/src/main/java/org/apache/camel/quarkus/component/langchain/it/LangChain4jRoute.java @@ -34,5 +34,8 @@ public void configure() { from("direct:ai-service-should-be-resolvable-by-interface") .bean(AiServiceResolvedByInterface.class); + + from("direct:ai-service-should-be-resolvable-by-name") + .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 85e73deb1f48..995b92bc21e0 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,6 +18,7 @@ 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; @@ -44,4 +45,14 @@ void aiServiceShouldBeResolvedByInterface() { .statusCode(200) .body(is("AiServiceResolvedByInterface has been resolved")); } + + @Test + @Disabled("https://github.com/apache/camel-quarkus/issues/6866") + void aiServiceShouldBeResolvedByName() { + RestAssured.given() + .get("/langchain4j/ai-service-should-be-resolvable-by-name") + .then() + .statusCode(200) + .body(is("AiServiceResolvedByName has been resolved")); + } }