Class: Durable::Llm::Providers::Huggingface

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

Defined Under Namespace

Classes: HuggingfaceChoice, HuggingfaceResponse

Constant Summary collapse

BASE_URL =
'https://api-inference.huggingface.co/models'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#embedding, #stream, #stream?, stream?

Constructor Details

#initialize(api_key: nil) ⇒ Huggingface

Returns a new instance of Huggingface.



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

def initialize(api_key: nil)
  @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.



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

def api_key
  @api_key
end

Class Method Details

.modelsObject



41
42
43
# File 'lib/durable/llm/providers/huggingface.rb', line 41

def self.models
  %w[gpt2 bert-base-uncased distilbert-base-uncased] # could use expansion
end

Instance Method Details

#completion(options) ⇒ Object



27
28
29
30
31
32
33
34
35
# File 'lib/durable/llm/providers/huggingface.rb', line 27

def completion(options)
  model = options.delete(:model) || 'gpt2'
  response = @conn.post("/#{model}") do |req|
    req.headers['Authorization'] = "Bearer #{@api_key}"
    req.body = options
  end

  handle_response(response)
end

#default_api_keyObject



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

def default_api_key
  Durable::Llm.configuration.huggingface&.api_key || ENV['HUGGINGFACE_API_KEY']
end

#modelsObject



37
38
39
# File 'lib/durable/llm/providers/huggingface.rb', line 37

def models
  self.class.models
end