Skip to content

Commit

Permalink
[ISSUE #4047] fix review question
Browse files Browse the repository at this point in the history
  • Loading branch information
JiangShuJu committed Apr 15, 2024
1 parent 5f600c6 commit 6ccc530
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand All @@ -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");
}
}

Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 6ccc530

Please sign in to comment.