Skip to content

Commit 6a9b300

Browse files
committed
add proxy/timeout support (#5)
1 parent 5d8cdc3 commit 6a9b300

File tree

1 file changed

+18
-27
lines changed
  • src/main/kotlin/com/cjcrafter/openai/chat

1 file changed

+18
-27
lines changed

src/main/kotlin/com/cjcrafter/openai/chat/ChatBot.kt

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,29 @@ import java.util.function.Consumer
3131
* to the API dashboard, where you can find your unique API key. Copy and store it securely.
3232
*
3333
* @property apiKey Your OpenAI API key. It starts with `"sk-"` (without the quotes).
34+
* @property client Controls proxies, timeouts, etc.
3435
* @constructor Create a ChatBot for responding to requests.
3536
*/
36-
class ChatBot(private val apiKey: String) {
37+
class ChatBot @JvmOverloads constructor(
38+
private val apiKey: String,
39+
private val client: OkHttpClient = OkHttpClient()
40+
) {
3741

38-
private val client: OkHttpClient = Builder()
39-
.connectTimeout(0, TimeUnit.SECONDS)
40-
.readTimeout(0, TimeUnit.SECONDS).build()
4142
private val mediaType: MediaType = "application/json; charset=utf-8".toMediaType()
4243
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())!! })
4645
.create()
4746

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+
4857
/**
4958
* Blocks the current thread until OpenAI responds to https request. The
5059
* returned value includes information including tokens, generated text,
@@ -59,14 +68,7 @@ class ChatBot(private val apiKey: String) {
5968
@Throws(IOException::class)
6069
fun generateResponse(request: ChatRequest): ChatResponse {
6170
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)
7072

7173
// Save the JsonObject to check for errors
7274
var rootObject: JsonObject? = null
@@ -80,9 +82,6 @@ class ChatBot(private val apiKey: String) {
8082
return ChatResponse(rootObject!!)
8183
}
8284
} 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("\nRoot Object:\n\n$rootObject")
8685
throw ex
8786
}
8887
}
@@ -135,15 +134,7 @@ class ChatBot(private val apiKey: String) {
135134
onFailure: Consumer<IOException> = Consumer { it.printStackTrace() }
136135
) {
137136
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)
147138

148139
client.newCall(httpRequest).enqueue(object : Callback {
149140
var cache: ChatResponseChunk? = null

0 commit comments

Comments
 (0)