Kotlin client for OpenAI's API with multiplatform and coroutines capabilities.
- Install OpenAI API Kotlin client by adding the following dependency to your
build.gradle
file:
repositories {
mavenCentral()
}
dependencies {
implementation "com.aallam.openai:openai-client:3.2.3"
}
- Choose and add to your dependencies one of Ktor's engines.
Alternatively, you can use openai-client-bom by adding the following dependency to your build.gradle
file
dependencies {
// import Kotlin API client BOM
implementation platform('com.aallam.openai:openai-client-bom:3.2.3')
// define dependencies without versions
implementation 'com.aallam.openai:openai-client'
runtimeOnly 'io.ktor:ktor-client-okhttp'
}
In multiplatform projects, add openai client dependency to commonMain
, and choose
an engine for each target.
Create an instance of OpenAI
client:
val openAI = OpenAI(apiKey)
In addition to creating an instance of OpenAI
using just the API key, you can also use OpenAIConfig
to configure the OpenAI client in a more detailed way.
val config = OpenAIConfig(
token = apiKey,
timeout = Timeout(socket = 60.seconds),
// additional configurations...
)
val openAI = OpenAI(config)
Note: OpenAI encourages using environment variables for the API key. Read more.
Use your OpenAI
instance to make API requests. Learn more.
Sample apps are available under sample
, please check the README for running instructions.
The specific rules are already bundled into the Jar which can be interpreted by R8 automatically.
Learn how to import snapshot version
To import snapshot versions into your project, add the following code snippet to your gradle file:
repositories {
//...
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
OpenAI Kotlin API Client is an open-sourced software licensed under the MIT license. This is an unofficial library, it is not affiliated with nor endorsed by OpenAI. Contributions are welcome.