Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public boolean configure(CamelContext camelContext, Object obj, String name, Obj
case "autowiredEnabled": target.setAutowiredEnabled(property(camelContext, boolean.class, value)); return true;
case "bridgeerrorhandler":
case "bridgeErrorHandler": target.setBridgeErrorHandler(property(camelContext, boolean.class, value)); return true;
case "chatmemory":
case "chatMemory": getOrCreateConfiguration(target).setChatMemory(property(camelContext, dev.langchain4j.memory.ChatMemory.class, value)); return true;
case "chatmodel":
case "chatModel": getOrCreateConfiguration(target).setChatModel(property(camelContext, dev.langchain4j.model.chat.ChatLanguageModel.class, value)); return true;
case "configuration": target.setConfiguration(property(camelContext, org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration.class, value)); return true;
Expand All @@ -45,7 +47,7 @@ public boolean configure(CamelContext camelContext, Object obj, String name, Obj

@Override
public String[] getAutowiredNames() {
return new String[]{"chatModel"};
return new String[]{"chatMemory", "chatModel"};
}

@Override
Expand All @@ -55,6 +57,8 @@ public Class<?> getOptionType(String name, boolean ignoreCase) {
case "autowiredEnabled": return boolean.class;
case "bridgeerrorhandler":
case "bridgeErrorHandler": return boolean.class;
case "chatmemory":
case "chatMemory": return dev.langchain4j.memory.ChatMemory.class;
case "chatmodel":
case "chatModel": return dev.langchain4j.model.chat.ChatLanguageModel.class;
case "configuration": return org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration.class;
Expand All @@ -72,6 +76,8 @@ public Object getOptionValue(Object obj, String name, boolean ignoreCase) {
case "autowiredEnabled": return target.isAutowiredEnabled();
case "bridgeerrorhandler":
case "bridgeErrorHandler": return target.isBridgeErrorHandler();
case "chatmemory":
case "chatMemory": return getOrCreateConfiguration(target).getChatMemory();
case "chatmodel":
case "chatModel": return getOrCreateConfiguration(target).getChatModel();
case "configuration": return target.getConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class LangChain4jToolsConfigurationConfigurer extends org.apache.camel.su
public boolean configure(CamelContext camelContext, Object obj, String name, Object value, boolean ignoreCase) {
org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration target = (org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration) obj;
switch (ignoreCase ? name.toLowerCase() : name) {
case "chatmemory":
case "chatMemory": target.setChatMemory(property(camelContext, dev.langchain4j.memory.ChatMemory.class, value)); return true;
case "chatmodel":
case "chatModel": target.setChatModel(property(camelContext, dev.langchain4j.model.chat.ChatLanguageModel.class, value)); return true;
default: return false;
Expand All @@ -32,6 +34,8 @@ public boolean configure(CamelContext camelContext, Object obj, String name, Obj
@Override
public Class<?> getOptionType(String name, boolean ignoreCase) {
switch (ignoreCase ? name.toLowerCase() : name) {
case "chatmemory":
case "chatMemory": return dev.langchain4j.memory.ChatMemory.class;
case "chatmodel":
case "chatModel": return dev.langchain4j.model.chat.ChatLanguageModel.class;
default: return null;
Expand All @@ -42,6 +46,8 @@ public Class<?> getOptionType(String name, boolean ignoreCase) {
public Object getOptionValue(Object obj, String name, boolean ignoreCase) {
org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration target = (org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration) obj;
switch (ignoreCase ? name.toLowerCase() : name) {
case "chatmemory":
case "chatMemory": return target.getChatMemory();
case "chatmodel":
case "chatModel": return target.getChatModel();
default: return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public boolean configure(CamelContext camelContext, Object obj, String name, Obj
case "bridgeErrorHandler": target.setBridgeErrorHandler(property(camelContext, boolean.class, value)); return true;
case "cameltoolparameter":
case "camelToolParameter": target.setCamelToolParameter(property(camelContext, org.apache.camel.component.langchain4j.tools.spec.CamelSimpleToolParameter.class, value)); return true;
case "chatmemory":
case "chatMemory": target.getConfiguration().setChatMemory(property(camelContext, dev.langchain4j.memory.ChatMemory.class, value)); return true;
case "chatmodel":
case "chatModel": target.getConfiguration().setChatModel(property(camelContext, dev.langchain4j.model.chat.ChatLanguageModel.class, value)); return true;
case "description": target.setDescription(property(camelContext, java.lang.String.class, value)); return true;
Expand All @@ -44,7 +46,7 @@ public boolean configure(CamelContext camelContext, Object obj, String name, Obj

@Override
public String[] getAutowiredNames() {
return new String[]{"chatModel"};
return new String[]{"chatMemory", "chatModel"};
}

@Override
Expand All @@ -54,6 +56,8 @@ public Class<?> getOptionType(String name, boolean ignoreCase) {
case "bridgeErrorHandler": return boolean.class;
case "cameltoolparameter":
case "camelToolParameter": return org.apache.camel.component.langchain4j.tools.spec.CamelSimpleToolParameter.class;
case "chatmemory":
case "chatMemory": return dev.langchain4j.memory.ChatMemory.class;
case "chatmodel":
case "chatModel": return dev.langchain4j.model.chat.ChatLanguageModel.class;
case "description": return java.lang.String.class;
Expand All @@ -77,6 +81,8 @@ public Object getOptionValue(Object obj, String name, boolean ignoreCase) {
case "bridgeErrorHandler": return target.isBridgeErrorHandler();
case "cameltoolparameter":
case "camelToolParameter": return target.getCamelToolParameter();
case "chatmemory":
case "chatMemory": return target.getConfiguration().getChatMemory();
case "chatmodel":
case "chatModel": return target.getConfiguration().getChatModel();
case "description": return target.getDescription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ public class LangChain4jToolsEndpointUriFactory extends org.apache.camel.support
private static final Set<String> SECRET_PROPERTY_NAMES;
private static final Set<String> MULTI_VALUE_PREFIXES;
static {
Set<String> props = new HashSet<>(10);
Set<String> props = new HashSet<>(11);
props.add("bridgeErrorHandler");
props.add("camelToolParameter");
props.add("chatMemory");
props.add("chatModel");
props.add("description");
props.add("exceptionHandler");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"bridgeErrorHandler": { "index": 1, "kind": "property", "displayName": "Bridge Error Handler", "group": "consumer", "label": "consumer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored." },
"lazyStartProducer": { "index": 2, "kind": "property", "displayName": "Lazy Start Producer", "group": "producer", "label": "producer", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing." },
"autowiredEnabled": { "index": 3, "kind": "property", "displayName": "Autowired Enabled", "group": "advanced", "label": "advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc." },
"chatModel": { "index": 4, "kind": "property", "displayName": "Chat Model", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "dev.langchain4j.model.chat.ChatLanguageModel", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": false, "configurationClass": "org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration", "configurationField": "configuration", "description": "Chat Language Model of type dev.langchain4j.model.chat.ChatLanguageModel" }
"chatMemory": { "index": 4, "kind": "property", "displayName": "Chat Memory", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "dev.langchain4j.memory.ChatMemory", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": false, "configurationClass": "org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration", "configurationField": "configuration", "description": "The chat memory to use" },
"chatModel": { "index": 5, "kind": "property", "displayName": "Chat Model", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "dev.langchain4j.model.chat.ChatLanguageModel", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": false, "configurationClass": "org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration", "configurationField": "configuration", "description": "Chat Language Model of type dev.langchain4j.model.chat.ChatLanguageModel" }
},
"properties": {
"toolId": { "index": 0, "kind": "path", "displayName": "Tool Id", "group": "common", "label": "", "required": true, "type": "string", "javaType": "java.lang.String", "deprecated": false, "deprecationNote": "", "autowired": false, "secret": false, "description": "The tool name" },
Expand All @@ -39,6 +40,7 @@
"exceptionHandler": { "index": 6, "kind": "parameter", "displayName": "Exception Handler", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.spi.ExceptionHandler", "optionalPrefix": "consumer.", "deprecated": false, "autowired": false, "secret": false, "description": "To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored." },
"exchangePattern": { "index": 7, "kind": "parameter", "displayName": "Exchange Pattern", "group": "consumer (advanced)", "label": "consumer,advanced", "required": false, "type": "object", "javaType": "org.apache.camel.ExchangePattern", "enum": [ "InOnly", "InOut" ], "deprecated": false, "autowired": false, "secret": false, "description": "Sets the exchange pattern when the consumer creates an exchange." },
"lazyStartProducer": { "index": 8, "kind": "parameter", "displayName": "Lazy Start Producer", "group": "producer (advanced)", "label": "producer,advanced", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": false, "description": "Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel's routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing." },
"chatModel": { "index": 9, "kind": "parameter", "displayName": "Chat Model", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "dev.langchain4j.model.chat.ChatLanguageModel", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": false, "configurationClass": "org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration", "configurationField": "configuration", "description": "Chat Language Model of type dev.langchain4j.model.chat.ChatLanguageModel" }
"chatMemory": { "index": 9, "kind": "parameter", "displayName": "Chat Memory", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "dev.langchain4j.memory.ChatMemory", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": false, "configurationClass": "org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration", "configurationField": "configuration", "description": "The chat memory to use" },
"chatModel": { "index": 10, "kind": "parameter", "displayName": "Chat Model", "group": "advanced", "label": "advanced", "required": false, "type": "object", "javaType": "dev.langchain4j.model.chat.ChatLanguageModel", "deprecated": false, "deprecationNote": "", "autowired": true, "secret": false, "configurationClass": "org.apache.camel.component.langchain4j.tools.LangChain4jToolsConfiguration", "configurationField": "configuration", "description": "Chat Language Model of type dev.langchain4j.model.chat.ChatLanguageModel" }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.camel.component.langchain4j.tools;

import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.spi.Configurer;
Expand All @@ -31,6 +32,10 @@ public class LangChain4jToolsConfiguration implements Cloneable {
@Metadata(autowired = true)
private ChatLanguageModel chatModel;

@UriParam(label = "advanced", description = "The chat memory to use")
@Metadata(autowired = true)
private ChatMemory chatMemory;

public LangChain4jToolsConfiguration() {
}

Expand All @@ -47,6 +52,14 @@ public void setChatModel(ChatLanguageModel chatModel) {
this.chatModel = chatModel;
}

public ChatMemory getChatMemory() {
return chatMemory;
}

public void setChatMemory(ChatMemory chatMemory) {
this.chatMemory = chatMemory;
}

public LangChain4jToolsConfiguration copy() {
try {
return (LangChain4jToolsConfiguration) super.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import dev.langchain4j.agent.tool.ToolSpecification;
import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.ChatMessage;
import dev.langchain4j.data.message.ChatMessageType;
import dev.langchain4j.data.message.ToolExecutionResultMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.output.Response;
import org.apache.camel.Exchange;
Expand All @@ -43,6 +45,8 @@ public class LangChain4jToolsProducer extends DefaultProducer {

private ChatLanguageModel chatLanguageModel;

private ChatMemory chatMemory;

private final ObjectMapper objectMapper = new ObjectMapper();

public LangChain4jToolsProducer(LangChain4jToolsEndpoint endpoint) {
Expand All @@ -67,6 +71,7 @@ private void processMultipleMessages(Exchange exchange) throws InvalidPayloadExc
protected void doStart() throws Exception {
super.doStart();
this.chatLanguageModel = this.endpoint.getConfiguration().getChatModel();
this.chatMemory = this.endpoint.getConfiguration().getChatMemory();
ObjectHelper.notNull(chatLanguageModel, "chatLanguageModel");
}

Expand Down Expand Up @@ -104,6 +109,10 @@ private String toolsChat(List<ChatMessage> chatMessages, Exchange exchange) {
return null;
}

if (response == null) {
return null;
}

// Then, talk again to call the tools and compute the final response
return chatWithLLMForToolCalling(chatMessages, exchange, response, toolPair);
}
Expand Down Expand Up @@ -137,6 +146,10 @@ private String chatWithLLMForToolCalling(
}

final Response<AiMessage> generate = this.chatLanguageModel.generate(chatMessages);
if (chatMemory != null) {
chatMemory.add(generate.content());
}

return extractAiResponse(generate);
}

Expand All @@ -149,7 +162,19 @@ private String chatWithLLMForToolCalling(
* @return the response provided by the model
*/
private Response<AiMessage> chatWithLLMForTools(List<ChatMessage> chatMessages, ToolPair toolPair, Exchange exchange) {
Response<AiMessage> response = this.chatLanguageModel.generate(chatMessages, toolPair.toolSpecifications());

if (chatMemory != null) {
// first round chat, need to add System and User message. the following rounds only need to add User message.
boolean isEmpty = chatMemory.messages().size() == 0;
for (ChatMessage message : chatMessages) {
if (isEmpty || message.type() == ChatMessageType.USER) {
chatMemory.add(message);
}
}
}

Response<AiMessage> response = this.chatLanguageModel
.generate(chatMemory != null ? chatMemory.messages() : chatMessages, toolPair.toolSpecifications());

if (!response.content().hasToolExecutionRequests()) {
exchange.getMessage().setHeader(LangChain4jTools.NO_TOOLS_CALLED_HEADER, Boolean.TRUE);
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
<camel-cxf-rest.tesb.version>4.8.1.20250320</camel-cxf-rest.tesb.version>
<camel-spring.tesb.version>4.8.1.20250320</camel-spring.tesb.version>
<cxf.tesb.version>4.1.0.2</cxf.tesb.version>
<camel-langchain4j-tools.tesb.version>4.8.1.20250320</camel-langchain4j-tools.tesb.version>
<camel-langchain4j-tools.tesb.version>4.8.1.20250905</camel-langchain4j-tools.tesb.version>
<camel-as2-api.tesb.version>4.8.1.20250320</camel-as2-api.tesb.version>
<camel-aws2-kinesis.tesb.version>4.8.1.20250320</camel-aws2-kinesis.tesb.version>
<camel-aws2-s3.tesb.version>4.8.1.20250320</camel-aws2-s3.tesb.version>
Expand Down