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

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

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.



23
24
25
26
27
28
29
30
# File 'lib/durable/llm/providers/groq.rb', line 23

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.



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

def api_key
  @api_key
end

#connObject (readonly)

Returns the value of attribute conn.



32
33
34
# File 'lib/durable/llm/providers/groq.rb', line 32

def conn
  @conn
end

Class Method Details

.stream?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/durable/llm/providers/groq.rb', line 62

def self.stream?
  true
end

Instance Method Details

#completion(options) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/durable/llm/providers/groq.rb', line 34

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



17
18
19
# File 'lib/durable/llm/providers/groq.rb', line 17

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

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



43
44
45
46
47
48
49
50
# File 'lib/durable/llm/providers/groq.rb', line 43

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



52
53
54
55
56
57
58
59
60
# File 'lib/durable/llm/providers/groq.rb', line 52

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/durable/llm/providers/groq.rb', line 66

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