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
1
Merge Requests
1
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
101f2c27
Commit
101f2c27
authored
Apr 03, 2024
by
Jonathan Thomas
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added error logging to Chat GPT HTTP request
parent
3124cc32
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
24 additions
and
12 deletions
+24
-12
ChatGPTRequest.java
src/main/java/com/owlmaddie/ChatGPTRequest.java
+24
-12
No files found.
src/main/java/com/owlmaddie/ChatGPTRequest.java
View file @
101f2c27
...
@@ -6,6 +6,8 @@ import net.minecraft.resource.ResourceManager;
...
@@ -6,6 +6,8 @@ import net.minecraft.resource.ResourceManager;
import
net.minecraft.util.Identifier
;
import
net.minecraft.util.Identifier
;
import
org.slf4j.Logger
;
import
org.slf4j.Logger
;
import
org.slf4j.LoggerFactory
;
import
org.slf4j.LoggerFactory
;
import
java.nio.charset.StandardCharsets
;
import
java.io.IOException
;
import
java.io.BufferedReader
;
import
java.io.BufferedReader
;
import
java.io.InputStream
;
import
java.io.InputStream
;
...
@@ -89,15 +91,11 @@ public class ChatGPTRequest {
...
@@ -89,15 +91,11 @@ public class ChatGPTRequest {
public
static
CompletableFuture
<
String
>
fetchMessageFromChatGPT
(
String
systemPrompt
,
Map
<
String
,
String
>
context
,
List
<
ChatDataManager
.
ChatMessage
>
messageHistory
,
Boolean
jsonMode
)
{
public
static
CompletableFuture
<
String
>
fetchMessageFromChatGPT
(
String
systemPrompt
,
Map
<
String
,
String
>
context
,
List
<
ChatDataManager
.
ChatMessage
>
messageHistory
,
Boolean
jsonMode
)
{
return
CompletableFuture
.
supplyAsync
(()
->
{
return
CompletableFuture
.
supplyAsync
(()
->
{
try
{
try
{
// Load and prepare the system prompt template
String
systemMessage
=
""
;
String
systemMessage
=
""
;
if
(!
systemPrompt
.
isEmpty
())
{
if
(
systemPrompt
!=
null
&&
!
systemPrompt
.
isEmpty
())
{
// Load prompt from resources
systemMessage
=
loadPromptFromResource
(
ModInit
.
serverInstance
.
getResourceManager
(),
"prompts/"
+
systemPrompt
);
systemMessage
=
loadPromptFromResource
(
ModInit
.
serverInstance
.
getResourceManager
(),
"prompts/"
+
systemPrompt
);
}
// Replace placeholders (if any)
systemMessage
=
replacePlaceholders
(
systemMessage
,
context
);
systemMessage
=
replacePlaceholders
(
systemMessage
,
context
);
}
URL
url
=
new
URL
(
"https://api.openai.com/v1/chat/completions"
);
URL
url
=
new
URL
(
"https://api.openai.com/v1/chat/completions"
);
HttpURLConnection
connection
=
(
HttpURLConnection
)
url
.
openConnection
();
HttpURLConnection
connection
=
(
HttpURLConnection
)
url
.
openConnection
();
...
@@ -105,8 +103,9 @@ public class ChatGPTRequest {
...
@@ -105,8 +103,9 @@ public class ChatGPTRequest {
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
connection
.
setRequestProperty
(
"Authorization"
,
"Bearer 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"
);
connection
.
setRequestProperty
(
"Authorization"
,
"Bearer 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"
);
connection
.
setDoOutput
(
true
);
connection
.
setDoOutput
(
true
);
connection
.
setConnectTimeout
(
10000
);
// 10 seconds connection timeout
connection
.
setReadTimeout
(
10000
);
// 10 seconds read timeout
// Build JSON payload for ChatGPT (with previous messages)
List
<
ChatGPTRequestMessage
>
messages
=
new
ArrayList
<>();
List
<
ChatGPTRequestMessage
>
messages
=
new
ArrayList
<>();
messages
.
add
(
new
ChatGPTRequestMessage
(
"system"
,
systemMessage
));
messages
.
add
(
new
ChatGPTRequestMessage
(
"system"
,
systemMessage
));
for
(
ChatDataManager
.
ChatMessage
chatMessage
:
messageHistory
)
{
for
(
ChatDataManager
.
ChatMessage
chatMessage
:
messageHistory
)
{
...
@@ -121,11 +120,24 @@ public class ChatGPTRequest {
...
@@ -121,11 +120,24 @@ public class ChatGPTRequest {
String
jsonInputString
=
gsonInput
.
toJson
(
payload
);
String
jsonInputString
=
gsonInput
.
toJson
(
payload
);
try
(
OutputStream
os
=
connection
.
getOutputStream
())
{
try
(
OutputStream
os
=
connection
.
getOutputStream
())
{
byte
[]
input
=
jsonInputString
.
getBytes
(
"utf-8"
);
byte
[]
input
=
jsonInputString
.
getBytes
(
StandardCharsets
.
UTF_8
);
os
.
write
(
input
,
0
,
input
.
length
);
os
.
write
(
input
,
0
,
input
.
length
);
}
}
try
(
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
(),
"utf-8"
)))
{
// 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
());
}
LOGGER
.
error
(
"Error response from OpenAI: "
+
errorResponse
.
toString
());
}
return
null
;
}
try
(
BufferedReader
br
=
new
BufferedReader
(
new
InputStreamReader
(
connection
.
getInputStream
(),
StandardCharsets
.
UTF_8
)))
{
StringBuilder
response
=
new
StringBuilder
();
StringBuilder
response
=
new
StringBuilder
();
String
responseLine
;
String
responseLine
;
while
((
responseLine
=
br
.
readLine
())
!=
null
)
{
while
((
responseLine
=
br
.
readLine
())
!=
null
)
{
...
@@ -136,12 +148,12 @@ public class ChatGPTRequest {
...
@@ -136,12 +148,12 @@ public class ChatGPTRequest {
ChatGPTResponse
chatGPTResponse
=
gsonOutput
.
fromJson
(
response
.
toString
(),
ChatGPTResponse
.
class
);
ChatGPTResponse
chatGPTResponse
=
gsonOutput
.
fromJson
(
response
.
toString
(),
ChatGPTResponse
.
class
);
if
(
chatGPTResponse
!=
null
&&
chatGPTResponse
.
choices
!=
null
&&
!
chatGPTResponse
.
choices
.
isEmpty
())
{
if
(
chatGPTResponse
!=
null
&&
chatGPTResponse
.
choices
!=
null
&&
!
chatGPTResponse
.
choices
.
isEmpty
())
{
String
content
=
chatGPTResponse
.
choices
.
get
(
0
).
message
.
content
;
String
content
=
chatGPTResponse
.
choices
.
get
(
0
).
message
.
content
;
LOGGER
.
info
(
content
);
LOGGER
.
info
(
"Generated message: "
+
content
);
return
content
;
return
content
;
}
}
}
}
}
catch
(
Exception
e
)
{
}
catch
(
IO
Exception
e
)
{
LOGGER
.
error
(
"Failed to fetch
greeting
from ChatGPT"
,
e
);
LOGGER
.
error
(
"Failed to fetch
message
from ChatGPT"
,
e
);
}
}
return
null
;
// If there was an error or no response, return null
return
null
;
// If there was an error or no response, return null
});
});
...
...
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