/openai-kotlin

OpenAI API client for Kotlin with multiplatform and coroutines capabilities.

Primary LanguageKotlinMIT LicenseMIT

OpenAI API client for Kotlin

Maven Central License Kotlin Documentation

Kotlin client for OpenAI's API with multiplatform and coroutines capabilities.

🛠 Setup

  1. Install OpenAI API Kotlin client by adding the following dependency to your gradle.build file:
repositories {
    mavenCentral()
}

dependencies {
    implementation "com.aallam.openai:openai-client:<version>"
}
  1. Choose and add to your dependencies one of Ktor's engines.

Alternatively, you can use openai-client-bom

Multiplaform

In multiplatform projects, add openai client dependency to commonMain, and choose an engine for each target.

💡 Getting Started

Create an instance of OpenAI client:

val openAI = OpenAI(apiKey)

ℹ️ OpenAI encourages using environment variables for the API key. Read more.

Use your OpenAI instance to make API requests:

List models
val models: List<Model> = openAI.models()
Retrieve an model
val id = ModelId("text-ada-001")
val model: Model = openAI.model(id)
Create completion
val completionRequest = CompletionRequest(
    model = ModelId("text-ada-001"),
    prompt = "Somebody once told me the world is gonna roll me",
    echo = true
)
val completion: TextCompletion = openAI.completion(Ada, completionRequest)
Create completion stream
val request = CompletionRequest(
    model = ModelId("text-davinci-002"),
    prompt = "Once upon a time",
    maxTokens = 5,
    temperature = 1.0,
    topP = 1.0,
    n = 1,
    stop = listOf("\n"),
)
val completions: Flow<TextCompletion> = openAI.completions(request)
Create edits
val edit = openAI.edit(
    request = EditsRequest(
        model = ModelId("text-davinci-edit-001"),
        input = "What day of the wek is it?",
        instruction = "Fix the spelling mistakes"
    )
)
List files
val files: List<File> = openAI.files()
Create embeddings
val embeddings: List<Embedding> = openAI.embeddings(
    request = EmbeddingRequest(
        model = ModelId("text-similarity-babbage-001"),
        input = listOf("The food was delicious and the waiter...")
    )
)
Create moderation
val moderation = openAI.moderations(
    request = ModerationRequest(
        input = "I want to kill them."
    )
)
Create fine-tunes
val fineTune = openAI.fineTune(
    request = FineTuneRequest(
        trainingFile = trainingFile,
        model = ModelId("ada")
    )
)
Create images
val images = openAI.image(
    creation = ImageCreationURL(
        prompt = "A cute baby sea otter",
        n = 2,
        size = ImageSize.is1024x1024
    )
)
Edit images
val images = openAI.image(
    edit = ImageEditURL( // or 'ImageEditJSON'
        image = FilePath(imagePath),
        mask = FilePath(maskPath),
        prompt = "a sunlit indoor lounge area with a pool containing a flamingo",
        n = 1,
        size = ImageSize.is1024x1024
    )
)
Create image variants
val images = openAI.image(
    variation = ImageVariationURL( // or, 'ImageVariationJSON'
        image = FilePath(imagePath),
        n = 1,
        size = ImageSize.is1024x1024
    )
)

ℹ️ Sample apps

Sample apps are available under sample, please check the README for running instructions.

📄 License

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.

⭐️ Support

Buy Me A Coffee