@@ -31,20 +31,29 @@ import java.util.function.Consumer
31
31
* to the API dashboard, where you can find your unique API key. Copy and store it securely.
32
32
*
33
33
* @property apiKey Your OpenAI API key. It starts with `"sk-"` (without the quotes).
34
+ * @property client Controls proxies, timeouts, etc.
34
35
* @constructor Create a ChatBot for responding to requests.
35
36
*/
36
- class ChatBot (private val apiKey : String ) {
37
+ class ChatBot @JvmOverloads constructor(
38
+ private val apiKey : String ,
39
+ private val client : OkHttpClient = OkHttpClient ()
40
+ ) {
37
41
38
- private val client: OkHttpClient = Builder ()
39
- .connectTimeout(0 , TimeUnit .SECONDS )
40
- .readTimeout(0 , TimeUnit .SECONDS ).build()
41
42
private val mediaType: MediaType = " application/json; charset=utf-8" .toMediaType()
42
43
private val gson: Gson = GsonBuilder ()
43
- .registerTypeAdapter(
44
- ChatUser ::class .java,
45
- JsonSerializer <ChatUser > { src, _, context -> context!! .serialize(src!! .name.lowercase())!! })
44
+ .registerTypeAdapter(ChatUser ::class .java, JsonSerializer <ChatUser > { src, _, context -> context!! .serialize(src!! .name.lowercase())!! })
46
45
.create()
47
46
47
+ private fun buildRequest (request : ChatRequest ): Request {
48
+ val json = gson.toJson(request)
49
+ val body: RequestBody = json.toRequestBody(mediaType)
50
+ return Request .Builder ()
51
+ .url(" https://api.openai.com/v1/chat/completions" )
52
+ .addHeader(" Content-Type" , " application/json" )
53
+ .addHeader(" Authorization" , " Bearer $apiKey " )
54
+ .post(body).build()
55
+ }
56
+
48
57
/* *
49
58
* Blocks the current thread until OpenAI responds to https request. The
50
59
* returned value includes information including tokens, generated text,
@@ -59,14 +68,7 @@ class ChatBot(private val apiKey: String) {
59
68
@Throws(IOException ::class )
60
69
fun generateResponse (request : ChatRequest ): ChatResponse {
61
70
request.stream = false // use streamResponse for stream=true
62
-
63
- val json = gson.toJson(request)
64
- val body: RequestBody = json.toRequestBody(mediaType)
65
- val httpRequest: Request = Request .Builder ()
66
- .url(" https://api.openai.com/v1/chat/completions" )
67
- .addHeader(" Content-Type" , " application/json" )
68
- .addHeader(" Authorization" , " Bearer $apiKey " )
69
- .post(body).build()
71
+ val httpRequest = buildRequest(request)
70
72
71
73
// Save the JsonObject to check for errors
72
74
var rootObject: JsonObject ? = null
@@ -80,9 +82,6 @@ class ChatBot(private val apiKey: String) {
80
82
return ChatResponse (rootObject!! )
81
83
}
82
84
} catch (ex: Throwable ) {
83
- System .err.println (" Some error occurred whilst using the Chat Completion API" )
84
- System .err.println (" Request:\n\n $json " )
85
- System .err.println (" \n Root Object:\n\n $rootObject " )
86
85
throw ex
87
86
}
88
87
}
@@ -135,15 +134,7 @@ class ChatBot(private val apiKey: String) {
135
134
onFailure : Consumer <IOException > = Consumer { it.printStackTrace() }
136
135
) {
137
136
request.stream = true // use requestResponse for stream=false
138
-
139
- val json = gson.toJson(request)
140
- val body: RequestBody = json.toRequestBody(mediaType)
141
- val httpRequest: Request = Request .Builder ()
142
- .url(" https://api.openai.com/v1/chat/completions" )
143
- .addHeader(" Content-Type" , " application/json" )
144
- .addHeader(" Authorization" , " Bearer $apiKey " )
145
- .post(body)
146
- .build()
137
+ val httpRequest = buildRequest(request)
147
138
148
139
client.newCall(httpRequest).enqueue(object : Callback {
149
140
var cache: ChatResponseChunk ? = null
0 commit comments