The Unofficial OpenAI Kotlin library provides convenient access to the OpenAI API for multiple languages via Kotlin Multiplatform Module.
This library contains support for the following platforms:
- Android
- iOS
- JVM
- JS
It includes statically defined types for both requests and responses for all the publicly documented APIs.
Using the library requires an OpenAI API Key!
If you do not have an api key already, follow these steps:
- Create an account at https://beta.openai.com/
- Navigate to the API Keys page
- Create a new secret key
The following is a simple example to fetch the details of a model
val client = OpenAiClient.Builder(apiKey = "<YOUR KEY HERE>") {
// optional configurations
}
val model = client.api.retrieveModel("text-davinci-003")
assertEquals("text-davinci-003", model.id)
Next is a more complex example of streaming the results of a completion request
val model = "text-davinci-003"
val request = CreateCompletionRequest(
model,
prompt = "Say this is a test",
logprobs = null,
stop = listOf("\n")
)
val result = client.api.streamCreateCompletion(request)
CoroutineScope(Dispatchers.IO).launch {
result.collect { completion ->
// ... do stuff here
}
}
- Navigate to the
android
module - Make a copy of the
.env.template
file and name it.env
- Populate the required values for each key in
.env
- NOTE: Do NOT rename any keys!
- NOTE: Do NOT commit the
.env
file!
- Run application
- Navigate to the
ios
module - Make a copy of the
Config.xcconfig.template
file and name itConfig.xcconfig
- Populate the required values for each key in
Config.xcconfig
- NOTE: Do NOT rename any keys!
- NOTE: Do NOT commit the
Config.xcconfig
file!
- Run application
TBA
- Navigate to the
web
module - Make a copy of the
.env.template
file and name it.env
- Populate the required values for each key in
.env
- NOTE: Do NOT rename any keys (they must be prefixed with
REACT_APP
)! - NOTE: Do NOT commit the
.env
file!
- NOTE: Do NOT rename any keys (they must be prefixed with
- Execute
npm run start
To access variables of type Long
(e.g created
/ createdAt
), you must access the a1_1
of the variable.
data.created.a1_1
This is simply a result of kotlin
not being able to export the Long
type to JS
yet.