Module: Durable::Llm::Providers

Defined in:
lib/durable/llm/providers.rb,
lib/durable/llm/providers/xai.rb,
lib/durable/llm/providers/base.rb,
lib/durable/llm/providers/groq.rb,
lib/durable/llm/providers/cohere.rb,
lib/durable/llm/providers/google.rb,
lib/durable/llm/providers/openai.rb,
lib/durable/llm/providers/mistral.rb,
lib/durable/llm/providers/deepseek.rb,
lib/durable/llm/providers/opencode.rb,
lib/durable/llm/providers/together.rb,
lib/durable/llm/providers/anthropic.rb,
lib/durable/llm/providers/fireworks.rb,
lib/durable/llm/providers/openrouter.rb,
lib/durable/llm/providers/perplexity.rb,
lib/durable/llm/providers/huggingface.rb,
lib/durable/llm/providers/azure_openai.rb

Overview

Main module for LLM providers, providing registry and utility methods

Defined Under Namespace

Classes: Anthropic, AzureOpenai, Base, Cohere, DeepSeek, Fireworks, Google, Groq, Huggingface, Mistral, OpenAI, OpenRouter, Opencode, Perplexity, Together, Xai

Constant Summary collapse

Openai =
OpenAI
Claude =
Anthropic
Claude3 =
Anthropic
AzureOpenAI =
AzureOpenai

Class Method Summary collapse

Class Method Details

.available_providersArray<String>

Returns a list of all available provider names as strings.

Alias for providers that returns strings instead of symbols, useful for display purposes in error messages and documentation.

Returns:

  • (Array<String>)

    Array of provider names



92
93
94
# File 'lib/durable/llm/providers.rb', line 92

def self.available_providers
  providers.map(&:to_s).sort
end

.load_allvoid

This method returns an undefined value.

Loads all provider files in the providers directory.

This method dynamically requires all Ruby files in the providers subdirectory, ensuring that all provider classes are loaded and available for use.



35
36
37
# File 'lib/durable/llm/providers.rb', line 35

def self.load_all
  Dir[File.join(__dir__, 'providers', '*.rb')].sort.each { |file| require file }
end

.model_id_to_provider(model_id) ⇒ Class?

Finds the provider class that supports a given model ID.

This method searches through all providers to find which one supports the specified model ID. Returns nil if no provider supports the model.

Parameters:

  • model_id (String)

    The model ID to search for

Returns:

  • (Class, nil)

    The provider class that supports the model, or nil



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/durable/llm/providers.rb', line 121

def self.model_id_to_provider(model_id)
  providers.each do |provider_sym|
    provider_class = provider_class_for(provider_sym)
    begin
      return provider_class if provider_class.models.include?(model_id)
    rescue StandardError
      next
    end
  end
  nil
end

.model_idsArray<String>

Returns a flat list of all model IDs across all providers.

This method aggregates model IDs from all available providers by calling their models method. If a provider fails to return models (e.g., due to missing API keys), it gracefully handles the error and continues.

Returns:

  • (Array<String>)

    Array of model IDs



103
104
105
106
107
108
109
110
111
112
# File 'lib/durable/llm/providers.rb', line 103

def self.model_ids
  providers.flat_map do |provider_sym|
    provider_class = provider_class_for(provider_sym)
    begin
      provider_class.models
    rescue StandardError
      []
    end
  end
end

.provider_class_for(provider_sym) ⇒ Class

Returns the provider class for a given provider symbol.

This method handles the mapping from provider symbols to their corresponding class constants, including special cases where the symbol doesn't directly map to a capitalized class name.

Parameters:

  • provider_sym (Symbol)

    The provider symbol (e.g., :openai, :anthropic)

Returns:

  • (Class)

    The provider class

Raises:

  • (NameError)

    If the provider class cannot be found



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/durable/llm/providers.rb', line 48

def self.provider_class_for(provider_sym)
  # Handle special cases where capitalize doesn't match
  case provider_sym
  when :deepseek
    DeepSeek
  when :openrouter
    OpenRouter
  when :azureopenai
    AzureOpenai
  when :opencode
    Opencode
  else
    const_get(provider_sym.to_s.capitalize)
  end
end

.providersArray<Symbol>

Returns a list of all available provider symbols.

This method dynamically discovers all provider classes by inspecting the module's constants and filtering for classes that inherit from Base, excluding the Base class itself.

Returns:

  • (Array<Symbol>)

    Array of provider symbols



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/durable/llm/providers.rb', line 71

def self.providers
  @providers ||= begin
    provider_classes = constants.select do |const_name|
      const = const_get(const_name)
      next if const.name.split('::').last == 'Base'

      const.is_a?(Class) && const < Durable::Llm::Providers::Base
    end

    provider_classes.map do |const_name|
      const_get(const_name).name.split('::').last.downcase.to_sym
    end.uniq
  end
end