Centralize ChatGPT stateful requests
All stateful requests for ChatGPT are now handled by the
ChatGptHttpClient class, which consistently applies the OpenAI
assistant version HTTP header to every ChatGPT request.
Change-Id: I43549936ffd85da238b021007e161b53f33f9289
Signed-off-by: Patrizio <patrizio.gelosi@amarulasolutions.com>
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/http/HttpClient.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/http/HttpClient.java
index cf7b88b..85abe94 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/http/HttpClient.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/common/client/http/HttpClient.java
@@ -49,14 +49,10 @@
public Request createRequestFromJson(String uri, String bearer, Object requestObject,
Map<String, String> additionalHeaders) {
String bodyJson = getGson().toJson(requestObject);
- log.info("ChatGPT request body: {}", bodyJson);
+ log.debug("Request body: {}", bodyJson);
RequestBody body = RequestBody.create(bodyJson, MediaType.get("application/json"));
return createRequest(uri, bearer, body, additionalHeaders);
}
- public Request createRequestFromJson(String uri, String bearer, Object requestObject) {
- return createRequestFromJson(uri, bearer, requestObject, null);
- }
-
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptAssistant.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptAssistant.java
index 7a8071a..d1c14c3 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptAssistant.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptAssistant.java
@@ -5,7 +5,6 @@
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.ClientBase;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.chatgpt.ChatGptTools;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.api.gerrit.GerritChange;
-import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.http.HttpClient;
import com.googlesource.gerrit.plugins.chatgpt.mode.common.model.api.chatgpt.ChatGptTool;
import com.googlesource.gerrit.plugins.chatgpt.mode.stateful.client.api.UriResourceLocatorStateful;
import com.googlesource.gerrit.plugins.chatgpt.mode.stateful.client.api.git.GitRepoFiles;
@@ -19,7 +18,6 @@
import java.net.URI;
import java.nio.file.Path;
-import java.util.Map;
import static com.googlesource.gerrit.plugins.chatgpt.utils.FileUtils.createTempFileWithContent;
import static com.googlesource.gerrit.plugins.chatgpt.utils.GsonUtils.getGson;
@@ -30,7 +28,7 @@
public static final String KEY_ASSISTANT_ID = "assistantId";
public static final String CODE_INTERPRETER_TOOL_TYPE = "code_interpreter";
- private final HttpClient httpClient = new HttpClient();
+ private final ChatGptHttpClient httpClient = new ChatGptHttpClient();
private final GerritChange change;
private final GitRepoFiles gitRepoFiles;
private final PluginDataHandler pluginDataHandler;
@@ -80,7 +78,6 @@
private Request createRequest(String fileId) {
URI uri = URI.create(config.getGptDomain() + UriResourceLocatorStateful.chatCreateAssistantsUri());
log.debug("ChatGPT Create Assistant request URI: {}", uri);
- Map<String, String> additionalHeaders = Map.of("OpenAI-Beta", "assistants=v1");
ChatGptPromptStateful chatGptPromptStateful = new ChatGptPromptStateful(config, change);
ChatGptParameters chatGptParameters = new ChatGptParameters(config, change.getIsCommentEvent());
ChatGptTool[] tools = new ChatGptTool[] {
@@ -97,7 +94,7 @@
.tools(tools)
.build();
- return httpClient.createRequestFromJson(uri.toString(), config.getGptToken(), requestBody, additionalHeaders);
+ return httpClient.createRequestFromJson(uri.toString(), config.getGptToken(), requestBody);
}
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptHttpClient.java b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptHttpClient.java
new file mode 100644
index 0000000..077daa9
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/chatgpt/mode/stateful/client/api/chatgpt/ChatGptHttpClient.java
@@ -0,0 +1,15 @@
+package com.googlesource.gerrit.plugins.chatgpt.mode.stateful.client.api.chatgpt;
+
+import com.googlesource.gerrit.plugins.chatgpt.mode.common.client.http.HttpClient;
+import okhttp3.Request;
+
+import java.util.Map;
+
+public class ChatGptHttpClient extends HttpClient {
+ private static final Map<String, String> BETA_VERSION_HEADER = Map.of("OpenAI-Beta", "assistants=v1");
+
+ public Request createRequestFromJson(String uri, String bearer, Object requestObject) {
+ return createRequestFromJson(uri, bearer, requestObject, BETA_VERSION_HEADER);
+ }
+
+}