jevinjiang commented on code in PR #4817:
URL: https://github.com/apache/eventmesh/pull/4817#discussion_r1568852210


##########
eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/connector/ChatGPTSourceConnector.java:
##########
@@ -0,0 +1,232 @@
+/*
+ * 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.eventmesh.connector.chatgpt.source.connector;
+
+import org.apache.eventmesh.common.ThreadPoolFactory;
+import org.apache.eventmesh.common.exception.EventMeshException;
+import 
org.apache.eventmesh.connector.chatgpt.source.config.ChatGPTSourceConfig;
+import org.apache.eventmesh.connector.chatgpt.source.dto.ChatGPTRequestDTO;
+import org.apache.eventmesh.connector.chatgpt.source.enums.ChatGPTRequestType;
+import org.apache.eventmesh.connector.chatgpt.source.handlers.ChatHandler;
+import org.apache.eventmesh.connector.chatgpt.source.handlers.ParseHandler;
+import org.apache.eventmesh.connector.chatgpt.source.managers.OpenaiManager;
+import org.apache.eventmesh.openconnect.api.config.Config;
+import org.apache.eventmesh.openconnect.api.connector.ConnectorContext;
+import org.apache.eventmesh.openconnect.api.connector.SourceConnectorContext;
+import org.apache.eventmesh.openconnect.api.source.Source;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;
+import org.apache.eventmesh.openconnect.util.CloudEventUtil;
+
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.BufferedReader;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+import io.cloudevents.CloudEvent;
+import io.netty.handler.codec.http.HttpResponseStatus;
+import io.vertx.core.Vertx;
+import io.vertx.core.http.HttpMethod;
+import io.vertx.core.http.HttpServer;
+import io.vertx.core.http.HttpServerOptions;
+import io.vertx.ext.web.RequestBody;
+import io.vertx.ext.web.Router;
+import io.vertx.ext.web.RoutingContext;
+import io.vertx.ext.web.handler.BodyHandler;
+
+import lombok.extern.slf4j.Slf4j;
+
+@Slf4j
+public class ChatGPTSourceConnector implements Source {
+
+    private static final int DEFAULT_BATCH_SIZE = 10;
+
+    private ChatGPTSourceConfig sourceConfig;
+    private BlockingQueue<CloudEvent> queue;
+    private HttpServer server;
+    private final ExecutorService chatgptSourceExecutorService = 
ThreadPoolFactory.createThreadPoolExecutor(
+        Runtime.getRuntime().availableProcessors() * 2, 
Runtime.getRuntime().availableProcessors() * 2, "ChatGPTSourceThread");
+
+    private OpenaiManager openaiManager;
+    private String parsePromptTemplateStr;
+    private ChatHandler chatHandler;
+    private ParseHandler parseHandler;
+
+    @Override
+    public Class<? extends Config> configClass() {
+        return ChatGPTSourceConfig.class;
+    }
+
+    @Override
+    public void init(Config config) {
+        this.sourceConfig = (ChatGPTSourceConfig) config;
+        doInit();
+    }
+
+    @Override
+    public void init(ConnectorContext connectorContext) {
+        SourceConnectorContext sourceConnectorContext = 
(SourceConnectorContext) connectorContext;
+        this.sourceConfig = (ChatGPTSourceConfig) 
sourceConnectorContext.getSourceConfig();
+        doInit();
+    }
+
+    public void initParsePrompt() {
+        String parsePromptFileName = 
sourceConfig.getConnectorConfig().getParsePromptFileName();
+        URL resource = 
Thread.currentThread().getContextClassLoader().getResource(parsePromptFileName);
+        if (resource == null) {
+            log.warn("cannot find prompt file {} in resources", 
parsePromptFileName);
+            return;
+        }
+        String filePath = resource.getPath();
+        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) 
{
+            StringBuilder builder = new StringBuilder();
+            String line;
+            while ((line = br.readLine()) != null) {
+                if (!line.startsWith("#") && StringUtils.isNotBlank(line)) {
+                    builder.append(line).append("\n");
+                }
+            }
+            this.parsePromptTemplateStr = builder.toString();
+        } catch (IOException e) {
+            throw new IllegalStateException("Unable to read file", e);
+        }
+    }
+
+
+    @SuppressWarnings("checkstyle:WhitespaceAround")
+    private void doInit() {
+        initParsePrompt();
+        this.openaiManager = new OpenaiManager(sourceConfig);
+        this.chatHandler = new ChatHandler(this.openaiManager);
+        if (StringUtils.isNotEmpty(parsePromptTemplateStr)) {
+            this.parseHandler = new ParseHandler(openaiManager, 
parsePromptTemplateStr);
+        }
+        this.queue = new LinkedBlockingQueue<>(1024);
+        final Vertx vertx = Vertx.vertx();
+        final Router router = Router.router(vertx);
+        
router.route().path(this.sourceConfig.connectorConfig.getPath()).method(HttpMethod.POST).handler(BodyHandler.create()).handler(ctx
 -> {
+            try {
+                RequestBody body = ctx.body();
+                ChatGPTRequestDTO bodyObject = 
body.asPojo(ChatGPTRequestDTO.class);
+                validateRequestDTO(bodyObject);
+                handleRequest(bodyObject, ctx);
+            } catch (Exception e) {
+                handleError(e, ctx);
+            }
+        });
+        this.server = vertx.createHttpServer(new 
HttpServerOptions().setPort(this.sourceConfig.connectorConfig.getPort())
+            
.setIdleTimeout(this.sourceConfig.connectorConfig.getIdleTimeout())).requestHandler(router);

Review Comment:
   The same problem as openaiTimeout will also occur. I added the processing 
logic and rechecked the places where default values ​​need to be added.



-- 
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: issues-unsubscr...@eventmesh.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@eventmesh.apache.org
For additional commands, e-mail: issues-h...@eventmesh.apache.org

Reply via email to