Class: GroqRuby::Resources::Chat::Completions

Inherits:
Base
  • Object
show all
Defined in:
lib/groq_ruby/resources/chat/completions.rb

Overview

‘client.chat.completions.create(…)` — the workhorse of the API. Supports both buffered and Server-Sent-Events streaming responses.

Examples:

Buffered call

resp = client.chat.completions.create(
  model: "llama-3.3-70b-versatile",
  messages: [{role: "user", content: "Hello"}]
)
resp.choices.first.message.content

Streaming call (block form)

client.chat.completions.create(
  model: "llama-3.3-70b-versatile",
  messages: [{role: "user", content: "Hello"}],
  stream: true
) { |chunk| print chunk.choices.first.delta.content }

Constant Summary collapse

PATH =
"/openai/v1/chat/completions".freeze
SCHEMA =
Dry::Schema.define do
  required(:model).filled(:string)
  required(:messages).value(:array, min_size?: 1)
  optional(:temperature).filled(:float, gteq?: 0.0, lteq?: 2.0)
  optional(:top_p).filled(:float, gteq?: 0.0, lteq?: 1.0)
  optional(:n).filled(:integer, gt?: 0)
  optional(:max_tokens).filled(:integer, gt?: 0)
  optional(:max_completion_tokens).filled(:integer, gt?: 0)
  optional(:presence_penalty).filled(:float, gteq?: -2.0, lteq?: 2.0)
  optional(:frequency_penalty).filled(:float, gteq?: -2.0, lteq?: 2.0)
  optional(:seed).filled(:integer)
  optional(:stop)
  optional(:stream).filled(:bool)
  optional(:user).maybe(:string)
  optional(:tools).value(:array)
  optional(:tool_choice)
  optional(:response_format).value(:hash)
  optional(:logprobs).filled(:bool)
  optional(:top_logprobs).filled(:integer, gteq?: 0, lteq?: 20)
  optional(:reasoning_effort).filled(:string, included_in?: %w[none default low medium high])
  optional(:reasoning_format).filled(:string, included_in?: %w[hidden raw parsed])
  optional(:service_tier).filled(:string, included_in?: %w[auto on_demand flex performance])
  optional(:parallel_tool_calls).filled(:bool)
  optional(:logit_bias).value(:hash)
  optional(:metadata).value(:hash)
end

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from GroqRuby::Resources::Base

Instance Method Details

#create(stream: false, **params) {|chunk| ... } ⇒ GroqRuby::Models::ChatCompletion, GroqRuby::Streaming::ChunkStream

Parameters:

  • stream (Boolean) (defaults to: false)

    if true, response is streamed via SSE.

  • params (Hash)

    all chat completion parameters supported by the Groq API. Required: ‘:model`, `:messages`. Many optional: `:temperature`, `:top_p`, `:n`, `:max_completion_tokens`, `:stop`, `:tools`, `:tool_choice`, `:response_format`, `:reasoning_effort`, `:reasoning_format`, `:service_tier`, `:seed`, `:logit_bias`, `:metadata`, etc.

Yield Parameters:

Returns:

Raises:



67
68
69
70
71
72
73
74
# File 'lib/groq_ruby/resources/chat/completions.rb', line 67

def create(stream: false, **params, &block)
  body = validate!(SCHEMA, params.merge(stream: stream))
  if stream
    stream_response(body, &block)
  else
    buffered_response(body)
  end
end