Class: Durable::Llm::Providers::Huggingface
- 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
-
#api_key ⇒ Object
Returns the value of attribute api_key.
Class Method Summary collapse
Instance Method Summary collapse
- #completion(options) ⇒ Object
- #default_api_key ⇒ Object
-
#initialize(api_key: nil) ⇒ Huggingface
constructor
A new instance of Huggingface.
- #models ⇒ Object
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_key ⇒ Object
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
.models ⇒ Object
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() model = .delete(:model) || 'gpt2' response = @conn.post("/#{model}") do |req| req.headers['Authorization'] = "Bearer #{@api_key}" req.body = end handle_response(response) end |
#default_api_key ⇒ Object
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 |
#models ⇒ Object
37 38 39 |
# File 'lib/durable/llm/providers/huggingface.rb', line 37 def models self.class.models end |