@@ -2,6 +2,8 @@ package com.cjcrafter.openai
2
2
3
3
import com.cjcrafter.openai.gson.ChatChoiceChunkAdapter
4
4
import com.cjcrafter.openai.chat.*
5
+ import com.cjcrafter.openai.completions.CompletionRequest
6
+ import com.cjcrafter.openai.completions.CompletionResponse
5
7
import com.cjcrafter.openai.exception.OpenAIError
6
8
import com.cjcrafter.openai.exception.WrappedIOError
7
9
import com.cjcrafter.openai.gson.ChatUserAdapter
@@ -40,17 +42,42 @@ class OpenAI @JvmOverloads constructor(
40
42
private val mediaType = " application/json; charset=utf-8" .toMediaType()
41
43
private val gson = createGson()
42
44
43
- private fun buildRequest (request : Any ): Request {
45
+ private fun buildRequest (request : Any , endpoint : String ): Request {
44
46
val json = gson.toJson(request)
45
47
val body: RequestBody = json.toRequestBody(mediaType)
46
48
return Request .Builder ()
47
- .url(" https://api.openai.com/v1/chat/completions " )
49
+ .url(" https://api.openai.com/v1/$endpoint " )
48
50
.addHeader(" Content-Type" , " application/json" )
49
51
.addHeader(" Authorization" , " Bearer $apiKey " )
50
52
.apply { if (organization != null ) addHeader(" OpenAI-Organization" , organization) }
51
53
.post(body).build()
52
54
}
53
55
56
+ @Throws(OpenAIError ::class )
57
+ fun createCompletion (request : CompletionRequest ): CompletionResponse {
58
+ @Suppress(" DEPRECATION" )
59
+ request.stream = false // use streamResponse for stream=true
60
+ val httpRequest = buildRequest(request, " completions" )
61
+
62
+ // Save the JsonObject to check for errors
63
+ var rootObject: JsonObject ?
64
+ try {
65
+ client.newCall(httpRequest).execute().use { response ->
66
+
67
+ // Servers respond to API calls with json blocks. Since raw JSON isn't
68
+ // very developer friendly, we wrap for easy data access.
69
+ rootObject = JsonParser .parseString(response.body!! .string()).asJsonObject
70
+ if (rootObject!! .has(" error" ))
71
+ throw OpenAIError .fromJson(rootObject!! .get(" error" ).asJsonObject)
72
+
73
+ return gson.fromJson(rootObject, CompletionResponse ::class .java)
74
+ // return ChatResponse(rootObject!!)
75
+ }
76
+ } catch (ex: IOException ) {
77
+ throw WrappedIOError (ex)
78
+ }
79
+ }
80
+
54
81
/* *
55
82
* Blocks the current thread until OpenAI responds to https request. The
56
83
* returned value includes information including tokens, generated text,
@@ -65,7 +92,7 @@ class OpenAI @JvmOverloads constructor(
65
92
fun createChatCompletion (request : ChatRequest ): ChatResponse {
66
93
@Suppress(" DEPRECATION" )
67
94
request.stream = false // use streamResponse for stream=true
68
- val httpRequest = buildRequest(request)
95
+ val httpRequest = buildRequest(request, " chat/completions " )
69
96
70
97
// Save the JsonObject to check for errors
71
98
var rootObject: JsonObject ?
@@ -150,7 +177,7 @@ class OpenAI @JvmOverloads constructor(
150
177
) {
151
178
@Suppress(" DEPRECATION" )
152
179
request.stream = true // use requestResponse for stream=false
153
- val httpRequest = buildRequest(request)
180
+ val httpRequest = buildRequest(request, " chat/completions " )
154
181
155
182
client.newCall(httpRequest).enqueue(object : Callback {
156
183
var cache: ChatResponseChunk ? = null
0 commit comments