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

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

Overview

This class is abstract.

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:

Examples:

Implementing a custom provider

class MyProvider < Durable::Llm::Providers::Base
  def default_api_key
    Durable::Llm.configuration.my_provider&.api_key ||
      ENV['MY_PROVIDER_API_KEY']
  end

  def completion(options)
    # Make API request
    response = make_request(options)
    handle_response(response)
  end

  def models
    ['model-1', 'model-2']
  end

  private

  def handle_response(response)
    # Process and return response
  end
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil) ⇒ Base

Initializes a new provider instance

Examples:

Initialize with explicit API key

provider = Durable::Llm::Providers::OpenAI.new(api_key: 'sk-...')

Initialize with default API key from configuration

provider = Durable::Llm::Providers::OpenAI.new

Parameters:

  • api_key (String, nil) (defaults to: nil)

    The API key to use for authentication. If nil, uses default_api_key



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_keyString?

Returns The API key used for authentication.

Returns:

  • (String, nil)

    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

.modelsArray<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.

Examples:

Get available models for OpenAI

models = Durable::Llm::Providers::OpenAI.models
# => ["gpt-4", "gpt-3.5-turbo", ...]

Returns:

  • (Array<String>)

    The list of available model names



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

.optionsArray<String>

Returns the list of supported option names for completions

Returns:

  • (Array<String>)

    The supported option names



146
147
148
# File 'lib/durable/llm/providers/base.rb', line 146

def self.options
  %w[temperature max_tokens top_p frequency_penalty presence_penalty]
end

.stream?Boolean

Checks if this provider class supports streaming

Returns:

  • (Boolean)

    True if streaming is supported, false otherwise



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

Parameters:

  • options (Hash)

    The completion options including model, messages, etc.

Returns:

  • (Object)

    The completion response object

Raises:

  • (NotImplementedError)

    Subclasses must implement this method



85
86
87
# File 'lib/durable/llm/providers/base.rb', line 85

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

#default_api_keyString?

Returns The default API key for this provider, or nil if not configured.

Returns:

  • (String, nil)

    The default API key for this provider, or nil if not configured

Raises:

  • (NotImplementedError)

    Subclasses must implement this method



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

Parameters:

  • model (String)

    The model to use for generating embeddings

  • input (String, Array<String>)

    The input text(s) to embed

  • options (Hash)

    Additional options for the embedding request

Returns:

  • (Object)

    The embedding response object

Raises:

  • (NotImplementedError)

    Subclasses must implement this method



189
190
191
# File 'lib/durable/llm/providers/base.rb', line 189

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

#modelsArray<String>

Retrieves the list of available models for this provider instance

Returns:

  • (Array<String>)

    The list of available model names

Raises:

  • (NotImplementedError)

    Subclasses must implement this method



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

Parameters:

  • options (Hash)

    The stream options including model, messages, etc.

Yields:

  • (Object)

    Yields stream response chunks as they arrive

Returns:

  • (Object)

    The final response object

Raises:

  • (NotImplementedError)

    Subclasses must implement this method



178
179
180
# File 'lib/durable/llm/providers/base.rb', line 178

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

#stream?Boolean

Checks if this provider instance supports streaming

Returns:

  • (Boolean)

    True if streaming is supported, false otherwise



168
169
170
# File 'lib/durable/llm/providers/base.rb', line 168

def stream?
  self.class.stream?
end