From ac62bba39798f1d6511efe9ef4b953962ddb8559 Mon Sep 17 00:00:00 2001 From: Otavio Rodolfo Piske Date: Tue, 3 Dec 2024 10:31:17 +0100 Subject: [PATCH] CAMEL-21503: remove the tools feature from the chat component --- .../chat/LangChain4jChatComponent.java | 6 - .../chat/LangChain4jChatEndpoint.java | 86 +----------- .../chat/LangChain4jChatProducer.java | 55 +------- .../chat/tool/CamelSimpleToolParameter.java | 43 ------ .../chat/tool/CamelToolExecutorCache.java | 59 -------- .../chat/tool/CamelToolSpecification.java | 80 ----------- .../chat/tool/NamedJsonSchemaProperty.java | 41 ------ .../LangChain4jConsumerIT.java | 128 ------------------ 8 files changed, 2 insertions(+), 496 deletions(-) delete mode 100644 components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelSimpleToolParameter.java delete mode 100644 components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelToolExecutorCache.java delete mode 100644 components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelToolSpecification.java delete mode 100644 components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/NamedJsonSchemaProperty.java delete mode 100644 components/camel-ai/camel-langchain4j-chat/src/test/java/org/apache/camel/component/langchain4j.chat/LangChain4jConsumerIT.java diff --git a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatComponent.java b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatComponent.java index 08412be9e1b02..0ceab358b7606 100644 --- a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatComponent.java +++ b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatComponent.java @@ -17,14 +17,12 @@ package org.apache.camel.component.langchain4j.chat; import java.util.Map; -import java.util.stream.Collectors; import org.apache.camel.CamelContext; import org.apache.camel.Endpoint; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.annotations.Component; import org.apache.camel.support.DefaultComponent; -import org.apache.camel.util.PropertiesHelper; import static org.apache.camel.component.langchain4j.chat.LangChain4jChat.SCHEME; @@ -60,11 +58,7 @@ protected Endpoint createEndpoint(String uri, String remaining, Map toolParameters = PropertiesHelper.extractProperties(parameters, "parameter."); Endpoint endpoint = new LangChain4jChatEndpoint(uri, this, remaining, langchain4jChatConfiguration); - ((LangChain4jChatEndpoint) endpoint).setParameters(toolParameters.entrySet().stream() - .collect(Collectors.toMap(Map.Entry::getKey, e -> (String) e.getValue()))); - setProperties(endpoint, parameters); return endpoint; } diff --git a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatEndpoint.java b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatEndpoint.java index 4d57d9434de91..75a6fac7ee717 100644 --- a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatEndpoint.java +++ b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatEndpoint.java @@ -17,24 +17,16 @@ package org.apache.camel.component.langchain4j.chat; import java.util.Map; -import java.util.UUID; -import dev.langchain4j.agent.tool.JsonSchemaProperty; -import dev.langchain4j.agent.tool.ToolSpecification; import org.apache.camel.Category; import org.apache.camel.Consumer; import org.apache.camel.Processor; import org.apache.camel.Producer; -import org.apache.camel.component.langchain4j.chat.tool.CamelSimpleToolParameter; -import org.apache.camel.component.langchain4j.chat.tool.CamelToolExecutorCache; -import org.apache.camel.component.langchain4j.chat.tool.CamelToolSpecification; -import org.apache.camel.component.langchain4j.chat.tool.NamedJsonSchemaProperty; import org.apache.camel.spi.Metadata; import org.apache.camel.spi.UriEndpoint; import org.apache.camel.spi.UriParam; import org.apache.camel.spi.UriPath; import org.apache.camel.support.DefaultEndpoint; -import org.apache.camel.util.StringHelper; import static org.apache.camel.component.langchain4j.chat.LangChain4jChat.SCHEME; @@ -51,19 +43,6 @@ public class LangChain4jChatEndpoint extends DefaultEndpoint { @UriParam private LangChain4jChatConfiguration configuration; - @Metadata(label = "consumer") - @UriParam(description = "Tool description") - private String description; - - @Metadata(label = "consumer") - @UriParam(description = "List of Tool parameters in the form of parameter.=", prefix = "parameter.", - multiValue = true, enums = "string,integer,number,object,array,boolean,null") - private Map parameters; - - @Metadata(label = "consumer,advanced") - @UriParam(description = "Tool's Camel Parameters, programmatically define Tool description and parameters") - private CamelSimpleToolParameter camelToolParameter; - public LangChain4jChatEndpoint(String uri, LangChain4jChatComponent component, String chatId, LangChain4jChatConfiguration configuration) { super(uri, component); @@ -78,44 +57,7 @@ public Producer createProducer() throws Exception { @Override public Consumer createConsumer(Processor processor) throws Exception { - ToolSpecification.Builder toolSpecificationBuilder = ToolSpecification.builder(); - toolSpecificationBuilder.name(UUID.randomUUID().toString()); - if (camelToolParameter != null) { - toolSpecificationBuilder.description(camelToolParameter.getDescription()); - - for (NamedJsonSchemaProperty namedJsonSchemaProperty : camelToolParameter.getProperties()) { - toolSpecificationBuilder.addParameter(namedJsonSchemaProperty.getName(), - namedJsonSchemaProperty.getProperties()); - } - } else if (description != null) { - toolSpecificationBuilder.description(description); - - if (parameters != null) { - parameters.forEach((name, type) -> toolSpecificationBuilder.addParameter(name, JsonSchemaProperty.type(type))); - } - } else { - // Consumer without toolParameter or description - throw new IllegalArgumentException( - "In order to use the langchain4j component as a consumer, you need to specify at least description, or a camelToolParameter"); - } - - String simpleDescription = null; - if (description != null) { - simpleDescription = StringHelper.dashToCamelCase(description.replace(" ", "-")); - } - - ToolSpecification toolSpecification = toolSpecificationBuilder - .name(simpleDescription) - .build(); - - final LangChain4jChatConsumer langChain4jChatConsumer = new LangChain4jChatConsumer(this, processor); - configureConsumer(langChain4jChatConsumer); - - CamelToolSpecification camelToolSpecification - = new CamelToolSpecification(toolSpecification, langChain4jChatConsumer); - CamelToolExecutorCache.getInstance().put(chatId, camelToolSpecification); - - return camelToolSpecification.getConsumer(); + throw new UnsupportedOperationException("Consumer not supported"); } /** @@ -131,34 +73,8 @@ public LangChain4jChatConfiguration getConfiguration() { return configuration; } - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Map getParameters() { - return parameters; - } - - public void setParameters(Map parameters) { - this.parameters = parameters; - } - - public CamelSimpleToolParameter getCamelToolParameter() { - return camelToolParameter; - } - - public void setCamelToolParameter(CamelSimpleToolParameter camelToolParameter) { - this.camelToolParameter = camelToolParameter; - } - @Override protected void doStop() throws Exception { super.doStop(); - - CamelToolExecutorCache.getInstance().getTools().clear(); } } diff --git a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatProducer.java b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatProducer.java index b7cc1c7d58196..7b1a990b44d96 100644 --- a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatProducer.java +++ b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/LangChain4jChatProducer.java @@ -18,16 +18,10 @@ import java.util.List; import java.util.Map; -import java.util.Set; -import java.util.stream.Collectors; -import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; -import dev.langchain4j.agent.tool.ToolExecutionRequest; -import dev.langchain4j.agent.tool.ToolSpecification; import dev.langchain4j.data.message.AiMessage; import dev.langchain4j.data.message.ChatMessage; -import dev.langchain4j.data.message.ToolExecutionResultMessage; import dev.langchain4j.data.message.UserMessage; import dev.langchain4j.model.chat.ChatLanguageModel; import dev.langchain4j.model.input.Prompt; @@ -39,8 +33,6 @@ import org.apache.camel.Exchange; import org.apache.camel.InvalidPayloadException; import org.apache.camel.NoSuchHeaderException; -import org.apache.camel.component.langchain4j.chat.tool.CamelToolExecutorCache; -import org.apache.camel.component.langchain4j.chat.tool.CamelToolSpecification; import org.apache.camel.support.DefaultProducer; import org.apache.camel.util.ObjectHelper; @@ -171,52 +163,7 @@ private String sendListChatMessage(List chatMessages, Exchange exch } - final Map> tools = CamelToolExecutorCache.getInstance().getTools(); - if (tools.containsKey(langChain4jChatEndpoint.getChatId())) { - final Set camelToolSpecificationSet = tools - .get(langChain4jChatEndpoint.getChatId()); - - final List toolSpecifications = camelToolSpecificationSet.stream() - .map(camelToolSpecification -> camelToolSpecification.getToolSpecification()) - .collect(Collectors.toList()); - - response = this.chatLanguageModel.generate(chatMessages, toolSpecifications); - } else { - response = this.chatLanguageModel.generate(chatMessages); - } - - if (response.content().hasToolExecutionRequests()) { - chatMessages.add(response.content()); - - for (ToolExecutionRequest toolExecutionRequest : response.content().toolExecutionRequests()) { - String toolName = toolExecutionRequest.name(); - CamelToolSpecification camelToolSpecification = CamelToolExecutorCache.getInstance().getTools() - .get(langChain4jChatEndpoint.getChatId()).stream() - .filter(cts -> cts.getToolSpecification().name().equals(toolName)) - .findFirst().orElseThrow(() -> new RuntimeException("Tool " + toolName + " not found")); - try { - // Map Json to Header - JsonNode jsonNode = objectMapper.readValue(toolExecutionRequest.arguments(), JsonNode.class); - - jsonNode.fieldNames() - .forEachRemaining(name -> exchange.getMessage().setHeader(name, jsonNode.get(name))); - - // Execute the consumer route - camelToolSpecification.getConsumer().getProcessor().process(exchange); - } catch (Exception e) { - // How to handle this exception? - exchange.setException(e); - } - - chatMessages.add(new ToolExecutionResultMessage( - toolExecutionRequest.id(), - toolExecutionRequest.name(), - exchange.getIn().getBody(String.class))); - } - - response = this.chatLanguageModel.generate(chatMessages); - } - + response = this.chatLanguageModel.generate(chatMessages); return extractAiResponse(response); } diff --git a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelSimpleToolParameter.java b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelSimpleToolParameter.java deleted file mode 100644 index 9d83d1d1fa624..0000000000000 --- a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelSimpleToolParameter.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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.component.langchain4j.chat.tool; - -import java.util.List; - -/** - * langchain4j Simple Tool parameter implementation, this class can be used to provide multiple properties/input - * parameters to the tool itself, the NamedJsonSchemaProperty can be then found as headers into the consumer route - */ -@Deprecated(since = "4.8.0") -public class CamelSimpleToolParameter { - - private final String description; - private final List properties; - - public CamelSimpleToolParameter(String description, List properties) { - this.description = description; - this.properties = properties; - } - - public List getProperties() { - return properties; - } - - public String getDescription() { - return description; - } -} diff --git a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelToolExecutorCache.java b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelToolExecutorCache.java deleted file mode 100644 index 17dcb918ad1de..0000000000000 --- a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelToolExecutorCache.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * 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.component.langchain4j.chat.tool; - -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; - -/** - * Caches Tools Specification and Consumer route reference by the chatId, so that different chats can have different - * Tool implementation - */ -@Deprecated(since = "4.8.0") -public final class CamelToolExecutorCache { - - private Map> tools; - - private CamelToolExecutorCache() { - tools = new ConcurrentHashMap<>(); - } - - private static final class SingletonHolder { - private static final CamelToolExecutorCache INSTANCE = new CamelToolExecutorCache(); - } - - public static CamelToolExecutorCache getInstance() { - return SingletonHolder.INSTANCE; - } - - public void put(String chatId, CamelToolSpecification specification) { - if (tools.get(chatId) != null) { - tools.get(chatId).add(specification); - } else { - Set camelToolSpecifications = new LinkedHashSet<>(); - camelToolSpecifications.add(specification); - tools.put(chatId, camelToolSpecifications); - } - } - - public Map> getTools() { - return tools; - } - -} diff --git a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelToolSpecification.java b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelToolSpecification.java deleted file mode 100644 index 5a5ff039e2fcc..0000000000000 --- a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/CamelToolSpecification.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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.component.langchain4j.chat.tool; - -import java.util.Objects; - -import dev.langchain4j.agent.tool.ToolSpecification; -import org.apache.camel.component.langchain4j.chat.LangChain4jChatConsumer; - -/** - * Holds ToolSpecification needed by langchain4j and the associated Camel Consumer. In this way, a specific route can be - * invoked by a specific Tool - */ -@Deprecated(since = "4.8.0") -public class CamelToolSpecification { - - private ToolSpecification toolSpecification; - private LangChain4jChatConsumer consumer; - - public CamelToolSpecification(ToolSpecification toolSpecification, LangChain4jChatConsumer consumer) { - this.toolSpecification = toolSpecification; - this.consumer = consumer; - } - - public ToolSpecification getToolSpecification() { - return toolSpecification; - } - - public void setToolSpecification(ToolSpecification toolSpecification) { - this.toolSpecification = toolSpecification; - } - - public LangChain4jChatConsumer getConsumer() { - return consumer; - } - - public void setConsumer(LangChain4jChatConsumer consumer) { - this.consumer = consumer; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - CamelToolSpecification that = (CamelToolSpecification) o; - return Objects.equals(toolSpecification, that.toolSpecification) && Objects.equals(consumer, - that.consumer); - } - - @Override - public int hashCode() { - return Objects.hash(toolSpecification, consumer); - } - - @Override - public String toString() { - return "CamelToolSpecification{" + - "toolSpecification=" + toolSpecification + - ", consumer=" + consumer + - '}'; - } -} diff --git a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/NamedJsonSchemaProperty.java b/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/NamedJsonSchemaProperty.java deleted file mode 100644 index 050d2553f0a77..0000000000000 --- a/components/camel-ai/camel-langchain4j-chat/src/main/java/org/apache/camel/component/langchain4j/chat/tool/NamedJsonSchemaProperty.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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.component.langchain4j.chat.tool; - -import java.util.List; - -import dev.langchain4j.agent.tool.JsonSchemaProperty; - -@Deprecated(since = "4.8.0") -public class NamedJsonSchemaProperty { - - private final String name; - private final List properties; - - public NamedJsonSchemaProperty(String name, List properties) { - this.name = name; - this.properties = properties; - } - - public String getName() { - return name; - } - - public List getProperties() { - return properties; - } -} diff --git a/components/camel-ai/camel-langchain4j-chat/src/test/java/org/apache/camel/component/langchain4j.chat/LangChain4jConsumerIT.java b/components/camel-ai/camel-langchain4j-chat/src/test/java/org/apache/camel/component/langchain4j.chat/LangChain4jConsumerIT.java deleted file mode 100644 index aa8748b88907e..0000000000000 --- a/components/camel-ai/camel-langchain4j-chat/src/test/java/org/apache/camel/component/langchain4j.chat/LangChain4jConsumerIT.java +++ /dev/null @@ -1,128 +0,0 @@ -/* - * 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.component.langchain4j.chat; - -import java.util.ArrayList; -import java.util.List; - -import dev.langchain4j.agent.tool.JsonSchemaProperty; -import dev.langchain4j.data.message.ChatMessage; -import dev.langchain4j.data.message.SystemMessage; -import dev.langchain4j.data.message.UserMessage; -import dev.langchain4j.model.chat.ChatLanguageModel; -import dev.langchain4j.model.openai.OpenAiChatModel; -import org.apache.camel.CamelContext; -import org.apache.camel.Exchange; -import org.apache.camel.builder.RouteBuilder; -import org.apache.camel.component.langchain4j.chat.tool.CamelSimpleToolParameter; -import org.apache.camel.component.langchain4j.chat.tool.NamedJsonSchemaProperty; -import org.apache.camel.test.infra.ollama.services.OllamaService; -import org.apache.camel.test.infra.ollama.services.OllamaServiceFactory; -import org.apache.camel.test.junit5.CamelTestSupport; -import org.assertj.core.api.Assertions; -import org.junit.jupiter.api.RepeatedTest; -import org.junit.jupiter.api.condition.DisabledIfSystemProperty; -import org.junit.jupiter.api.extension.RegisterExtension; - -import static java.time.Duration.ofSeconds; - -@DisabledIfSystemProperty(named = "ci.env.name", matches = ".*", disabledReason = "Requires too much network resources") -public class LangChain4jConsumerIT extends CamelTestSupport { - - public static final String MODEL_NAME = "llama3.1:latest"; - private final String nameFromDB = "pippo"; - private ChatLanguageModel chatLanguageModel; - - @RegisterExtension - static OllamaService OLLAMA = OllamaServiceFactory.createServiceWithConfiguration(() -> MODEL_NAME); - - @Override - protected void setupResources() throws Exception { - super.setupResources(); - - chatLanguageModel = createModel(); - } - - @Override - protected CamelContext createCamelContext() throws Exception { - CamelContext context = super.createCamelContext(); - - LangChain4jChatComponent component - = context.getComponent(LangChain4jChat.SCHEME, LangChain4jChatComponent.class); - - component.getConfiguration().setChatModel(chatLanguageModel); - - return context; - } - - protected ChatLanguageModel createModel() { - chatLanguageModel = OpenAiChatModel.builder() - .apiKey("NO_API_KEY") - .modelName(MODEL_NAME) - .baseUrl(OLLAMA.getEndpoint()) - .temperature(0.0) - .timeout(ofSeconds(60)) - .logRequests(true) - .logResponses(true) - .build(); - - return chatLanguageModel; - } - - @Override - protected RouteBuilder createRouteBuilder() { - return new RouteBuilder() { - public void configure() { - - NamedJsonSchemaProperty namedJsonSchemaProperty - = new NamedJsonSchemaProperty("name", List.of(JsonSchemaProperty.STRING)); - CamelSimpleToolParameter camelToolParameter = new CamelSimpleToolParameter( - "This is a tool description", - List.of(namedJsonSchemaProperty)); - context().getRegistry().bind("parameters", camelToolParameter); - - from("direct:test") - .to("langchain4j-chat:test1?chatOperation=CHAT_MULTIPLE_MESSAGES") - .log("response is: ${body}"); - - from("langchain4j-chat:test1?description=Query user database by number¶meter.number=integer") - .setBody(simple("{\"name\": \"pippo\"}")); - - from("langchain4j-chat:test1?camelToolParameter=#parameters") - .setBody(constant("Hello World")); - } - }; - } - - @RepeatedTest(10) - public void testSimpleInvocation() { - List messages = new ArrayList<>(); - messages.add(new SystemMessage( - """ - You provide the requested information using the functions you hava available. You can invoke the functions to obtain the information you need to complete the answer. - """)); - messages.add(new UserMessage(""" - What is the name of the user 1? - """)); - - Exchange message = fluentTemplate.to("direct:test").withBody(messages).request(Exchange.class); - - Assertions.assertThat(message).isNotNull(); - final String responseContent = message.getMessage().getBody().toString(); - Assertions.assertThat(responseContent).containsIgnoringCase(nameFromDB); - } -}