1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package com.owlmaddie.chat;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.owlmaddie.commands.ConfigurationHandler;
import com.owlmaddie.json.ChatGPTResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
/**
* The {@code ChatGPTRequest} class is used to send HTTP requests to our LLM to generate
* messages.
*/
public class ChatGPTRequest {
public static final Logger LOGGER = LoggerFactory.getLogger("creaturechat");
public static String lastErrorMessage;
static class ChatGPTRequestMessage {
String role;
String content;
public ChatGPTRequestMessage(String role, String content) {
this.role = role;
this.content = content;
}
}
static class ChatGPTRequestPayload {
String model;
List<ChatGPTRequestMessage> messages;
ResponseFormat response_format;
float temperature;
int max_tokens;
boolean stream;
public ChatGPTRequestPayload(String model, List<ChatGPTRequestMessage> messages, Boolean jsonMode, float temperature, int maxTokens) {
this.model = model;
this.messages = messages;
this.temperature = temperature;
this.max_tokens = maxTokens;
this.stream = false;
if (jsonMode) {
this.response_format = new ResponseFormat("json_object");
} else {
this.response_format = new ResponseFormat("text");
}
}
}
static class ResponseFormat {
String type;
public ResponseFormat(String type) {
this.type = type;
}
}
public static String removeQuotes(String str) {
if (str != null && str.length() > 1 && str.startsWith("\"") && str.endsWith("\"")) {
return str.substring(1, str.length() - 1);
}
return str;
}
// Class to represent the error response structure
public static class ErrorResponse {
Error error;
static class Error {
String message;
String type;
String code;
}
}
public static String parseAndLogErrorResponse(String errorResponse) {
try {
Gson gson = new Gson();
ErrorResponse response = gson.fromJson(errorResponse, ErrorResponse.class);
if (response.error != null) {
LOGGER.error("Error Message: " + response.error.message);
LOGGER.error("Error Type: " + response.error.type);
LOGGER.error("Error Code: " + response.error.code);
return response.error.message;
} else {
LOGGER.error("Unknown error response: " + errorResponse);
return "Unknown: " + errorResponse;
}
} catch (JsonSyntaxException e) {
LOGGER.warn("Failed to parse error response as JSON, falling back to plain text");
LOGGER.error("Error response: " + errorResponse);
} catch (Exception e) {
LOGGER.error("Failed to parse error response", e);
}
return removeQuotes(errorResponse);
}
// Function to replace placeholders in the template
public static String replacePlaceholders(String template, Map<String, String> replacements) {
String result = template;
for (Map.Entry<String, String> entry : replacements.entrySet()) {
result = result.replaceAll(Pattern.quote("{{" + entry.getKey() + "}}"), entry.getValue());
}
return result.replace("\"", "") ;
}
// Function to roughly estimate # of OpenAI tokens in String
private static int estimateTokenSize(String text) {
return (int) Math.round(text.length() / 3.5);
}
public static CompletableFuture<String> fetchMessageFromChatGPT(ConfigurationHandler.Config config, String systemPrompt, Map<String, String> contextData, List<ChatMessage> messageHistory, Boolean jsonMode) {
// Init API & LLM details
String apiUrl = config.getUrl();
String apiKey = config.getApiKey();
String modelName = config.getModel();
Integer timeout = config.getTimeout() * 1000;
int maxContextTokens = config.getMaxContextTokens();
int maxOutputTokens = config.getMaxOutputTokens();
double percentOfContext = config.getPercentOfContext();
return CompletableFuture.supplyAsync(() -> {
try {
// Replace placeholders
String systemMessage = replacePlaceholders(systemPrompt, contextData);
URL url = new URL(apiUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("Authorization", "Bearer " + apiKey);
connection.setDoOutput(true);
connection.setConnectTimeout(timeout); // 10 seconds connection timeout
connection.setReadTimeout(timeout); // 10 seconds read timeout
// Create messages list (for chat history)
List<ChatGPTRequestMessage> messages = new ArrayList<>();
// Don't exceed a specific % of total context window (to limit message history in request)
int remainingContextTokens = (int) ((maxContextTokens - maxOutputTokens) * percentOfContext);
int usedTokens = estimateTokenSize("system: " + systemMessage);
// Iterate backwards through the message history
for (int i = messageHistory.size() - 1; i >= 0; i--) {
ChatMessage chatMessage = messageHistory.get(i);
String senderName = chatMessage.sender.toString().toLowerCase(Locale.ENGLISH);
String messageText = replacePlaceholders(chatMessage.message, contextData);
int messageTokens = estimateTokenSize(senderName + ": " + messageText);
if (usedTokens + messageTokens > remainingContextTokens) {
break; // If adding this message would exceed the token limit, stop adding more messages
}
// Add the message to the temporary list
messages.add(new ChatGPTRequestMessage(senderName, messageText));
usedTokens += messageTokens;
}
// Add system message
messages.add(new ChatGPTRequestMessage("system", systemMessage));
// Reverse the list to restore chronological order
// This is needed since we build the list in reverse order for token restricting above
Collections.reverse(messages);
// Convert JSON to String
ChatGPTRequestPayload payload = new ChatGPTRequestPayload(modelName, messages, jsonMode, 1.0f, maxOutputTokens);
Gson gsonInput = new Gson();
String jsonInputString = gsonInput.toJson(payload);
try (OutputStream os = connection.getOutputStream()) {
byte[] input = jsonInputString.getBytes(StandardCharsets.UTF_8);
os.write(input, 0, input.length);
}
// Check for error message in response
if (connection.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
try (BufferedReader errorReader = new BufferedReader(new InputStreamReader(connection.getErrorStream(), StandardCharsets.UTF_8))) {
String errorLine;
StringBuilder errorResponse = new StringBuilder();
while ((errorLine = errorReader.readLine()) != null) {
errorResponse.append(errorLine.trim());
}
// Parse and log the error response using Gson
String cleanError = parseAndLogErrorResponse(errorResponse.toString());
lastErrorMessage = cleanError;
} catch (Exception e) {
LOGGER.error("Failed to read error response", e);
lastErrorMessage = "Failed to read error response: " + e.getMessage();
}
return null;
} else {
lastErrorMessage = null;
}
try (BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), StandardCharsets.UTF_8))) {
StringBuilder response = new StringBuilder();
String responseLine;
while ((responseLine = br.readLine()) != null) {
response.append(responseLine.trim());
}
Gson gsonOutput = new Gson();
ChatGPTResponse chatGPTResponse = gsonOutput.fromJson(response.toString(), ChatGPTResponse.class);
if (chatGPTResponse != null && chatGPTResponse.choices != null && !chatGPTResponse.choices.isEmpty()) {
String content = chatGPTResponse.choices.get(0).message.content;
return content;
} else {
lastErrorMessage = "Failed to parse response from LLM";
return null;
}
}
} catch (Exception e) {
LOGGER.error("Failed to request message from LLM", e);
lastErrorMessage = "Failed to request message from LLM: " + e.getMessage();
return null;
}
});
}
}