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

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

Defined Under Namespace

Classes: GroqChoice, 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, #stream, #stream?

Constructor Details

#initialize(api_key: nil) ⇒ Groq

Returns a new instance of Groq.



30
31
32
# File 'lib/durable/llm/providers/groq.rb', line 30

def initialize(api_key: nil)
  @api_key = api_key || default_api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



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

def api_key
  @api_key
end

Class Method Details

.connObject



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

def self.conn
  Faraday.new(url: BASE_URL) do |faraday|
    faraday.request :json
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end

.stream?Boolean

Returns:

  • (Boolean)


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

def self.stream?
  false
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

#connObject



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

def conn
  self.class.conn
end

#default_api_keyObject



12
13
14
# File 'lib/durable/llm/providers/groq.rb', line 12

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)
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