Code Monkey home page Code Monkey logo

openai-kotlin's Introduction

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

openai-kotlin's People

Contributors

aallam avatar renovate[bot] avatar patricklaflamme avatar stuie avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.