davsclaus opened a new pull request, #3365:
URL: https://github.com/apache/camel-kamelets/pull/3365

   A new action Kamelet, `openai-chat-action`, on the Camel `openai` component 
(`openai:chat-completion`, Stable): it sends the body as the user message to a 
chat model of OpenAI, or of any server with the same API (Ollama, vLLM, LM 
Studio), and gives back the answer.
   
   ## Options
   - `baseUrl`: the API URL, default `https://api.openai.com/v1`, pattern 
`^https?://.+`.
   - `apiKey`: a credential (`format: password`, credentials x-descriptor). 
When it is not set, the `OPENAI_API_KEY` environment variable is used. With 
neither, the Kamelet fails to start (the OpenAI SDK requires a credential), so 
a server that checks no key still needs some text, such as `none`.
   - `model` (required), `systemMessage`, `temperature`, `maxTokens` (sent as 
`max_completion_tokens`).
   - `jsonSchema`: structured output. It is JSON text or 
`resource:file:<path>`, and it is sent as `response_format` (`json_schema`, 
name `camel_schema`).
   - `stripThinking`: removes the `<think>` block, which goes to 
`CamelOpenAIThinkingContent`.
   
   No tools, MCP, `tags`, conversation memory or other agentic options are 
exposed, so the text of a message cannot make the model act.
   
   ## Input / Output
   - **Input:** the body is the user message as text. Bytes and streams are 
read as text whatever their Content-Type. The template converts the body to a 
String, because the component would send a `byte[]`/stream body as an image, 
PDF or audio based on its Content-Type.
   - **Output:** the body is the answer as text, or the JSON text of the answer 
with `jsonSchema`. These headers are set: `CamelOpenAIFinishReason`, 
`CamelOpenAIResponseModel`, `CamelOpenAIResponseId`, and 
`CamelOpenAIPromptTokens`/`CompletionTokens`/`TotalTokens`. They are declared 
under `spec.dataTypes`, with no `default` data type.
   - **Security:** `CamelOpenAI*` headers are removed before the call. The 
producer reads the model, the system/user/developer messages, the sampling, 
`maxTokens`, the JSON schema, the output class and streaming from them, and 
they take precedence over the options.
   - **Errors:** an error answer fails the message with a `com.openai.errors` 
exception: `UnauthorizedException` for 401, `InternalServerException` for 5xx. 
Requests that get 408/409/429/5xx are sent 2 more times first (the SDK default 
`maxRetries`).
   
   ## Tests (new `OpenAiIT`, Citrus HTTP stub of `POST /v1/chat/completions`, 
no containers, no real model or key)
   - `openai-chat-action-test` sends a `byte[]` body with `Content-Type: 
image/png`, plus `CamelOpenAIModel`/`SystemMessage`/`UserMessage`/`MaxTokens` 
override headers. It checks that the request has the model, a system message 
with `& % +` and parentheses, the body as the user message, `temperature`, 
`max_completion_tokens` and the Bearer key, and that the override headers are 
ignored. It also checks that the body is the answer with the think block 
stripped, and the finish reason, model, id, token and thinking headers.
   - `openai-chat-action-json-test` checks that the request has 
`response_format` with the schema and that the body is the JSON text of the 
answer. It runs twice: once with the schema inline, once from `resource:file:`.
   - `openai-chat-action-error-test`: 401 fails at once with 
`UnauthorizedException`, and 503 is sent 3 times, then fails with 
`InternalServerException`.
   - Local: `./mvnw -o clean install -DskipITs` passes. `OpenAiIT` gives Tests 
run: 3, Failures: 0, Errors: 0 (about 25 s). As a negative check I removed the 
`removeHeaders`/`convertBodyTo` steps, and the first test then fails 
(`max_completion_tokens` was 5).
   
   ## Good to know for review
   - If a route puts the JSON schema inline, two closing braces next to each 
other (`}}`) are read as the end of a property placeholder. The option 
description says to write `} }` or to use a file. Both forms are tested.
   - The support level is Stable, the same as the component.
   
   Fixes #3361.
   
   ## Upgrade note
   - New `openai-chat-action` Kamelet: sends the body to a chat model of OpenAI 
or of any OpenAI-compatible server (`baseUrl`) and gives back the answer as 
text, or as JSON text with `jsonSchema`; the model cannot call tools, and 
`CamelOpenAI*` headers are removed from the message first (#3361).
   
   _Claude Code on behalf of Claus Ibsen_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)
   
   https://claude.ai/code/session_016Y4dDmBDsSWV117kccuNf9
   


-- 
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