1
1
package com .beanbeanjuice .cafebot .utility .api ;
2
2
3
- import com .beanbeanjuice .cafeapi .wrapper .requests .Request ;
4
3
import com .beanbeanjuice .cafebot .CafeBot ;
5
- import com .beanbeanjuice .cafebot .utility .listeners .ai .AIResponseListener ;
6
4
import com .beanbeanjuice .cafebot .utility .listeners .ai .PreviousMessage ;
5
+ import com .beanbeanjuice .cafebot .utility .logging .LogLevel ;
7
6
import com .fasterxml .jackson .databind .JsonNode ;
8
7
import com .fasterxml .jackson .databind .ObjectMapper ;
9
8
import com .fasterxml .jackson .databind .node .ArrayNode ;
10
- import com .fasterxml .jackson .databind .node .JsonNodeFactory ;
11
9
import com .fasterxml .jackson .databind .node .ObjectNode ;
12
- import com .fasterxml .jackson .databind .util .JSONPObject ;
13
10
import lombok .RequiredArgsConstructor ;
14
11
import org .apache .hc .client5 .http .async .methods .SimpleHttpRequest ;
15
12
import org .apache .hc .client5 .http .async .methods .SimpleHttpResponse ;
19
16
import org .apache .hc .core5 .http .HttpResponse ;
20
17
import org .apache .hc .core5 .http .HttpStatus ;
21
18
import org .apache .hc .core5 .http .Method ;
22
- import org .apache .hc .core5 .net .URIAuthority ;
23
19
import org .apache .hc .core5 .net .URIBuilder ;
24
20
25
21
import java .io .IOException ;
34
30
@ RequiredArgsConstructor
35
31
public class OpenAIAPIWrapper {
36
32
33
+ private final CafeBot bot ;
37
34
private final String authorizationKey ;
38
35
private final String assistantID ;
39
36
private final Map <String , Map <String , Queue <PreviousMessage >>> previousMessageMap ;
@@ -55,8 +52,6 @@ public void setHeaders() {
55
52
private CompletableFuture <String > getThread (final String guildID ) throws URISyntaxException {
56
53
if (aiThreads .containsKey (guildID )) return CompletableFuture .supplyAsync (() -> aiThreads .get (guildID ));
57
54
58
- System .out .println ("Thread not found, creating." );
59
-
60
55
return createThread ().thenApplyAsync ((threadID ) -> {
61
56
aiThreads .put (guildID , threadID );
62
57
return threadID ;
@@ -84,7 +79,10 @@ private CompletableFuture<String> createThread() throws URISyntaxException {
84
79
throw new RuntimeException (e );
85
80
}
86
81
87
- return body .get ("id" ).asText ();
82
+ String newThreadID = body .get ("id" ).asText ();
83
+ bot .getLogger ().log (OpenAIAPIWrapper .class , LogLevel .DEBUG , "Created AI Thread: " + newThreadID , true , false );
84
+
85
+ return newThreadID ;
88
86
});
89
87
}
90
88
@@ -119,9 +117,11 @@ private CompletableFuture<String> createRun(final String guildID, final String c
119
117
120
118
return CompletableFuture .supplyAsync (() -> {
121
119
SimpleHttpResponse httpResponse = null ;
120
+
122
121
try {
123
122
httpResponse = (SimpleHttpResponse ) this .get (httpRequest );
124
123
} catch (Exception e ) {
124
+ bot .getLogger ().log (OpenAIAPIWrapper .class , LogLevel .WARN , "Error getting response from run." );
125
125
throw new RuntimeException (e );
126
126
}
127
127
@@ -131,10 +131,14 @@ private CompletableFuture<String> createRun(final String guildID, final String c
131
131
try {
132
132
body = new ObjectMapper ().readTree (bodyBytes );
133
133
} catch (IOException e ) {
134
+ bot .getLogger ().log (OpenAIAPIWrapper .class , LogLevel .WARN , "Error reading response from run." );
134
135
throw new RuntimeException (e );
135
136
}
136
137
137
- return body .get ("id" ).asText ();
138
+ String newRunID = body .get ("id" ).asText ();
139
+ bot .getLogger ().log (OpenAIAPIWrapper .class , LogLevel .DEBUG , "Created Run: " + newRunID );
140
+
141
+ return newRunID ;
138
142
});
139
143
140
144
}
@@ -156,7 +160,10 @@ private CompletableFuture<String> getLatestMessageFromRun(final String threadID,
156
160
try { body = new ObjectMapper ().readTree (bodyBytes ); }
157
161
catch (IOException e ) { throw new RuntimeException (e ); }
158
162
159
- return body .get ("data" ).get (0 ).get ("content" ).get (0 ).get ("text" ).get ("value" ).asText ();
163
+ String newResponse = body .get ("data" ).get (0 ).get ("content" ).get (0 ).get ("text" ).get ("value" ).asText ();
164
+ bot .getLogger ().log (OpenAIAPIWrapper .class , LogLevel .DEBUG , "Created New Response: " + newResponse );
165
+
166
+ return newResponse ;
160
167
});
161
168
}
162
169
@@ -176,7 +183,10 @@ private CompletableFuture<String> getRunStatus(final String threadID, final Stri
176
183
try { body = new ObjectMapper ().readTree (bodyBytes ); }
177
184
catch (IOException e ) { throw new RuntimeException (e ); }
178
185
179
- return body .get ("status" ).asText ();
186
+ String newRunStatus = body .get ("status" ).asText ();
187
+ bot .getLogger ().log (OpenAIAPIWrapper .class , LogLevel .DEBUG , "New AI Run Status: " + newRunStatus );
188
+
189
+ return newRunStatus ;
180
190
});
181
191
}
182
192
@@ -219,8 +229,13 @@ public CompletableFuture<String> getResponse(final String guildID, final String
219
229
// Run assistant on thread
220
230
.thenComposeAsync (
221
231
(threadID ) -> {
222
- try { return this .createRun (guildID , channelID , threadID ); }
223
- catch (URISyntaxException e ) { throw new RuntimeException (e ); }
232
+ try {
233
+ return this .createRun (guildID , channelID , threadID );
234
+ }
235
+ catch (URISyntaxException e ) {
236
+ bot .getLogger ().log (OpenAIAPIWrapper .class , LogLevel .WARN , "Error running AI on thread." );
237
+ throw new RuntimeException (e );
238
+ }
224
239
}
225
240
)
226
241
0 commit comments