gloq

Package VersionHex Docstest

A Gleam library for the GroqCloud inference API. Build type-safe chat completion requests using a fluent builder, then send them with any HTTP client you choose.

gleam add gloq

After adding, run gleam deps download if prompted.


Quick start

import gleam/httpc
import gleam/io
import gloq
import gloq/response

pub fn main() {
  let assert Ok(res) =
    gloq.default_groq_request()
    |> gloq.with_key("YOUR_API_KEY")
    |> gloq.with_context("What is the capital of France?")
    |> gloq.build()
    |> httpc.send()

  let assert Ok(completion) = response.decode(res.body)
  io.println(response.content(completion))
}

Choosing a model

Import constants from gloq/models instead of using raw strings:

import gloq
import gloq/models

gloq.default_groq_request()
|> gloq.with_key(api_key)
|> gloq.with_model(models.llama_3_3_70b_versatile)
|> gloq.with_context("Explain monads in one sentence.")
|> gloq.build()

Available model constants (see gloq/models for the full list):

Constant Model ID
models.llama_3_3_70b_versatilellama-3.3-70b-versatile
models.llama_3_1_8b_instantllama-3.1-8b-instant(default)
models.llama_3_1_70b_versatilellama-3.1-70b-versatile
models.mixtral_8x7b_32768mixtral-8x7b-32768
models.gemma2_9b_itgemma2-9b-it
models.deepseek_r1_distill_llama_70bdeepseek-r1-distill-llama-70b

Multi-turn conversations

Use add_user_message, add_assistant_message, and with_system_prompt to build a full conversation thread:

gloq.default_groq_request()
|> gloq.with_key(api_key)
|> gloq.with_model(models.llama_3_3_70b_versatile)
|> gloq.with_system_prompt("You are a terse assistant. Reply in one sentence.")
|> gloq.add_user_message("What is Gleam?")
|> gloq.add_assistant_message("Gleam is a type-safe language that runs on the Erlang VM.")
|> gloq.add_user_message("What is its mascot?")
|> gloq.build()

Structured JSON output

Pass "json_object" to with_response_format and instruct the model to return JSON in the system prompt:

gloq.default_groq_request()
|> gloq.with_key(api_key)
|> gloq.with_system_prompt("Respond only with valid JSON.")
|> gloq.with_context("Give me a person with a name and age.")
|> gloq.with_response_format("json_object")
|> gloq.build()

Decoding responses

gloq/response provides types and a decoder for the API response:

import gloq/response

case response.decode(res.body) {
  Ok(completion) -> {
    io.println(response.content(completion))
    // completion.usage.total_tokens
    // completion.model
    // response.all_contents(completion)
  }
  Error(_) ->
    // Try decoding an API error instead
    case response.decode_error(res.body) {
      Ok(err) -> io.println(err.message)
      Error(_) -> io.println("Unknown error")
    }
}

All builder options

Function Type Default Description
with_keyString"" GroqCloud API key
with_modelString"llama-3.1-8b-instant" Model to use
with_contextString"" Single-turn prompt
with_userString"user" Role label for single-turn message
with_system_promptString System message prepended to every request
add_user_messageString Append a user turn
add_assistant_messageString Append an assistant turn
with_messagesList(Message)[] Replace the full message list
with_temperatureFloat1.0 0–2, higher = more random
with_top_pFloat1.0 Nucleus sampling threshold
with_max_tokensInt(model default) Max tokens to generate
with_frequency_penaltyFloat0.0 -2.0–2.0, penalises repetition
with_presence_penaltyFloat0.0 -2.0–2.0, encourages new topics
with_seedInt Enables deterministic sampling
with_stopString Stop sequence
with_streamBoolFalse Enable SSE streaming
with_nInt1 Number of completions (only 1 supported)
with_logprobsBool Return token log probabilities
with_parallel_tool_callsBoolTrue Allow parallel tool calls
with_response_formatString"json_object" for structured output

Model listing

import gleam/httpc
import gloq

// List all available models
let req = gloq.list_models(api_key)

// Get a specific model's details
let req = gloq.get_model(api_key, "llama-3.1-8b-instant")

Migration from v1.x

Changed defaults

new_groq_request behaviour

JSON body fix

New fields on GroqRequestBuilder

Removed dependencies

send is still present but deprecated


Further documentation at https://hexdocs.pm/gloq.