Skip to content

Conversation

@ibek
Copy link

@ibek ibek commented Nov 5, 2025

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:

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

  • 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

  • If this is a large change, bug fix, or code improvement, I checked there is a JIRA issue filed for the change (usually before you start working on it).

Apache Camel coding standards and style

  • I checked that each commit in the pull request has a meaningful subject line and body.
  • I have run mvn clean install -DskipTests locally from root folder and I have committed all auto-generated changes.

@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2025

🌟 Thank you for your contribution to the Apache Camel project! 🌟

🤖 CI automation will test this PR automatically.

🐫 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 have access to the summary.

  • ⚠️ Be careful when sharing logs. Review their contents before sharing them publicly.

Copy link
Contributor

@orpiske orpiske left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks pretty nice!!!


protected String resolveApiKey() {
// Priority: URI parameter > component config > environment variable > application.properties
if (configuration.getApiKey() != null && !configuration.getApiKey().isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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


Currently, only the `chat-completion` operation is supported.

== Component Options
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ibek ibek force-pushed the openai-component branch from a7d1403 to ddee3aa Compare November 5, 2025 22:40
Copy link
Contributor

@oscerd oscerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks really promising. The model uriParam cannot be an enum, because we're going to have it exploded soon, but I think we should have some mechanism determing what models is better to use for the context.

@ibek ibek force-pushed the openai-component branch 2 times, most recently from 16b9bb5 to caf2a74 Compare November 6, 2025 10:50
@ibek
Copy link
Author

ibek commented Nov 6, 2025

It's now possible to set the openai component like this:

- 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

@davsclaus
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants