Class: Durable::Llm::Providers::Base

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

Direct Known Subclasses

Anthropic, Cohere, Groq, Huggingface, OpenAI

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ Base

Returns a new instance of Base.



11
12
13
# File 'lib/durable/llm/providers/base.rb', line 11

def initialize(api_key: nil)
  @api_key = api_key || default_api_key
end

Instance Attribute Details

#api_keyObject

Returns the value of attribute api_key.



9
10
11
# File 'lib/durable/llm/providers/base.rb', line 9

def api_key
  @api_key
end

Class Method Details

.modelsObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/durable/llm/providers/base.rb', line 19

def self.models
  cache_dir = File.expand_path("#{Dir.home}/.local/durable-llm/cache")

  FileUtils.mkdir_p(cache_dir) unless File.directory?(cache_dir)
  cache_file = File.join(cache_dir, "#{name.split('::').last}.json")

  file_exists = File.exist?(cache_file)
  file_new_enough = file_exists && File.mtime(cache_file) > Time.now - 3600

  if file_exists && file_new_enough
    JSON.parse(File.read(cache_file))
  else
    models = new.models
    File.write(cache_file, JSON.generate(models)) if models.length > 0
    models
  end
end

.stream?Boolean

Returns:

  • (Boolean)


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

def self.stream?
  false
end

Instance Method Details

#completion(options) ⇒ Object

Raises:

  • (NotImplementedError)


15
16
17
# File 'lib/durable/llm/providers/base.rb', line 15

def completion(options)
  raise NotImplementedError, 'Subclasses must implement completion'
end

#default_api_keyObject

Raises:

  • (NotImplementedError)


5
6
7
# File 'lib/durable/llm/providers/base.rb', line 5

def default_api_key
  raise NotImplementedError, 'Subclasses must implement default_api_key'
end

#embedding(model:, input:, **options) ⇒ Object

Raises:

  • (NotImplementedError)


53
54
55
# File 'lib/durable/llm/providers/base.rb', line 53

def embedding(model:, input:, **options)
  raise NotImplementedError, 'Subclasses must implement embedding'
end

#modelsObject

Raises:

  • (NotImplementedError)


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

def models
  raise NotImplementedError, 'Subclasses must implement models'
end

#stream(options, &block) ⇒ Object

Raises:

  • (NotImplementedError)


49
50
51
# File 'lib/durable/llm/providers/base.rb', line 49

def stream(options, &block)
  raise NotImplementedError, 'Subclasses must implement stream'
end

#stream?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/durable/llm/providers/base.rb', line 45

def stream?
  self.class.stream?
end