Class: Durable::Llm::Providers::Groq
- 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
-
#api_key ⇒ Object
Returns the value of attribute api_key.
-
#conn ⇒ Object
readonly
Returns the value of attribute conn.
Class Method Summary collapse
Instance Method Summary collapse
- #completion(options) ⇒ Object
- #default_api_key ⇒ Object
- #embedding(model:, input:, **options) ⇒ Object
-
#initialize(api_key: nil) ⇒ Groq
constructor
A new instance of Groq.
- #models ⇒ Object
- #stream(options) ⇒ Object
Methods inherited from Base
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_key ⇒ Object
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 |
#conn ⇒ Object (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
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() response = conn.post('chat/completions') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.body = end handle_response(response) end |
#default_api_key ⇒ Object
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 (model:, input:, **) response = conn.post('embeddings') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.body = { model: model, input: input, ** } end handle_response(response, GroqEmbeddingResponse) end |
#models ⇒ Object
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() [:stream] = true response = conn.post('chat/completions') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.headers['Accept'] = 'text/event-stream' ['temperature'] = ['temperature'].to_f if ['temperature'] req.body = user_proc = proc do |chunk, _size, _total| yield GroqStreamResponse.new(chunk) end req..on_data = to_json_stream(user_proc: user_proc) end handle_response(response) end |