Zineb Bendhiba created CAMEL-23695:
--------------------------------------
Summary: Langchain4j-agent: improve structured output when
Langchain4j 1.16.0 is out
Key: CAMEL-23695
URL: https://issues.apache.org/jira/browse/CAMEL-23695
Project: Camel
Issue Type: Improvement
Reporter: Zineb Bendhiba
I've implemented the {{{}{}}}[chatRequestTransformer in Apache Camel's
camel-langchain4j-agent
component|https://issues.apache.org/jira/browse/CAMEL-23642] and hit a snag.
The one-liner transformer doesn't work against DefaultAiServices:
{code:java}
// This throws: IllegalArgumentException: Cannot set both 'parameters' and
'responseFormat' on ChatRequest .chatRequestTransformer(chatRequest ->
chatRequest.toBuilder().responseFormat(responseFormat).build()) {code}
DefaultAiServices always sets a ChatRequestParameters object on the ChatRequest
(even with no tools), and ChatRequest.validate() rejects having both parameters
and a standalone responseFormat at the same time.
I had to merge responseFormat into the existing ChatRequestParameters instead:
{code:java}
{{ .chatRequestTransformer(req -> {
ChatRequestParameters existing = req.parameters();
ChatRequestParameters withFormat = existing != null
? existing.overrideWith(
DefaultChatRequestParameters.builder().responseFormat(responseFormat).build())
: DefaultChatRequestParameters.builder().responseFormat(responseFormat).build();
return
ChatRequest.builder().messages(req.messages()).parameters(withFormat).build();
})}}{code}
This was fixe in this [Langchain4j
PR|https://github.com/langchain4j/langchain4j/pull/5325], and the fix will be
available in LangChain4j 1.16.0.
--
This message was sent by Atlassian Jira
(v8.20.10#820010)