oscerd opened a new pull request, #28971:
URL: https://github.com/apache/camel/pull/28971

   Fixes [CAMEL-26222](https://issues.apache.org/jira/browse/CAMEL-26222), 
found while adding the catalog body types (CAMEL-24961).
   
   Fixes [CAMEL-26222](https://issues.apache.org/jira/browse/CAMEL-26222).
   
   ## 1. chat, chatStreaming and deploymentChat used a List body as 
List<ChatMessage> unchecked
   
   **Was:** `AbstractWatsonxAiHandler.getMessages` returned any `List` body, 
and any `CamelIBMWatsonxAiMessages` header, as a `List<ChatMessage>`. A 
`List<String>` failed later in the watsonx.ai SDK with a raw 
`ClassCastException` (`String cannot be cast to ChatMessage`).
   
   **Now:** the elements are checked, for the body and for the header. A 
`ChatMessage` is used as it is, a `String` becomes a user message 
(`UserMessage.text`), and any other element fails with an 
`IllegalArgumentException` that names its class, before a request is sent. A 
single `ChatMessage` body is a conversation of one message; before, it failed 
with "Chat messages must be provided", and with the change of item 2 it would 
otherwise have been sent as its `toString()`.
   
   ## 2. chat, embedding and rerank accepted only a String body
   
   **Was:** `getMessages` and `getInputs` checked `instanceof String`, so a 
`byte[]` or `InputStream` body (for example from platform-http) was not used: 
chat, chatStreaming and deploymentChat failed with "Chat messages must be 
provided", or sent only the system message of the 
`CamelIBMWatsonxAiSystemMessage` header, and embedding and rerank failed with 
"Input texts must be provided".
   
   **Now:** a body that is not a `List` is read with `getBody(String.class)`, 
as textGeneration (`getInput`) does, so bytes and streams work for chat, 
chatStreaming, deploymentChat, embedding and rerank. A String body behaves as 
before. As for textGeneration, a Map or a POJO is read as its `toString()`.
   
   ## Tests
   
   - New `WatsonxAiMessageBodyTest` (21 tests): the real watsonx.ai SDK against 
a JDK `HttpServer` that plays watsonx.ai and records the requests. A test 
subclass of the endpoint builds the SDK services with a fixed token, so no IBM 
Cloud IAM call is made. It covers a List of Strings, a List of ChatMessage, a 
mixed List, a List with another element, Strings in the messages header, a 
single ChatMessage, bytes, a stream, the system message header with bytes, no 
body, and a String, for chat, chatStreaming (with an SSE response) and 
deploymentChat, and bytes, a stream, a String, a List and no body for embedding 
and rerank.
   - With the `src/main` changes reverted, 16 of the 21 new tests fail: a 
`ClassCastException` from the SDK for the String elements, "Chat messages must 
be provided" or "Input texts must be provided" for bytes, streams and the 
ChatMessage body, a `ClassCastException` instead of the 
`IllegalArgumentException` for the List with an Integer, and one message 
instead of two for the system message header with bytes. The 5 that pass are 
the List of ChatMessage, the String body, a String or a List of Strings for 
embedding, and no body for chat and embedding.
   - Module: 29 unit tests, 0 failures (the ITs need watsonx.ai credentials and 
are skipped).
   
   ## Docs and generated files
   
   - The component doc says where chat, embedding and rerank take their input 
from: the header, a List, a ChatMessage, or text read from the body.
   - `@BodyTypes`: chat, chatStreaming, deploymentChat, embedding and rerank 
take text, bytes, a stream or a List; the description says how a List is read. 
The regenerated `ibm-watsonx-ai.json` and the component doc are copied into the 
catalog.
   - No upgrade guide entry: the bodies that change behaviour failed before.
   
   ## Not changed
   
   - The elements of a List body for embedding and rerank are not checked: the 
SDK sends them in the JSON as they are, and watsonx.ai rejects a List that is 
not a List of Strings.
   - With a List or a ChatMessage body, the `CamelIBMWatsonxAiSystemMessage` 
header is not added, as before for a List body: the body is the whole 
conversation.
   - processToolCalls still reads the `CamelIBMWatsonxAiMessages` header 
unchecked; it only adds the assistant and tool messages to it, and the next 
chat checks it.
   
   Main only, as the other fixes of the CAMEL-24961 follow-ups.
   
   _Claude Code on behalf of Andrea Cosentino (oscerd)_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to