
Unofficial library facilitates access to OpenAI API, offering statically defined request and response types. Supports multiple platforms with simple and complex usage examples for fetching and streaming data.
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:
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:
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
}
}android module.env.template file and name it .env
.env
.env file!ios moduleConfig.xcconfig.template file and name it Config.xcconfig
Config.xcconfig
Config.xcconfig file!TBA
web module.env.template file and name it .env
.env
REACT_APP)!.env file!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_1This is simply a result of kotlin not being able to export the Long type to JS yet.
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:
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:
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
}
}android module.env.template file and name it .env
.env
.env file!ios moduleConfig.xcconfig.template file and name it Config.xcconfig
Config.xcconfig
Config.xcconfig file!TBA
web module.env.template file and name it .env
.env
REACT_APP)!.env file!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_1This is simply a result of kotlin not being able to export the Long type to JS yet.