Class: Durable::Llm::Providers::Huggingface
- Inherits:
-
Base
- Object
- Base
- Durable::Llm::Providers::Huggingface
show all
- Defined in:
- lib/durable/llm/providers/huggingface.rb
Overview
Hugging Face provider for accessing Hugging Face’s inference API models.
Provides completion, embedding, and streaming capabilities with authentication handling, error management, and response normalization.
Defined Under Namespace
Classes: HuggingfaceChoice, HuggingfaceEmbeddingResponse, HuggingfaceResponse, HuggingfaceStreamResponse, HuggingfaceStreamToken
Constant Summary
collapse
- BASE_URL =
'https://api-inference.huggingface.co'
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from Base
options, #stream?
Constructor Details
#initialize(api_key: nil) ⇒ Huggingface
Returns a new instance of Huggingface.
27
28
29
30
31
32
33
34
35
|
# File 'lib/durable/llm/providers/huggingface.rb', line 27
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
super()
end
|
Instance Attribute Details
#api_key ⇒ Object
Returns the value of attribute api_key.
25
26
27
|
# File 'lib/durable/llm/providers/huggingface.rb', line 25
def api_key
@api_key
end
|
Class Method Details
.models ⇒ Object
78
79
80
|
# File 'lib/durable/llm/providers/huggingface.rb', line 78
def self.models
%w[gpt2 bert-base-uncased distilbert-base-uncased]
end
|
.stream? ⇒ Boolean
60
61
62
|
# File 'lib/durable/llm/providers/huggingface.rb', line 60
def self.stream?
true
end
|
Instance Method Details
#completion(options) ⇒ Object
37
38
39
40
41
42
43
44
45
|
# File 'lib/durable/llm/providers/huggingface.rb', line 37
def completion(options)
model = options.delete(:model) || 'gpt2'
response = @conn.post("models/#{model}") do |req|
req.['Authorization'] = "Bearer #{@api_key}"
req.body = options
end
handle_response(response)
end
|
#default_api_key ⇒ Object
21
22
23
|
# File 'lib/durable/llm/providers/huggingface.rb', line 21
def default_api_key
Durable::Llm.configuration.huggingface&.api_key || ENV['HUGGINGFACE_API_KEY']
end
|
#embedding(model:, input:, **options) ⇒ Object
47
48
49
50
51
52
53
54
|
# File 'lib/durable/llm/providers/huggingface.rb', line 47
def embedding(model:, input:, **options)
response = @conn.post("models/#{model}") do |req|
req.['Authorization'] = "Bearer #{@api_key}"
req.body = { inputs: input, **options }
end
handle_response(response, HuggingfaceEmbeddingResponse)
end
|
#models ⇒ Object
56
57
58
|
# File 'lib/durable/llm/providers/huggingface.rb', line 56
def models
self.class.models
end
|
#stream(options) ⇒ Object
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/durable/llm/providers/huggingface.rb', line 64
def stream(options)
model = options.delete(:model) || 'gpt2'
options[:stream] = true
@conn.post("models/#{model}") do |req|
req.['Authorization'] = "Bearer #{@api_key}"
req.['Accept'] = 'text/event-stream'
req.body = options
req.options.on_data = to_json_stream(user_proc: proc { |chunk|
yield HuggingfaceStreamResponse.new(chunk)
})
end
end
|