Class: Durable::Llm::Providers::Groq

Inherits:
Base
  • Object
show all
Defined in:
lib/durable/llm/providers/groq.rb

Overview

Groq provider for accessing language models via OpenAI-compatible API.

Provides completion, embedding, and streaming capabilities with proper error handling and response normalization.

Defined Under Namespace

Classes: GroqChoice, GroqEmbeddingResponse, GroqMessage, GroqResponse, GroqStreamChoice, GroqStreamDelta, GroqStreamResponse

Constant Summary collapse

BASE_URL =
'https://api.groq.com/openai/v1'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

models, options, #stream?

Constructor Details

#initialize(api_key: nil) ⇒ Groq

Returns a new instance of Groq.



27
28
29
30
31
32
33
34
# File 'lib/durable/llm/providers/groq.rb', line 27

def initialize(api_key: nil)
  super
  @conn = Faraday.new(url: BASE_URL) do |faraday|
    faraday.request :json
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



25
26
27
# File 'lib/durable/llm/providers/groq.rb', line 25

def api_key
  @api_key
end

#connObject (readonly)

Returns the value of attribute conn.



36
37
38
# File 'lib/durable/llm/providers/groq.rb', line 36

def conn
  @conn
end

Class Method Details

.stream?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/durable/llm/providers/groq.rb', line 66

def self.stream?
  true
end

Instance Method Details

#completion(options) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/durable/llm/providers/groq.rb', line 38

def completion(options)
  response = conn.post('chat/completions') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.body = options
  end

  handle_response(response)
end

#default_api_keyObject



21
22
23
# File 'lib/durable/llm/providers/groq.rb', line 21

def default_api_key
  Durable::Llm.configuration.groq&.api_key || ENV['GROQ_API_KEY']
end

#embedding(model:, input:, **options) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/durable/llm/providers/groq.rb', line 47

def embedding(model:, input:, **options)
  response = conn.post('embeddings') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.body = { model: model, input: input, **options }
  end

  handle_response(response, GroqEmbeddingResponse)
end

#modelsObject



56
57
58
59
60
61
62
63
64
# File 'lib/durable/llm/providers/groq.rb', line 56

def models
  response = conn.get('models') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
  end

  resp = handle_response(response).to_h

  resp['data'].map { |model| model['id'] }
end

#stream(options) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/durable/llm/providers/groq.rb', line 70

def stream(options)
  options[:stream] = true

  response = conn.post('chat/completions') do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.headers['Accept'] = 'text/event-stream'

    options['temperature'] = options['temperature'].to_f if options['temperature']

    req.body = options

    user_proc = proc do |chunk, _size, _total|
      yield GroqStreamResponse.new(chunk)
    end

    req.options.on_data = to_json_stream(user_proc: user_proc)
  end

  handle_response(response)
end