Class: Durable::Llm::Providers::Fireworks
- Defined in:
- lib/durable/llm/providers/fireworks.rb
Overview
Fireworks AI provider for accessing Fireworks AI's language models.
Provides completion, embedding, and streaming capabilities with proper error handling and response normalization.
Defined Under Namespace
Classes: FireworksChoice, FireworksEmbeddingResponse, FireworksMessage, FireworksResponse, FireworksStreamChoice, FireworksStreamDelta, FireworksStreamResponse
Constant Summary collapse
- BASE_URL =
'https://api.fireworks.ai/inference/v1'
Instance Attribute Summary collapse
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Class Method Summary collapse
Instance Method Summary collapse
-
#completion(options) ⇒ FireworksResponse
Performs a chat completion request to Fireworks AI.
- #default_api_key ⇒ Object
-
#embedding(model:, input:, **options) ⇒ FireworksEmbeddingResponse
Generates embeddings for the given input using Fireworks AI.
-
#initialize(api_key: nil) ⇒ Fireworks
constructor
Initializes a new Fireworks provider instance.
-
#models ⇒ Array<String>
Retrieves the list of available models from Fireworks AI.
-
#stream(options) {|FireworksStreamResponse| ... } ⇒ nil
Performs a streaming chat completion request to Fireworks AI.
Methods inherited from Base
Constructor Details
#initialize(api_key: nil) ⇒ Fireworks
Initializes a new Fireworks provider instance.
31 32 33 34 35 36 37 38 39 |
# File 'lib/durable/llm/providers/fireworks.rb', line 31 def initialize(api_key: nil) super() @api_key = api_key || default_api_key @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/fireworks.rb', line 25 def api_key @api_key end |
Class Method Details
.stream? ⇒ Boolean
92 93 94 |
# File 'lib/durable/llm/providers/fireworks.rb', line 92 def self.stream? true end |
Instance Method Details
#completion(options) ⇒ FireworksResponse
Performs a chat completion request to Fireworks AI.
49 50 51 52 53 54 55 56 |
# File 'lib/durable/llm/providers/fireworks.rb', line 49 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/fireworks.rb', line 21 def default_api_key Durable::Llm.configuration.fireworks&.api_key || ENV['FIREWORKS_API_KEY'] end |
#embedding(model:, input:, **options) ⇒ FireworksEmbeddingResponse
Generates embeddings for the given input using Fireworks AI.
68 69 70 71 72 73 74 75 |
# File 'lib/durable/llm/providers/fireworks.rb', line 68 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, FireworksEmbeddingResponse) end |
#models ⇒ Array<String>
Retrieves the list of available models from Fireworks AI.
84 85 86 87 88 89 90 |
# File 'lib/durable/llm/providers/fireworks.rb', line 84 def models response = @conn.get('models') do |req| req.headers['Authorization'] = "Bearer #{@api_key}" end handle_response(response).data.map { |model| model['id'] } end |
#stream(options) {|FireworksStreamResponse| ... } ⇒ nil
Performs a streaming chat completion request to Fireworks AI.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/durable/llm/providers/fireworks.rb', line 105 def stream() [:stream] = true @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 FireworksStreamResponse.new(chunk) end req..on_data = to_json_stream(user_proc: user_proc) end # For streaming, errors are handled in to_json_stream, no need for handle_response nil end |