Skip to content

Commit 0017d93

Browse files
committed
improve javadocs in OpenAI
1 parent 9be3d3f commit 0017d93

File tree

1 file changed

+37
-9
lines changed
  • src/main/kotlin/com/cjcrafter/openai

1 file changed

+37
-9
lines changed

src/main/kotlin/com/cjcrafter/openai/OpenAI.kt

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ import java.io.IOException
1919
import java.util.function.Consumer
2020

2121
/**
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+
*
2226
* To get your API key:
2327
* 1. Log in to your account: Go to [https://www.openai.com/](openai.com) and
2428
* log in.
@@ -38,7 +42,6 @@ class OpenAI @JvmOverloads constructor(
3842
private val organization: String? = null,
3943
private val client: OkHttpClient = OkHttpClient()
4044
) {
41-
4245
private val mediaType = "application/json; charset=utf-8".toMediaType()
4346
private val gson = createGson()
4447

@@ -56,7 +59,7 @@ class OpenAI @JvmOverloads constructor(
5659
@Throws(OpenAIError::class)
5760
fun createCompletion(request: CompletionRequest): CompletionResponse {
5861
@Suppress("DEPRECATION")
59-
request.stream = false // use streamResponse for stream=true
62+
request.stream = false // use streamCompletion for stream=true
6063
val httpRequest = buildRequest(request, "completions")
6164

6265
// Save the JsonObject to check for errors
@@ -71,9 +74,9 @@ class OpenAI @JvmOverloads constructor(
7174
throw OpenAIError.fromJson(rootObject!!.get("error").asJsonObject)
7275

7376
return gson.fromJson(rootObject, CompletionResponse::class.java)
74-
//return ChatResponse(rootObject!!)
7577
}
7678
} catch (ex: IOException) {
79+
// Wrap the IOException, so we don't need to catch multiple exceptions
7780
throw WrappedIOError(ex)
7881
}
7982
}
@@ -106,9 +109,9 @@ class OpenAI @JvmOverloads constructor(
106109
throw OpenAIError.fromJson(rootObject!!.get("error").asJsonObject)
107110

108111
return gson.fromJson(rootObject, ChatResponse::class.java)
109-
//return ChatResponse(rootObject!!)
110112
}
111113
} catch (ex: IOException) {
114+
// Wrap the IOException, so we don't need to catch multiple exceptions
112115
throw WrappedIOError(ex)
113116
}
114117
}
@@ -214,13 +217,38 @@ class OpenAI @JvmOverloads constructor(
214217

215218
companion object {
216219

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+
*/
217234
@JvmStatic
218235
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())
224252
}
225253
}
226254
}

0 commit comments

Comments
 (0)