diff --git a/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/connector/ChatGPTSourceConnector.java b/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/connector/ChatGPTSourceConnector.java index 9cbeb2574a..b2427f0205 100644 --- a/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/connector/ChatGPTSourceConnector.java +++ b/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/connector/ChatGPTSourceConnector.java @@ -96,7 +96,10 @@ public void init(ConnectorContext connectorContext) { 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)); + 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(); @@ -118,7 +121,9 @@ private void doInit() { initParsePrompt(); this.openaiManager = new OpenaiManager(sourceConfig); this.chatHandler = new ChatHandler(this.openaiManager); - this.parseHandler = new ParseHandler(openaiManager, parsePromptTemplateStr); + 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); @@ -139,7 +144,7 @@ private void doInit() { private void validateRequestDTO(ChatGPTRequestDTO bodyObject) { if (bodyObject.getSubject() == null || bodyObject.getDataContentType() == null || bodyObject.getText() == null) { - throw new IllegalArgumentException("Attributes 'subject', 'datacontenttype', and 'prompt' cannot be null"); + throw new IllegalArgumentException("Attributes 'subject', 'datacontenttype', and 'text' cannot be null"); } } @@ -167,6 +172,10 @@ private CloudEvent invokeHandler(ChatGPTRequestType chatgptRequestType, ChatGPTR case CHAT: return chatHandler.invoke(bodyObject); case PARSE: + if (StringUtils.isBlank(parsePromptTemplateStr)) { + throw new IllegalStateException( + "the request type of PARSE must be configured with the correct parsePromptFileName in source-config.yml"); + } return parseHandler.invoke(bodyObject); default: throw new IllegalStateException("the request type is illegal"); diff --git a/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/dto/ChatGPTRequestDTO.java b/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/dto/ChatGPTRequestDTO.java index 07cac5ca42..dc073c07a9 100644 --- a/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/dto/ChatGPTRequestDTO.java +++ b/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/dto/ChatGPTRequestDTO.java @@ -36,14 +36,14 @@ public class ChatGPTRequestDTO { private String requestType = ChatGPTRequestType.CHAT.name(); - private String source; + private String source = "/"; - private String subject; + private String subject = "chatGPT"; @JsonProperty("datacontenttype") - private String dataContentType; + private String dataContentType = "application/json"; - private String type; + private String type = "cloudevents"; private String text; diff --git a/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/managers/OpenaiManager.java b/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/managers/OpenaiManager.java index 9e97e6cdbc..246e0b890b 100644 --- a/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/managers/OpenaiManager.java +++ b/eventmesh-connectors/eventmesh-connector-chatgpt/src/main/java/org/apache/eventmesh/connector/chatgpt/source/managers/OpenaiManager.java @@ -17,6 +17,7 @@ package org.apache.eventmesh.connector.chatgpt.source.managers; +import static com.theokanning.openai.service.OpenAiService.defaultClient; import static com.theokanning.openai.service.OpenAiService.defaultObjectMapper; import static com.theokanning.openai.service.OpenAiService.defaultRetrofit; @@ -85,8 +86,7 @@ private void initOpenAi(ChatGPTSourceConfig sourceConfig) { } ObjectMapper mapper = defaultObjectMapper(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(chatgptProxyConfig.getHost(), chatgptProxyConfig.getPort())); - OkHttpClient client = - OpenAiService.defaultClient(openaiConfig.getToken(), Duration.ofSeconds(openaiConfig.getTimeout())).newBuilder().proxy(proxy).build(); + OkHttpClient client = defaultClient(openaiConfig.getToken(), Duration.ofSeconds(openaiConfig.getTimeout())).newBuilder().proxy(proxy).build(); Retrofit retrofit = defaultRetrofit(client, mapper); OpenAiApi api = retrofit.create(OpenAiApi.class); this.openAiService = new OpenAiService(api);