Skip to content

vyfor/groq-kt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

64 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ“š 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

Maven Central Version

Add these dependencies to your project:

dependencies {
    implementation("io.github.vyfor:groq-kt:$version")
    /* 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 = GroqClient(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 = GroqClient(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("$VISION_MODEL")

    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("$TRANSCRIPTION_MODEL")

    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("$TRANSLATION_MODEL")

    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.