Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
CreatureChat
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Jobs
Commits
Open sidebar
Public
CreatureChat
Commits
e996b966
Commit
e996b966
authored
Aug 03, 2024
by
Jonathan Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
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
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
7 deletions
+39
-7
ChatGPTRequest.java
src/main/java/com/owlmaddie/chat/ChatGPTRequest.java
+38
-7
ConfigurationHandler.java
...ain/java/com/owlmaddie/commands/ConfigurationHandler.java
+1
-0
No files found.
src/main/java/com/owlmaddie/chat/ChatGPTRequest.java
View file @
e996b966
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
);
}
}
}
src/main/java/com/owlmaddie/commands/ConfigurationHandler.java
View file @
e996b966
...
...
@@ -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
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment