@@ -19,6 +19,10 @@ import java.io.IOException
19
19
import java.util.function.Consumer
20
20
21
21
/* *
22
+ * The `OpenAI` class contains all the API calls to OpenAI's endpoint. Whether
23
+ * you are working with images, chat, or completions, you need to have an
24
+ * `OpenAI` instance to make the API requests.
25
+ *
22
26
* To get your API key:
23
27
* 1. Log in to your account: Go to [https://www.openai.com/](openai.com) and
24
28
* log in.
@@ -38,7 +42,6 @@ class OpenAI @JvmOverloads constructor(
38
42
private val organization : String? = null ,
39
43
private val client : OkHttpClient = OkHttpClient ()
40
44
) {
41
-
42
45
private val mediaType = " application/json; charset=utf-8" .toMediaType()
43
46
private val gson = createGson()
44
47
@@ -56,7 +59,7 @@ class OpenAI @JvmOverloads constructor(
56
59
@Throws(OpenAIError ::class )
57
60
fun createCompletion (request : CompletionRequest ): CompletionResponse {
58
61
@Suppress(" DEPRECATION" )
59
- request.stream = false // use streamResponse for stream=true
62
+ request.stream = false // use streamCompletion for stream=true
60
63
val httpRequest = buildRequest(request, " completions" )
61
64
62
65
// Save the JsonObject to check for errors
@@ -71,9 +74,9 @@ class OpenAI @JvmOverloads constructor(
71
74
throw OpenAIError .fromJson(rootObject!! .get(" error" ).asJsonObject)
72
75
73
76
return gson.fromJson(rootObject, CompletionResponse ::class .java)
74
- // return ChatResponse(rootObject!!)
75
77
}
76
78
} catch (ex: IOException ) {
79
+ // Wrap the IOException, so we don't need to catch multiple exceptions
77
80
throw WrappedIOError (ex)
78
81
}
79
82
}
@@ -106,9 +109,9 @@ class OpenAI @JvmOverloads constructor(
106
109
throw OpenAIError .fromJson(rootObject!! .get(" error" ).asJsonObject)
107
110
108
111
return gson.fromJson(rootObject, ChatResponse ::class .java)
109
- // return ChatResponse(rootObject!!)
110
112
}
111
113
} catch (ex: IOException ) {
114
+ // Wrap the IOException, so we don't need to catch multiple exceptions
112
115
throw WrappedIOError (ex)
113
116
}
114
117
}
@@ -214,13 +217,38 @@ class OpenAI @JvmOverloads constructor(
214
217
215
218
companion object {
216
219
220
+ /* *
221
+ * Returns a `Gson` object that can be used to read/write .json files.
222
+ * This can be used to save requests/responses to a file, so you can
223
+ * keep a history of all API calls you've made.
224
+ *
225
+ * This is especially important for [ChatRequest], since users will
226
+ * expect you to save their conversations to be continued at later
227
+ * times.
228
+ *
229
+ * If you want to add your own type adapters, use [createGsonBuilder]
230
+ * instead.
231
+ *
232
+ * @return Google gson serializer for json files.
233
+ */
217
234
@JvmStatic
218
235
fun createGson (): Gson {
219
- return GsonBuilder ()
220
- .registerTypeAdapter(ChatUser ::class .java, ChatUserAdapter ())
221
- .registerTypeAdapter(FinishReason ::class .java, FinishReasonAdapter ())
222
- .registerTypeAdapter(ChatChoiceChunk ::class .java, ChatChoiceChunkAdapter ())
223
- .create()
236
+ return createGsonBuilder().create()
237
+ }
238
+
239
+ /* *
240
+ * Returns a `GsonBuilder` with all [com.google.gson.TypeAdapter] used
241
+ * by `com.cjcrafter.openai`. Unless you want to register your own
242
+ * adapters, I recommend using [createGson] instead of this method.
243
+ *
244
+ * @return Google gson builder for serializing json files.
245
+ */
246
+ @JvmStatic
247
+ fun createGsonBuilder (): GsonBuilder {
248
+ return GsonBuilder ()
249
+ .registerTypeAdapter(ChatUser ::class .java, ChatUserAdapter ())
250
+ .registerTypeAdapter(FinishReason ::class .java, FinishReasonAdapter ())
251
+ .registerTypeAdapter(ChatChoiceChunk ::class .java, ChatChoiceChunkAdapter ())
224
252
}
225
253
}
226
254
}
0 commit comments