Class: Durable::Llm::Providers::Fireworks

Inherits:
Base
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

models, options, #stream?

Constructor Details

#initialize(api_key: nil) ⇒ Fireworks

Initializes a new Fireworks provider instance.

Parameters:

  • api_key (String, nil) (defaults to: nil)

    The API key for Fireworks AI. If not provided, uses the default from configuration or environment.



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_keyObject

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

Returns:

  • (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.

Parameters:

  • options (Hash)

    The completion options including model, messages, temperature, etc.

Returns:

Raises:



45
46
47
48
49
50
51
52
# File 'lib/durable/llm/providers/fireworks.rb', line 45

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/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.

Parameters:

  • model (String)

    The model to use for generating embeddings.

  • input (String, Array<String>)

    The text input(s) to embed.

  • options (Hash)

    Additional options for the embedding request.

Returns:

Raises:



64
65
66
67
68
69
70
71
# File 'lib/durable/llm/providers/fireworks.rb', line 64

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, FireworksEmbeddingResponse)
end

#modelsArray<String>

Retrieves the list of available models from Fireworks AI.

Returns:

  • (Array<String>)

    An array of model IDs available for use.

Raises:



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.

Parameters:

  • options (Hash)

    The completion options including model, messages, temperature, etc.

Yields:

Returns:

  • (nil)

    Returns nil after streaming is complete.

Raises:



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(options)
  options[:stream] = true

  @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 FireworksStreamResponse.new(chunk)
    end

    req.options.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