You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by "ibek (via GitHub)" <gi...@apache.org> on 2025/11/05 14:50:11 UTC

[PR] CAMEL-22661: Add a native OpenAI component [camel]

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

   # Description
   
   The OpenAI component provides integration with OpenAI and OpenAI-compatible APIs for chat completion using the official openai-java SDK. The component also supports streaming, structured outputs (POJO/JSON schema), per-Exchange conversation memory, and image/text file + user prompts.
   
   Openai-java sdk https://github.com/openai/openai-java  
   
   The camel-openai component is meant for simple LLM invocations and unlike Langchain4j it doesn't require any custom java code or additional dependencies (providers, chatmodels, ..).
   
   Here are few examples how the component can be used:
   
   ```yaml
   - route:
       id: route-1145
       from:
         id: from-1972
         uri: timer
         parameters:
           repeatCount: 1
           timerName: timer
         steps:
           - setBody:
               constant: In one sentence, what is Apache Camel?
           - to: openai:chat-completion?model={{openai.model}}&baseUrl={{openai.baseUrl}}
           - log: "Response: ${body}"
   
   ```
   
   ```yaml
   - route:
       id: route-1145
       from:
         id: from-1972
         uri: timer
         parameters:
           repeatCount: 1
           timerName: timer
         steps:
           - setBody:
               constant: Generate a person profile for a software engineer
           - to: openai:chat-completion?model={{openai.model}}&baseUrl={{openai.baseUrl}}&outputClass=com.example.Person
           - log: "Structured response: ${body}"
   
   ```
   
   ```yaml
   - route:
       id: route-1145
       from:
         id: from-3120
         uri: file
         parameters:
           directoryName: assets
           fileName: animal.jpg
           noop: true
         steps:
           - setHeader:
               id: setHeader-3545
               constant:
                 expression: In one sentence, what animal is in the image?
               name: CamelOpenAIUserPrompt
           - to: openai:chat-completion?model=Qwen2_5-VL-7B-Instruct-IQ4_XS&baseUrl={{openai.baseUrl}}
           - log: "Response: ${body}"
   
   ```
   
   # Target
   
   - [x] I checked that the commit is targeting the correct branch (Camel 4 uses the `main` branch)
   
   # Tracking
   https://issues.apache.org/jira/browse/CAMEL-22661
   - [x] If this is a large change, bug fix, or code improvement, I checked there is a [JIRA issue](https://issues.apache.org/jira/browse/CAMEL) filed for the change (usually before you start working on it).
   
   <!--
   # *Note*: trivial changes like, typos, minor documentation fixes and other small items do not require a JIRA issue. In this case your pull request should address just this issue, without pulling in other changes.
   -->
   
   # Apache Camel coding standards and style
   
   - [x] I checked that each commit in the pull request has a meaningful subject line and body.
   
   <!--
   If you're unsure, you can format the pull request title like `[CAMEL-XXX] Fixes bug in camel-file component`, where you replace `CAMEL-XXX` with the appropriate JIRA issue.
   -->
   
   - [x] I have run `mvn clean install -DskipTests` locally from root folder and I have committed all auto-generated changes.
   
   <!--
   You can run the aforementioned command in your module so that the build auto-formats your code. This will also be verified as part of the checks and your PR may be rejected if if there are uncommited changes after running `mvn clean install -DskipTests`.
   
   You can learn more about the contribution guidelines at https://github.com/apache/camel/blob/main/CONTRIBUTING.md
   -->
   
   


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-22661: Add a native OpenAI component [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on code in PR #19820:
URL: https://github.com/apache/camel/pull/19820#discussion_r2497661055


##########
components/camel-ai/camel-openai/src/main/docs/openai-component.adoc:
##########
@@ -0,0 +1,343 @@
+= OpenAI Component
+:doctitle: OpenAI
+:shortname: openai
+:artifactid: camel-openai
+:description: OpenAI endpoint for chat completion.
+:since: 4.16

Review Comment:
   I have rebased this so its up to date with main branch - change the version in the pom.xml to be 4.17.0-SNAPSHOT or where else you can find. And do a full rebuild of the code from root. 
   
       mvn clean install -P fastinstall



-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-22661: Add a native OpenAI component [camel]

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #19820:
URL: https://github.com/apache/camel/pull/19820#issuecomment-3491653576

   :star2: Thank you for your contribution to the Apache Camel project! :star2: 
   
   :robot: CI automation will test this PR automatically.
   
   :camel: Apache Camel Committers, please review the following items:
   
   * First-time contributors **require MANUAL approval** for the GitHub Actions to run
   
   * You can use the command `/component-test (camel-)component-name1 (camel-)component-name2..` to request a test from the test bot.
   
   * You can label PRs using `build-all`, `build-dependents`, `skip-tests` and `test-dependents` to fine-tune the checks executed by this PR.
   
   * Build and test logs are available in the Summary page. **Only** [Apache Camel committers](https://camel.apache.org/community/team/#committers) have access to the summary. 
   
   * :warning: Be careful when sharing logs. Review their contents before sharing them publicly.


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-22661: Add a native OpenAI component [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus commented on PR #19820:
URL: https://github.com/apache/camel/pull/19820#issuecomment-3501434279

   This PR is a little bit big, so maybe tidy up last comments and get this merged. Then more work can be done in new PRs that are easier to review.
   
   Also a SB -starter is needed which should be fairly easy to do with the script file to create
   
   A quarkus extension then create a ticket in their system as this needs to be done manually


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-22661: Add a native OpenAI component [camel]

Posted by "Croway (via GitHub)" <gi...@apache.org>.
Croway commented on code in PR #19820:
URL: https://github.com/apache/camel/pull/19820#discussion_r2495463029


##########
components/camel-ai/camel-openai/src/main/docs/openai-component.adoc:
##########
@@ -0,0 +1,358 @@
+= OpenAI Component
+:doctitle: OpenAI
+:shortname: openai
+:artifactid: camel-openai
+:description: OpenAI endpoint for chat completion.
+:since: 4.17
+:supportlevel: Preview
+:tabs-sync-option:
+:component-header: Only producer is supported
+
+*Since Camel {since}*
+
+*{component-header}*
+
+The OpenAI component provides integration with OpenAI and OpenAI-compatible APIs for chat completion using the official openai-java SDK.
+
+== URI Format
+
+[source]
+----
+openai:operation[?options]
+----
+
+Currently, only the `chat-completion` operation is supported.
+
+== Component Options

Review Comment:
   These should be generated, like the partials in https://github.com/apache/camel/blob/main/components/camel-ai/camel-langchain4j-agent/src/main/docs/langchain4j-agent-component.adoc#uri-format



-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-22661: Add a native OpenAI component [camel]

Posted by "ibek (via GitHub)" <gi...@apache.org>.
ibek commented on code in PR #19820:
URL: https://github.com/apache/camel/pull/19820#discussion_r2496427992


##########
components/camel-ai/camel-openai/src/main/docs/openai-component.adoc:
##########
@@ -0,0 +1,343 @@
+= OpenAI Component
+:doctitle: OpenAI
+:shortname: openai
+:artifactid: camel-openai
+:description: OpenAI endpoint for chat completion.
+:since: 4.16

Review Comment:
   @Croway Federico, do you know where I can change this to 4.17? After a build/gen the 'since' value changes back to 4.16 automatically.



-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-22661: Add a native OpenAI component [camel]

Posted by "davsclaus (via GitHub)" <gi...@apache.org>.
davsclaus merged PR #19820:
URL: https://github.com/apache/camel/pull/19820


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-22661: Add a native OpenAI component [camel]

Posted by "orpiske (via GitHub)" <gi...@apache.org>.
orpiske commented on code in PR #19820:
URL: https://github.com/apache/camel/pull/19820#discussion_r2495180466


##########
components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIEndpoint.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.openai;
+
+import com.openai.client.OpenAIClient;
+import com.openai.client.okhttp.OpenAIOkHttpClient;
+import org.apache.camel.Category;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.spi.UriPath;
+import org.apache.camel.support.DefaultEndpoint;
+
+/**
+ * OpenAI endpoint for chat completion.
+ */
+@UriEndpoint(firstVersion = "4.16.0",
+             scheme = "openai",
+             title = "OpenAI",
+             syntax = "openai:operation",
+             category = { Category.AI },
+             producerOnly = true)
+public class OpenAIEndpoint extends DefaultEndpoint {
+
+    @UriPath
+    @Metadata(required = true, description = "The operation to perform (currently only chat-completion is supported)")
+    private String operation;
+
+    @UriParam
+    private OpenAIConfiguration configuration;
+
+    private OpenAIClient client;
+
+    public OpenAIEndpoint(String uri, OpenAIComponent component, OpenAIConfiguration configuration) {
+        super(uri, component);
+        this.configuration = configuration;
+    }
+
+    @Override
+    public Producer createProducer() throws Exception {
+        if (!"chat-completion".equals(operation)) {
+            throw new IllegalArgumentException("Only 'chat-completion' operation is supported");
+        }
+        return new OpenAIProducer(this);
+    }
+
+    @Override
+    public Consumer createConsumer(Processor processor) throws Exception {
+        throw new UnsupportedOperationException("Consumer not supported for OpenAI component");
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        client = createClient();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        if (client != null) {
+            client = null;
+        }

Review Comment:
   I'd probably invert this piece of code, so it happens before the `super.doStop()` to ensure LIFO order on the cleanup (which can help prevent obscure unwind bugs)



##########
components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIEndpoint.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.openai;
+
+import com.openai.client.OpenAIClient;
+import com.openai.client.okhttp.OpenAIOkHttpClient;
+import org.apache.camel.Category;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.spi.UriPath;
+import org.apache.camel.support.DefaultEndpoint;
+
+/**
+ * OpenAI endpoint for chat completion.
+ */
+@UriEndpoint(firstVersion = "4.16.0",
+             scheme = "openai",
+             title = "OpenAI",
+             syntax = "openai:operation",
+             category = { Category.AI },
+             producerOnly = true)
+public class OpenAIEndpoint extends DefaultEndpoint {
+
+    @UriPath
+    @Metadata(required = true, description = "The operation to perform (currently only chat-completion is supported)")
+    private String operation;
+
+    @UriParam
+    private OpenAIConfiguration configuration;
+
+    private OpenAIClient client;
+
+    public OpenAIEndpoint(String uri, OpenAIComponent component, OpenAIConfiguration configuration) {
+        super(uri, component);
+        this.configuration = configuration;
+    }
+
+    @Override
+    public Producer createProducer() throws Exception {
+        if (!"chat-completion".equals(operation)) {
+            throw new IllegalArgumentException("Only 'chat-completion' operation is supported");
+        }
+        return new OpenAIProducer(this);
+    }
+
+    @Override
+    public Consumer createConsumer(Processor processor) throws Exception {
+        throw new UnsupportedOperationException("Consumer not supported for OpenAI component");
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        client = createClient();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        if (client != null) {
+            client = null;
+        }
+    }
+
+    protected OpenAIClient createClient() {
+        String apiKey = resolveApiKey();
+
+        OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder();
+
+        if (apiKey != null && !apiKey.isEmpty()) {
+            builder.apiKey(apiKey);
+        }
+
+        if (configuration.getBaseUrl() != null && !configuration.getBaseUrl().isEmpty()) {

Review Comment:
   You can use `org.apache.camel.util.ObjectHelper.notNullOrEmpty()` here for consistency



##########
components/camel-ai/camel-openai/src/main/java/org/apache/camel/component/openai/OpenAIEndpoint.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.openai;
+
+import com.openai.client.OpenAIClient;
+import com.openai.client.okhttp.OpenAIOkHttpClient;
+import org.apache.camel.Category;
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.spi.Metadata;
+import org.apache.camel.spi.UriEndpoint;
+import org.apache.camel.spi.UriParam;
+import org.apache.camel.spi.UriPath;
+import org.apache.camel.support.DefaultEndpoint;
+
+/**
+ * OpenAI endpoint for chat completion.
+ */
+@UriEndpoint(firstVersion = "4.16.0",
+             scheme = "openai",
+             title = "OpenAI",
+             syntax = "openai:operation",
+             category = { Category.AI },
+             producerOnly = true)
+public class OpenAIEndpoint extends DefaultEndpoint {
+
+    @UriPath
+    @Metadata(required = true, description = "The operation to perform (currently only chat-completion is supported)")
+    private String operation;
+
+    @UriParam
+    private OpenAIConfiguration configuration;
+
+    private OpenAIClient client;
+
+    public OpenAIEndpoint(String uri, OpenAIComponent component, OpenAIConfiguration configuration) {
+        super(uri, component);
+        this.configuration = configuration;
+    }
+
+    @Override
+    public Producer createProducer() throws Exception {
+        if (!"chat-completion".equals(operation)) {
+            throw new IllegalArgumentException("Only 'chat-completion' operation is supported");
+        }
+        return new OpenAIProducer(this);
+    }
+
+    @Override
+    public Consumer createConsumer(Processor processor) throws Exception {
+        throw new UnsupportedOperationException("Consumer not supported for OpenAI component");
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+        client = createClient();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        if (client != null) {
+            client = null;
+        }
+    }
+
+    protected OpenAIClient createClient() {
+        String apiKey = resolveApiKey();
+
+        OpenAIOkHttpClient.Builder builder = OpenAIOkHttpClient.builder();
+
+        if (apiKey != null && !apiKey.isEmpty()) {
+            builder.apiKey(apiKey);
+        }
+
+        if (configuration.getBaseUrl() != null && !configuration.getBaseUrl().isEmpty()) {
+            builder.baseUrl(configuration.getBaseUrl());
+        }
+
+        return builder.build();
+    }
+
+    protected String resolveApiKey() {
+        // Priority: URI parameter > component config > environment variable > application.properties
+        if (configuration.getApiKey() != null && !configuration.getApiKey().isEmpty()) {

Review Comment:
   You can use org.apache.camel.util.ObjectHelper.isNotEmpty() here for consistency



-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-22661: Add a native OpenAI component [camel]

Posted by "ibek (via GitHub)" <gi...@apache.org>.
ibek commented on PR #19820:
URL: https://github.com/apache/camel/pull/19820#issuecomment-3496563419

   It's now possible to set the openai component like this:
   ```yaml
   - route:
       id: route-1145
       from:
         id: from-1972
         uri: timer
         parameters:
           repeatCount: 1
           timerName: timer
         steps:
           - to:
               id: to-1908
               uri: openai:chat-completion
               parameters:
                 userMessage: What's the weather in Brno?
                 tools: resource:classpath:assets/tools.json
           - log: "Response: ${body}"
   
   ```
   
   tools.json is standard representation of tools in openai api:
   ```
   [
     {
       "type": "function",
       "function": {
         "name": "get_weather",
         "description": "Weather by city",
         "parameters": {
           "type": "object",
           "properties": { "city": { "type": "string" } },
           "required": ["city"]
         }
       }
     }
   ]
   
   ```
   
   The response in this case (requiring a tool call) is list of ChatCompletionMessageToolCall objects from OpenAI SDK


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] CAMEL-22661: Add a native OpenAI component [camel]

Posted by "ibek (via GitHub)" <gi...@apache.org>.
ibek commented on PR #19820:
URL: https://github.com/apache/camel/pull/19820#issuecomment-3505125309

   It's ready for merge. The SB starter PR: https://github.com/apache/camel-spring-boot/pull/1563


-- 
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: commits-unsubscribe@camel.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org