/groq-kt

📦 Kotlin Multiplatform library for the Groq API

Primary LanguageKotlinMIT LicenseMIT

📚 Groq Kotlin Library

Maven Central Version

An idiomatic Kotlin Multiplatform library for the Groq API.

💎 Features

  • 🚀 Built with Ktor for seamless networking
  • 🎨 Complete and documented API for chat completions, audio transcription, and translation, including tool support and function calling
  • ⚡ Real-time streaming responses via Kotlin Flows
  • 🧩 Rich, idiomatic DSL for clean and expressive syntax
  • 🔒 Ensures required validations are met before request submission
  • 🔧 Allows specifying default values for API requests in client configuration
  • ⏳ Automatically handles rate limiting and retries on failed requests
  • 📱 Supports multiple platforms:
    • Android
    • iOS
    • JavaScript
    • JVM
    • Linux
    • macOS
    • Windows
    • WebAssembly
    • tvOS, watchOS

🔌 Requirements

  • Java 8 or higher (only for use within the JVM environment)

⚙️ Installation

Add these dependencies to your project:

dependencies {
    implementation("io.github.vyfor:groq-kt:0.1.0")
    /* required */
    implementation("io.ktor:ktor-client-$engine:$version")
}

For the list of supported engines, see Ktor Client Engines.

🧩 Usage

Initialization

/* It is recommended to use an environment variable for the API key */
val apiKey = System.getenv("GROQ_API_KEY")        // JVM
val apiKey = process.env.GROQ_API_KEY             // JS
val apiKey = getenv("GROQ_API_KEY")!!.toKString() // Native

val client = Groq(apiKey)

Specifying default values

You can configure default values for requests. These values will be automatically applied to every request made with a DSL function.

val client = Groq(apiKey) {
  defaults {
    chatCompletion {
      model = GroqModel.LLAMA_3_8B_8192
    }
    
    audioTranscription {
      format = AudioResponseFormat.VERBOSE_JSON
    }
  }
}

Chat completion

val response = client.chat {
    model = GroqModel.LLAMA_3_8B_8192

    messages {
        system("You are a helpful assistant.")
        text("What is the capital of Germany?")
    }
}

Streaming

val response = client.chatStreaming {
    model = GroqModel.LLAMA_3_2_90B_VISION_PREVIEW

    messages {
        user(
          "Describe what you see in the image.",
          "https://example.com/image.png"
        )
    }
}.data.collect { chunk ->
    println(chunk)
}

Audio transcription

val response = client.transcribe {
    model = GroqModel.DISTIL_WHISPER_LARGE_V3_EN

    file("path/to/audio.mp3")
    /* or */
    url = "https://example.com/audio.mp3"
}

Audio translation

Note

Does not seem to be supported by the API yet.

val response = client.translate {
    model = GroqModel.DISTIL_WHISPER_LARGE_V3_EN

    file("path/to/audio.mp3")
    /* or */
    url = "https://example.com/audio.mp3"
}

⚖️ License

groq-kt is licensed under the MIT License.

The project is not affiliated with Groq in any way.

📚 Documentation

The REST API documentation can be found on console.groq.com.

🌱 Contributing

This project is in beta and hasn't undergone excessive testing. Contributions of any kind are welcome, just make sure you read the Contribution Guidelines first. You can also contact me directly on Discord (vyfor) if you have any questions.