gloq
Types
Builder for a GroqCloud chat completion request.
Construct one with default_groq_request/0 or new_groq_request/0,
chain with_* setters, then call build/1 to get an HTTP request
you can send with any client (e.g. gleam_httpc or gleam_hackney).
pub type GroqRequestBuilder {
GroqRequestBuilder(
key: String,
user: String,
context: String,
model: String,
frequency_penalty: option.Option(Float),
logprobs: option.Option(Bool),
max_tokens: option.Option(Int),
n: option.Option(Int),
parallel_tool_calls: option.Option(Bool),
presence_penalty: option.Option(Float),
seed: option.Option(Int),
stop: option.Option(String),
stream: option.Option(Bool),
temperature: option.Option(Float),
top_p: option.Option(Float),
system_prompt: option.Option(String),
messages: List(Message),
response_format: option.Option(String),
)
}
Constructors
-
GroqRequestBuilder( key: String, user: String, context: String, model: String, frequency_penalty: option.Option(Float), logprobs: option.Option(Bool), max_tokens: option.Option(Int), n: option.Option(Int), parallel_tool_calls: option.Option(Bool), presence_penalty: option.Option(Float), seed: option.Option(Int), stop: option.Option(String), stream: option.Option(Bool), temperature: option.Option(Float), top_p: option.Option(Float), system_prompt: option.Option(String), messages: List(Message), response_format: option.Option(String), )
Values
pub fn add_assistant_message(
builder: GroqRequestBuilder,
content: String,
) -> GroqRequestBuilder
Appends an assistant message to the conversation (useful for continuing a thread).
pub fn add_message(
builder: GroqRequestBuilder,
role: String,
content: String,
) -> GroqRequestBuilder
Appends a message with the given role and content to the conversation.
pub fn add_user_message(
builder: GroqRequestBuilder,
content: String,
) -> GroqRequestBuilder
Appends a user message to the conversation.
pub fn build(
builder: GroqRequestBuilder,
) -> request.Request(String)
Builds the HTTP request ready to send with your chosen HTTP client. Only optional fields that have been explicitly set are included in the body.
Example:
import gleam/httpc
import gloq
let req =
gloq.default_groq_request()
|> gloq.with_key(api_key)
|> gloq.with_context("Hello!")
|> gloq.build()
let response = httpc.send(req)
pub fn default_groq_request() -> GroqRequestBuilder
Creates a new GroqRequestBuilder with sensible defaults.
Default model: llama-3.1-8b-instant. Default user role: "user".
pub fn get_model(
api_key: String,
model_id: String,
) -> request.Request(String)
Builds a GET request to retrieve details for a single model by ID.
pub fn list_models(api_key: String) -> request.Request(String)
Builds a GET request to list all models available on your GroqCloud account.
pub fn new_groq_request() -> GroqRequestBuilder
Creates a new GroqRequestBuilder with no preset values.
You must set key, model, user, and context (or messages) before calling build/1.
pub fn send(builder: GroqRequestBuilder) -> String
Sends the request to the GroqCloud API using the hackney HTTP client and returns the raw response body.
Deprecated — send logic is intentionally left to the consumer so you can use any HTTP client. Call
build/1instead and send the request withgleam_httpc,gleam_hackney, or your preferred client.
pub fn view_models(api_key: String) -> request.Request(String)
Builds a request to list all available models.
Note:
view_modelsis kept for backwards compatibility. New code should preferlist_models/1.
pub fn with_context(
builder: GroqRequestBuilder,
context: String,
) -> GroqRequestBuilder
Sets the prompt text for a single-turn request.
For multi-turn conversations use add_user_message/2 instead.
pub fn with_frequency_penalty(
builder: GroqRequestBuilder,
frequency_penalty: Float,
) -> GroqRequestBuilder
Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency, decreasing the likelihood of repetition.
pub fn with_key(
builder: GroqRequestBuilder,
key: String,
) -> GroqRequestBuilder
Sets the GroqCloud API key.
pub fn with_logprobs(
builder: GroqRequestBuilder,
logprobs: Bool,
) -> GroqRequestBuilder
Whether to return log probabilities of the output tokens. Not yet supported by all models.
pub fn with_max_tokens(
builder: GroqRequestBuilder,
max_tokens: Int,
) -> GroqRequestBuilder
Maximum number of tokens to generate. When None the model default is used.
pub fn with_messages(
builder: GroqRequestBuilder,
messages: List(Message),
) -> GroqRequestBuilder
Replaces the entire conversation message list.
When messages is non-empty, with_user/2 and with_context/2 are ignored.
pub fn with_model(
builder: GroqRequestBuilder,
model: String,
) -> GroqRequestBuilder
Sets the model to use for inference. See gloq/models for available constants.
pub fn with_n(
builder: GroqRequestBuilder,
n: Int,
) -> GroqRequestBuilder
Number of completion choices to generate. Only n = 1 is currently supported.
pub fn with_parallel_tool_calls(
builder: GroqRequestBuilder,
parallel_tool_calls: Bool,
) -> GroqRequestBuilder
Whether to enable parallel function calling during tool use.
pub fn with_presence_penalty(
builder: GroqRequestBuilder,
presence_penalty: Float,
) -> GroqRequestBuilder
Number between -2.0 and 2.0. Positive values penalize tokens that have already appeared, encouraging the model to discuss new topics.
pub fn with_response_format(
builder: GroqRequestBuilder,
format: String,
) -> GroqRequestBuilder
Request a structured response format. Pass "json_object" to get valid JSON.
When using this, instruct the model to produce JSON in your system prompt.
pub fn with_seed(
builder: GroqRequestBuilder,
seed: Int,
) -> GroqRequestBuilder
Setting a seed makes the system attempt to sample deterministically so that repeated requests with the same seed and parameters return the same result.
pub fn with_stop(
builder: GroqRequestBuilder,
stop: String,
) -> GroqRequestBuilder
Up to 4 sequences where the API stops generating tokens. The stop sequence itself is not included in the output.
pub fn with_stream(
builder: GroqRequestBuilder,
stream: Bool,
) -> GroqRequestBuilder
When True, partial message deltas are sent as server-sent events.
pub fn with_system_prompt(
builder: GroqRequestBuilder,
prompt: String,
) -> GroqRequestBuilder
Sets a system prompt that is prepended as the first message.
pub fn with_temperature(
builder: GroqRequestBuilder,
temperature: Float,
) -> GroqRequestBuilder
Sampling temperature between 0 and 2. Higher values increase randomness;
lower values increase focus. Do not use alongside with_top_p/2.
pub fn with_top_p(
builder: GroqRequestBuilder,
top_p: Float,
) -> GroqRequestBuilder
Nucleus sampling threshold. The model considers only the top tokens whose
cumulative probability exceeds this value. Do not use alongside with_temperature/2.
pub fn with_user(
builder: GroqRequestBuilder,
user: String,
) -> GroqRequestBuilder
Sets the user role label for the single-turn message (default: "user").
For multi-turn conversations use add_user_message/2 instead.