Class: Durable::Llm::Providers::Base Abstract
- Inherits:
-
Object
- Object
- Durable::Llm::Providers::Base
- Defined in:
- lib/durable/llm/providers/base.rb
Overview
Subclass and implement required methods
Abstract base class for all LLM providers
This class defines the common interface that all LLM provider implementations must follow. It provides default implementations for caching model lists, handling API keys, and stub implementations for optional features.
Subclasses must implement the following methods:
-
#default_api_key - Returns the default API key from configuration
-
#completion - Performs a completion request
-
#models - Returns list of available models
-
#handle_response - Processes API responses
Subclasses may optionally override:
-
#stream? - Check if streaming is supported
-
#stream - Perform streaming requests
-
#embedding - Generate embeddings
Direct Known Subclasses
Anthropic, AzureOpenai, Cohere, DeepSeek, Fireworks, Google, Groq, Huggingface, Mistral, OpenAI, OpenRouter, Opencode, Perplexity, Together, Xai
Instance Attribute Summary collapse
-
#api_key ⇒ String?
The API key used for authentication.
Class Method Summary collapse
-
.models ⇒ Array<String>
Retrieves the list of available models, with caching.
-
.options ⇒ Array<String>
Returns the list of supported option names for completions.
-
.stream? ⇒ Boolean
Checks if this provider class supports streaming.
Instance Method Summary collapse
-
#completion(options) ⇒ Object
Performs a completion request.
-
#default_api_key ⇒ String?
The default API key for this provider, or nil if not configured.
-
#embedding(model:, input:, **options) ⇒ Object
Performs an embedding request.
-
#initialize(api_key: nil) ⇒ Base
constructor
Initializes a new provider instance.
-
#models ⇒ Array<String>
Retrieves the list of available models for this provider instance.
-
#stream(options) {|Object| ... } ⇒ Object
Performs a streaming completion request.
-
#stream? ⇒ Boolean
Checks if this provider instance supports streaming.
Constructor Details
#initialize(api_key: nil) ⇒ Base
Initializes a new provider instance
76 77 78 |
# File 'lib/durable/llm/providers/base.rb', line 76 def initialize(api_key: nil) @api_key = api_key || default_api_key end |
Instance Attribute Details
#api_key ⇒ String?
Returns The API key used for authentication.
67 68 69 |
# File 'lib/durable/llm/providers/base.rb', line 67 def api_key @api_key end |
Class Method Details
.models ⇒ Array<String>
Retrieves the list of available models, with caching
Models are cached in ‘~/.local/durable-llm/cache/` for 1 hour to reduce API calls. The cache is automatically refreshed after expiration.
98 99 100 101 102 103 |
# File 'lib/durable/llm/providers/base.rb', line 98 def self.models cache_file = model_cache_file return cached_models(cache_file) if cache_valid?(cache_file) fetch_and_cache_models(cache_file) end |
.options ⇒ Array<String>
Returns the list of supported option names for completions
146 147 148 |
# File 'lib/durable/llm/providers/base.rb', line 146 def self. %w[temperature max_tokens top_p frequency_penalty presence_penalty] end |
.stream? ⇒ Boolean
Checks if this provider class supports streaming
161 162 163 |
# File 'lib/durable/llm/providers/base.rb', line 161 def self.stream? false end |
Instance Method Details
#completion(options) ⇒ Object
Performs a completion request
85 86 87 |
# File 'lib/durable/llm/providers/base.rb', line 85 def completion() raise NotImplementedError, 'Subclasses must implement completion' end |
#default_api_key ⇒ String?
Returns The default API key for this provider, or nil if not configured.
61 62 63 |
# File 'lib/durable/llm/providers/base.rb', line 61 def default_api_key raise NotImplementedError, 'Subclasses must implement default_api_key' end |
#embedding(model:, input:, **options) ⇒ Object
Performs an embedding request
189 190 191 |
# File 'lib/durable/llm/providers/base.rb', line 189 def (model:, input:, **) raise NotImplementedError, 'Subclasses must implement embedding' end |
#models ⇒ Array<String>
Retrieves the list of available models for this provider instance
154 155 156 |
# File 'lib/durable/llm/providers/base.rb', line 154 def models raise NotImplementedError, 'Subclasses must implement models' end |
#stream(options) {|Object| ... } ⇒ Object
Performs a streaming completion request
178 179 180 |
# File 'lib/durable/llm/providers/base.rb', line 178 def stream(, &block) raise NotImplementedError, 'Subclasses must implement stream' end |
#stream? ⇒ Boolean
Checks if this provider instance supports streaming
168 169 170 |
# File 'lib/durable/llm/providers/base.rb', line 168 def stream? self.class.stream? end |