Skip to content

Unofficial OpenAi API in Kotlin Multiplatform (Android, iOS, JVM, JS)

License

Notifications You must be signed in to change notification settings

Lyllt8/openai-kotlin

 
 

Repository files navigation

OpenAI Kotlin Multiplatform Library

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.

Usage

Using the library requires an OpenAI API Key!

If you do not have an api key already, follow these steps:

  1. Create an account at https://beta.openai.com/
  2. Navigate to the API Keys page
  3. 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
    }
}

Examples

Android

Setup

  1. Navigate to the android module
  2. Make a copy of the .env.template file and name it .env
  3. Populate the required values for each key in .env
    • NOTE: Do NOT rename any keys!
    • NOTE: Do NOT commit the .env file!
  4. Run application

iOS

Setup

  1. Navigate to the ios module
  2. Make a copy of the Config.xcconfig.template file and name it Config.xcconfig
  3. Populate the required values for each key in Config.xcconfig
    • NOTE: Do NOT rename any keys!
    • NOTE: Do NOT commit the Config.xcconfig file!
  4. Run application

TBA

NodeJS

Setup

  1. Navigate to the web module
  2. Make a copy of the .env.template file and name it .env
  3. 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!
  4. 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.

Tests

References

About

Unofficial OpenAi API in Kotlin Multiplatform (Android, iOS, JVM, JS)

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Kotlin 82.8%
  • TypeScript 7.8%
  • Swift 5.0%
  • Ruby 2.6%
  • HTML 1.2%
  • CSS 0.4%
  • Shell 0.2%