Skip to content

Commit

Permalink
[ISSUE apache#4047] fix code style
Browse files Browse the repository at this point in the history
  • Loading branch information
jevinjiang committed Apr 10, 2024
1 parent ca42ded commit 3331997
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
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.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
Expand Down Expand Up @@ -67,11 +68,8 @@ public class ChatGPTSourceConnector implements Source {
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 final ExecutorService chatgptSourceExecutorService = ThreadPoolFactory.createThreadPoolExecutor(
Runtime.getRuntime().availableProcessors() * 2, Runtime.getRuntime().availableProcessors() * 2, "ChatGPTSourceThread");

private OpenaiManager openaiManager;
private String parsePromptTemplateStr;
Expand All @@ -98,12 +96,18 @@ public void init(ConnectorContext connectorContext) {

public void initParsePrompt() {
String parsePromptFileName = sourceConfig.getConnectorConfig().getParsePromptFileName();
URL resource = this.getClass().getClassLoader().getResource(parsePromptFileName);
URL resource = Thread.currentThread().getContextClassLoader().getResource(parsePromptFileName);
AssertUtils.notNull(resource, String.format("cannot find file %s", parsePromptFileName));
try {
this.parsePromptTemplateStr = new String(Files.readAllBytes(Paths.get(resource.toURI())));
} catch (URISyntaxException e) {
throw new IllegalStateException("The file path is invalid", e);
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);
}
Expand Down Expand Up @@ -150,10 +154,8 @@ private void handleRequest(ChatGPTRequestDTO bodyObject, RoutingContext ctx) {
ctx.response().setStatusCode(HttpResponseStatus.OK.code()).end();
} catch (IllegalArgumentException e) {
log.error("[ChatGPTSourceConnector] the request type is illegal: {}", e.getMessage(), e);
ctx.response()
.setStatusCode(HttpResponseStatus.BAD_REQUEST.code())
.setStatusMessage(String.format("request type '%s' is not supported", bodyObject.getRequestType()))
.end();
ctx.response().setStatusCode(HttpResponseStatus.BAD_REQUEST.code())
.setStatusMessage(String.format("request type '%s' is not supported", bodyObject.getRequestType())).end();
} catch (Exception e) {
log.error("[ChatGPTSourceConnector] Error processing request: {}", e.getMessage(), e);
ctx.response().setStatusCode(HttpResponseStatus.INTERNAL_SERVER_ERROR.code()).end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ public Map<String, String> convertToMap(Object obj) {
}

public Method getGetter(Field field, Class<?> clazz) throws NoSuchMethodException {
boolean isBooleanField = false;
if (boolean.class.isAssignableFrom(field.getType()) || Boolean.class.isAssignableFrom(field.getType())) {
isBooleanField = true;
}
boolean isBooleanField = boolean.class.isAssignableFrom(field.getType()) || Boolean.class.isAssignableFrom(field.getType());
String handledFieldName = upperFirst(field.getName());
String methodName;
if (isBooleanField) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
#
# 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.
#

You are an AI assistant named CloudEventsConverter. avoid escape characters .
Your task is to construct a JSON object in CloudEvents format. Based on the field name and field description in the 'data' field of the CloudEvents formatted JSON object, convert the input text provided by the user into the content of the 'data' field, which must comply with the specifications of the content of the 'datacontenttype' field.
The role is :
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,45 +82,44 @@ void testPoll() throws Exception {
Assertions.assertEquals(batchSize, res1.size());

// test invalid requests
HttpPost invalidPost = new HttpPost(uri);
TestEvent event = new TestEvent();
event.type = "com.example.someevent";
event.source = "/mycontext";
event.datacontenttype = "text/plain";
event.text = expectedMessage;
HttpPost invalidPost = new HttpPost(uri);
invalidPost.setEntity(new StringEntity(JsonUtils.toJSONString(event)));
HttpResponse resp = httpClient.execute(invalidPost);
Assertions.assertEquals(HttpStatus.SC_BAD_REQUEST, resp.getStatusLine().getStatusCode());
}


HttpResponse mockStructuredChatRequest() throws Exception {
HttpPost httpPost = new HttpPost(uri);
TestEvent event = new TestEvent();
event.type = "com.example.someevent";
event.source = "/mycontext";
event.subject = "test";
event.datacontenttype = "text/plain";
event.text = expectedMessage;
event.requestType = "CHAT";
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(JsonUtils.toJSONString(event)));

return httpClient.execute(httpPost);
}


HttpResponse mockStructuredParseRequest() throws Exception {
HttpPost httpPost = new HttpPost(uri);
TestEvent event = new TestEvent();
event.type = "com.example.someevent";
event.source = "/mycontext";
event.subject = "test";
event.datacontenttype = "application/json";
event.text = expectedParseMessage;
event.requestType = "PARSE1";
event.requestType = "PARSE";
event.fields = "orderNo:this is order number;address:this is a address;phone:this is phone number";
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(JsonUtils.toJSONString(event)));

return httpClient.execute(httpPost);
}

Expand Down

0 comments on commit 3331997

Please sign in to comment.