Class: Durable::Llm::Providers::Fireworks
- Defined in:
- lib/durable/llm/providers/fireworks.rb
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.
27 28 29 30 31 32 33 34 35 |
# File 'lib/durable/llm/providers/fireworks.rb', line 27 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.
21 22 23 |
# File 'lib/durable/llm/providers/fireworks.rb', line 21 def api_key @api_key end |
Class Method Details
.stream? ⇒ Boolean
88 89 90 |
# File 'lib/durable/llm/providers/fireworks.rb', line 88 def self.stream? true end |
Instance Method Details
#completion(options) ⇒ FireworksResponse
Performs a chat completion request to Fireworks AI.
45 46 47 48 49 50 51 52 |
# File 'lib/durable/llm/providers/fireworks.rb', line 45 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
17 18 19 |
# File 'lib/durable/llm/providers/fireworks.rb', line 17 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.
64 65 66 67 68 69 70 71 |
# File 'lib/durable/llm/providers/fireworks.rb', line 64 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.
80 81 82 83 84 85 86 |
# File 'lib/durable/llm/providers/fireworks.rb', line 80 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.
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/durable/llm/providers/fireworks.rb', line 101 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 |