Commit e996b966 by Jonathan Thomas

Add new config option for saving debug prompts (needed for fine-tuning data generation)

parent 1467142d
Pipeline #12704 passed with stages
in 2 minutes 3 seconds
package com.owlmaddie.chat;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.owlmaddie.commands.ConfigurationHandler;
import com.owlmaddie.json.ChatGPTResponse;
......@@ -11,6 +12,7 @@ import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
......@@ -202,20 +204,26 @@ public class ChatGPTRequest {
lastErrorMessage = null;
}
StringBuilder response = new StringBuilder();
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;
LOGGER.info("Generated message: " + content);
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;
LOGGER.info("Generated message: " + content);
return content;
// Save debug prompts if required
if (config.saveDebugPrompts) {
saveDebugPrompts(systemMessage, messages, content);
}
return content;
}
} catch (IOException e) {
LOGGER.error("Failed to fetch message from ChatGPT", e);
......@@ -223,5 +231,28 @@ public class ChatGPTRequest {
return null; // If there was an error or no response, return null
});
}
private static void saveDebugPrompts(String systemMessage, List<ChatGPTRequestMessage> messages, String responseContent) {
try {
// Create the JSON structure
Map<String, Object> jsonStructure = new HashMap<>();
jsonStructure.put("messages", messages);
messages.add(new ChatGPTRequestMessage("assistant", responseContent)); // Add the assistant's response to the messages
// Determine the filename
String timestamp = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date());
String filename = systemMessage.contains("Short Greeting")
? "system-character-" + timestamp + ".json"
: "system-chat-" + timestamp + ".json";
// Save to file
Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
try (Writer writer = new FileWriter(filename)) {
gson.toJson(jsonStructure, writer);
}
} catch (IOException e) {
LOGGER.error("Failed to save debug prompts", e);
}
}
}
......@@ -73,6 +73,7 @@ public class ConfigurationHandler {
private int timeout = 10;
private List<String> whitelist = new ArrayList<>();
private List<String> blacklist = new ArrayList<>();
public boolean saveDebugPrompts = true;
// Getters and setters for existing fields
public String getApiKey() { return apiKey; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment