openai-kotlin

Project Url: Aallam/openai-kotlin
Introduction: OpenAI API client for Kotlin with multiplatform and coroutines capabilities.
More: Author   ReportBugs   
Tags:

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)

Use your OpenAI instance to make API requests:

List models kotlin val models: List<Model> = openAI.models()
Retrieve an model kotlin val id = ModelId("text-ada-001") val model: Model = openAI.model(id)
Create completion kotlin 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 kotlin 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 kotlin 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 kotlin val files: List<File> = openAI.files()
Create embeddings kotlin 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 kotlin val moderation = openAI.moderations( request = ModerationRequest( input = "I want to kill them." ) )
Create fine-tunes kotlin val fineTune = openAI.fineTune( request = FineTuneRequest( trainingFile = trainingFile, model = ModelId("ada") ) )
Create images kotlin val images = openAI.image( creation = ImageCreationURL( prompt = "A cute baby sea otter", n = 2, size = ImageSize.is1024x1024 ) )
Edit images kotlin 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 kotlin 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

Apps
About Me
GitHub: Trinea
Facebook: Dev Tools