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: [email protected]

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

Reply via email to