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


##########
eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/connector/ChatGPTSourceConnector.java:
##########
@@ -0,0 +1,224 @@
+/*
+ * 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.common.utils.AssertUtils;
+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);
+        AssertUtils.notNull(resource, String.format("cannot find file %s", 
parsePromptFileName));

Review Comment:
   You didn't adopt this latter point "_then subsequent processes are executed 
in normal mode and log to warn the user for the reason when the user request is 
of type PARSE_", in which case you handle it by reporting an error. I think 
that's fine.
   
   您并没有采纳后面这一点“当用户的请求是PARSE类型时,后续流程以普通模式执行,并用日志警告用户原因”,这种情况下,您的处理方式是报错。我觉得也可以。



##########
eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/managers/OpenaiManager.java:
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.managers;
+
+import static com.theokanning.openai.service.OpenAiService.defaultObjectMapper;
+import static com.theokanning.openai.service.OpenAiService.defaultRetrofit;
+
+import org.apache.eventmesh.common.utils.AssertUtils;
+import org.apache.eventmesh.common.utils.JsonUtils;
+import 
org.apache.eventmesh.connector.chatgpt.source.config.ChatGPTSourceConfig;
+import org.apache.eventmesh.connector.chatgpt.source.config.OpenaiConfig;
+import org.apache.eventmesh.connector.chatgpt.source.config.OpenaiProxyConfig;
+
+import java.net.InetSocketAddress;
+import java.net.Proxy;
+import java.time.Duration;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.theokanning.openai.client.OpenAiApi;
+import com.theokanning.openai.completion.chat.ChatCompletionRequest;
+import 
com.theokanning.openai.completion.chat.ChatCompletionRequest.ChatCompletionRequestBuilder;
+import com.theokanning.openai.completion.chat.ChatMessage;
+import com.theokanning.openai.service.OpenAiService;
+
+import lombok.Getter;
+import lombok.extern.slf4j.Slf4j;
+
+import okhttp3.OkHttpClient;
+import retrofit2.Retrofit;
+
+
+@Slf4j
+public class OpenaiManager {
+
+    @Getter
+    private OpenAiService openAiService;
+
+    private String chatCompletionRequestTemplateStr;
+
+    public OpenaiManager(ChatGPTSourceConfig sourceConfig) {
+        initOpenAi(sourceConfig);
+    }
+
+    public String getResult(ChatCompletionRequest req) {
+        StringBuilder gptData = new StringBuilder();
+        try {
+            openAiService.createChatCompletion(req).getChoices()
+                .forEach(chatCompletionChoice -> 
gptData.append(chatCompletionChoice.getMessage().getContent()));
+        } catch (Exception e) {
+            log.error("Failed to generate GPT connection record: {}", 
e.getMessage());
+        }
+        return gptData.toString();
+    }
+
+    public ChatCompletionRequest newChatCompletionRequest(List<ChatMessage> 
chatMessages) {
+        ChatCompletionRequest request = 
JsonUtils.parseObject(chatCompletionRequestTemplateStr, 
ChatCompletionRequest.class);
+        request.setMessages(chatMessages);
+        return request;
+    }
+
+    private void initOpenAi(ChatGPTSourceConfig sourceConfig) {
+        OpenaiConfig openaiConfig = sourceConfig.getOpenaiConfig();
+        AssertUtils.isTrue(openaiConfig.getTimeout() > 0, "openaiTimeout must 
be >= 0");

Review Comment:
   I'm a bit uncertain. If the configuration file does not have 'timeout' or it 
is present but the user hasn't configured a value for it, would having a 
default value of >0 for 'timeout' prevent reporting this error to the user?
   
   我有些不确定。当配置文件中没有timeout或者有但是用户没有配置它的值的时候,如果timeout有>0的默认值是不是就能避免将该错误报告给用户?



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