Module: Durable::Llm::ProviderUtilities
- Defined in:
- lib/durable/llm/provider_utilities.rb
Overview
Utility methods for provider management and comparison
This module offers helper methods for:
-
Discovering available providers
-
Finding providers that support specific models
-
Comparing provider capabilities
-
Routing requests to appropriate providers
Class Method Summary collapse
-
.all_provider_info ⇒ Array<Hash>
Lists all providers with their capabilities.
-
.available_providers ⇒ Array<Symbol>
Lists all available providers.
-
.compare_models(model_ids) ⇒ Hash
Compares models across providers based on common characteristics.
-
.complete_with_fallback(text, providers:, model_map: {}) ⇒ String?
Executes a completion with automatic provider fallback.
-
.fallback_chain(providers, options = {}) ⇒ Array<Durable::Llm::Client>
Creates a fallback chain of providers for redundancy.
-
.models_for_provider(provider_name, **options) ⇒ Array<String>
Gets all models available for a provider.
-
.provider_for_model(model_id) ⇒ Symbol?
Finds the provider that supports a given model.
-
.provider_info(provider_name) ⇒ Hash
Gets provider information including capabilities.
-
.providers_with_capability(capability) ⇒ Array<Symbol>
Finds all providers that support a specific capability.
-
.supports_capability?(provider_name, capability) ⇒ Boolean
Checks if a provider supports a specific capability.
Class Method Details
.all_provider_info ⇒ Array<Hash>
Lists all providers with their capabilities
194 195 196 |
# File 'lib/durable/llm/provider_utilities.rb', line 194 def all_provider_info available_providers.map { |p| provider_info(p) } end |
.available_providers ⇒ Array<Symbol>
Lists all available providers
29 30 31 |
# File 'lib/durable/llm/provider_utilities.rb', line 29 def available_providers Providers.available_providers end |
.compare_models(model_ids) ⇒ Hash
Compares models across providers based on common characteristics
105 106 107 108 109 110 111 112 113 114 |
# File 'lib/durable/llm/provider_utilities.rb', line 105 def compare_models(model_ids) model_ids.map do |model_id| provider = provider_for_model(model_id) { model: model_id, provider: provider, streaming: provider ? supports_capability?(provider, :streaming) : false } end end |
.complete_with_fallback(text, providers:, model_map: {}) ⇒ String?
Executes a completion with automatic provider fallback
157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/durable/llm/provider_utilities.rb', line 157 def complete_with_fallback(text, providers:, model_map: {}) providers.each do |provider| begin client = Durable::Llm.new(provider, model: model_map[provider]) return client.complete(text) rescue StandardError => e warn "Provider #{provider} failed: #{e.message}" next end end nil # All providers failed end |
.fallback_chain(providers, options = {}) ⇒ Array<Durable::Llm::Client>
Creates a fallback chain of providers for redundancy
This method helps build resilient systems by providing fallback options when a primary provider is unavailable.
133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/durable/llm/provider_utilities.rb', line 133 def fallback_chain(providers, = {}) model_map = [:model_map] || {} providers.map do |provider| model = model_map[provider] Durable::Llm.new(provider, model: model) rescue StandardError => e warn "Failed to create client for #{provider}: #{e.message}" nil end.compact end |
.models_for_provider(provider_name, **options) ⇒ Array<String>
Gets all models available for a provider
54 55 56 57 58 |
# File 'lib/durable/llm/provider_utilities.rb', line 54 def models_for_provider(provider_name, **) Durable::Llm.models(provider_name, **) rescue StandardError [] end |
.provider_for_model(model_id) ⇒ Symbol?
Finds the provider that supports a given model
43 44 45 |
# File 'lib/durable/llm/provider_utilities.rb', line 43 def provider_for_model(model_id) Providers.model_id_to_provider(model_id) end |
.provider_info(provider_name) ⇒ Hash
Gets provider information including capabilities
178 179 180 181 182 183 184 185 186 187 |
# File 'lib/durable/llm/provider_utilities.rb', line 178 def provider_info(provider_name) { name: provider_name, streaming: supports_capability?(provider_name, :streaming), embeddings: supports_capability?(provider_name, :embeddings), chat: supports_capability?(provider_name, :chat) } rescue StandardError => e { name: provider_name, error: e. } end |
.providers_with_capability(capability) ⇒ Array<Symbol>
Finds all providers that support a specific capability
93 94 95 96 97 |
# File 'lib/durable/llm/provider_utilities.rb', line 93 def providers_with_capability(capability) available_providers.select do |provider| supports_capability?(provider, capability) end end |
.supports_capability?(provider_name, capability) ⇒ Boolean
Checks if a provider supports a specific capability
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/durable/llm/provider_utilities.rb', line 68 def supports_capability?(provider_name, capability) provider_class = Providers.provider_class_for(provider_name) instance = provider_class.new case capability when :streaming instance.respond_to?(:stream?) && instance.stream? when :embeddings instance.respond_to?(:embedding) when :chat, :completion instance.respond_to?(:completion) else false end rescue StandardError false end |