This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 21f1f7ce187b CAMEL-25863: camel-langchain4j-chat - the docs say that
the body is the message and the answer replaces it (#28143)
21f1f7ce187b is described below
commit 21f1f7ce187b8a00d355ce56caabe625c754e14a
Author: Claus Ibsen <[email protected]>
AuthorDate: Tue Oct 20 15:08:02 2026 +0200
CAMEL-25863: camel-langchain4j-chat - the docs say that the body is the
message and the answer replaces it (#28143)
The page did not say that the body is sent as the user message and that the
answer of the model replaces it. A new
section shows, in Java, XML and YAML, a prompt built in a variable and the
answer received in another (variableSend,
variableReceive), so the body stays as it was. The section on templates
names the header and says that {{name}} is a
property placeholder in YAML, XML and properties files. The error for a
missing template names the header.
Closes #28143
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
---
.../catalog/docs/langchain4j-chat-component.adoc | 65 ++++++++++++++++++++++
.../src/main/docs/langchain4j-chat-component.adoc | 65 ++++++++++++++++++++++
.../langchain4j/chat/LangChain4jChatProducer.java | 3 +-
3 files changed, 132 insertions(+), 1 deletion(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/langchain4j-chat-component.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/langchain4j-chat-component.adoc
index d33dbe8b1c13..3cc503c82941 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/langchain4j-chat-component.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/langchain4j-chat-component.adoc
@@ -93,6 +93,65 @@ and `timeout`, set under the name the provider's builder
uses for them (`apiKey`
Endpoints with the same provider options share one model instance. The
provider is ignored when a model bean is
configured (`chatModel`).
+=== The body is the message, and the answer replaces it
+
+The component sends the body of the message to the model as the user message,
and the answer of the model, a
+`String`, replaces the body. To keep the data the route works on, such as a
review that it logs or sends on, build the
+prompt in a variable and receive the answer in another variable, with
`variableSend` and `variableReceive` of the `to`.
+The body stays as it was:
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:review")
+ .setVariable("prompt", simple("Answer with one word, positive or negative:
is this review positive? ${body[comment]}"))
+ .toV("langchain4j-chat:sentiment", "prompt", "sentiment")
+ .log("Review ${body[id]} is ${variable.sentiment}");
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+ <from uri="direct:review"/>
+ <setVariable name="prompt">
+ <simple>Answer with one word, positive or negative: is this review
positive? ${body[comment]}</simple>
+ </setVariable>
+ <to uri="langchain4j-chat:sentiment" variableSend="prompt"
variableReceive="sentiment"/>
+ <log message="Review ${body[id]} is ${variable.sentiment}"/>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+ from:
+ uri: direct:review
+ steps:
+ - setVariable:
+ name: prompt
+ expression:
+ simple:
+ expression: "Answer with one word, positive or negative: is
this review positive? ${body[comment]}"
+ - to:
+ uri: langchain4j-chat:sentiment
+ variableSend: prompt
+ variableReceive: sentiment
+ - log:
+ message: "Review ${body[id]} is ${variable.sentiment}"
+----
+====
+
+The review is a `Map` here (such as after `unmarshal` of JSON), and the model
is configured as in
+<<_configuring_the_model_by_provider,Configuring the model by provider>>.
Without `variableSend`, the body itself is the prompt; without
+`variableReceive`, the answer replaces the body, and `${body[id]}` reads the
answer of the model.
+
=== Using a specific Chat Model
The Camel LangChain4j chat component provides an abstraction for interacting
with various types of Large Language Models (LLMs) supported by
https://github.com/langchain4j/langchain4j[LangChain4j].
@@ -261,6 +320,12 @@ To switch to another Large Language Model and its
corresponding dependency, repl
To send a prompt with variables, use the Operation type
`LangChain4jChatOperations.CHAT_SINGLE_MESSAGE_WITH_PROMPT`.
This operation allows you to send a single prompt message with dynamic
variables, which will be replaced with values provided in the request.
+The template is the header `CamelLangChain4jChatPromptTemplate`
(`LangChain4jChatHeaders.PROMPT_TEMPLATE`), and the
+body is a `Map` with the values of its variables.
+
+NOTE: In YAML and XML routes, and in properties files, `{{name}}` is a
property placeholder, which Camel resolves
+before the route runs. There, build the prompt with the simple language
instead, as in
+<<_the_body_is_the_message_and_the_answer_replaces_it,The body is the
message>>.
.Route example:
[source, java]
diff --git
a/components/camel-ai/camel-langchain4j-chat/src/main/docs/langchain4j-chat-component.adoc
b/components/camel-ai/camel-langchain4j-chat/src/main/docs/langchain4j-chat-component.adoc
index d33dbe8b1c13..3cc503c82941 100644
---
a/components/camel-ai/camel-langchain4j-chat/src/main/docs/langchain4j-chat-component.adoc
+++
b/components/camel-ai/camel-langchain4j-chat/src/main/docs/langchain4j-chat-component.adoc
@@ -93,6 +93,65 @@ and `timeout`, set under the name the provider's builder
uses for them (`apiKey`
Endpoints with the same provider options share one model instance. The
provider is ignored when a model bean is
configured (`chatModel`).
+=== The body is the message, and the answer replaces it
+
+The component sends the body of the message to the model as the user message,
and the answer of the model, a
+`String`, replaces the body. To keep the data the route works on, such as a
review that it logs or sends on, build the
+prompt in a variable and receive the answer in another variable, with
`variableSend` and `variableReceive` of the `to`.
+The body stays as it was:
+
+[tabs]
+====
+Java::
++
+[source,java]
+----
+from("direct:review")
+ .setVariable("prompt", simple("Answer with one word, positive or negative:
is this review positive? ${body[comment]}"))
+ .toV("langchain4j-chat:sentiment", "prompt", "sentiment")
+ .log("Review ${body[id]} is ${variable.sentiment}");
+----
+
+XML::
++
+[source,xml]
+----
+<route>
+ <from uri="direct:review"/>
+ <setVariable name="prompt">
+ <simple>Answer with one word, positive or negative: is this review
positive? ${body[comment]}</simple>
+ </setVariable>
+ <to uri="langchain4j-chat:sentiment" variableSend="prompt"
variableReceive="sentiment"/>
+ <log message="Review ${body[id]} is ${variable.sentiment}"/>
+</route>
+----
+
+YAML::
++
+[source,yaml]
+----
+- route:
+ from:
+ uri: direct:review
+ steps:
+ - setVariable:
+ name: prompt
+ expression:
+ simple:
+ expression: "Answer with one word, positive or negative: is
this review positive? ${body[comment]}"
+ - to:
+ uri: langchain4j-chat:sentiment
+ variableSend: prompt
+ variableReceive: sentiment
+ - log:
+ message: "Review ${body[id]} is ${variable.sentiment}"
+----
+====
+
+The review is a `Map` here (such as after `unmarshal` of JSON), and the model
is configured as in
+<<_configuring_the_model_by_provider,Configuring the model by provider>>.
Without `variableSend`, the body itself is the prompt; without
+`variableReceive`, the answer replaces the body, and `${body[id]}` reads the
answer of the model.
+
=== Using a specific Chat Model
The Camel LangChain4j chat component provides an abstraction for interacting
with various types of Large Language Models (LLMs) supported by
https://github.com/langchain4j/langchain4j[LangChain4j].
@@ -261,6 +320,12 @@ To switch to another Large Language Model and its
corresponding dependency, repl
To send a prompt with variables, use the Operation type
`LangChain4jChatOperations.CHAT_SINGLE_MESSAGE_WITH_PROMPT`.
This operation allows you to send a single prompt message with dynamic
variables, which will be replaced with values provided in the request.
+The template is the header `CamelLangChain4jChatPromptTemplate`
(`LangChain4jChatHeaders.PROMPT_TEMPLATE`), and the
+body is a `Map` with the values of its variables.
+
+NOTE: In YAML and XML routes, and in properties files, `{{name}}` is a
property placeholder, which Camel resolves
+before the route runs. There, build the prompt with the simple language
instead, as in
+<<_the_body_is_the_message_and_the_answer_replaces_it,The body is the
message>>.
.Route example:
[source, java]
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 344de91051b4..a8dbc42228c2 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
@@ -78,7 +78,8 @@ public class LangChain4jChatProducer extends DefaultProducer {
final String promptTemplate =
exchange.getIn().getHeader(LangChain4jChatHeaders.PROMPT_TEMPLATE,
String.class);
if (promptTemplate == null) {
throw new NoSuchHeaderException(
- "The promptTemplate is a required header", exchange,
LangChain4jChatHeaders.PROMPT_TEMPLATE);
+ "The prompt template is a required header: " +
LangChain4jChatHeaders.PROMPT_TEMPLATE, exchange,
+ LangChain4jChatHeaders.PROMPT_TEMPLATE);
}
Map<String, Object> variables = (Map<String, Object>)
exchange.getIn().getMandatoryBody(Map.class);