Module: Legion::LLM::Call::Providers
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/call/providers.rb
Class Method Summary collapse
- .adapter_instance_config(config, instance_id) ⇒ Object
- .inject_anthropic_cache_control!(opts, provider) ⇒ Object
- .instance_metadata(config) ⇒ Object
- .loaded_provider_modules ⇒ Object
- .log_available ⇒ Object
- .normalize_instance_config(config) ⇒ Object
- .provider_aliases(provider_module) ⇒ Object
- .provider_module?(mod) ⇒ Boolean
-
.rediscover_all_providers ⇒ Object
– private helpers ————————————————–.
- .rediscover_provider_module(provider_module) ⇒ Object
- .register_provider_instance(provider_module, family, aliases, instance_id, config) ⇒ Object
- .safe_provider_family(provider_module) ⇒ Object
- .setup ⇒ Object
Class Method Details
.adapter_instance_config(config, instance_id) ⇒ Object
106 107 108 109 110 |
# File 'lib/legion/llm/call/providers.rb', line 106 def adapter_instance_config(config, instance_id) config.except(:tier, :capabilities).tap do |registry_config| registry_config[:instance_id] ||= instance_id end end |
.inject_anthropic_cache_control!(opts, provider) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/legion/llm/call/providers.rb', line 23 def inject_anthropic_cache_control!(opts, provider) resolved_provider = (provider || Legion::LLM::Settings.value(:default_provider))&.to_sym return unless resolved_provider == :anthropic caching_settings = Legion::LLM::Settings.value(:prompt_caching, default: {}) || {} return unless Legion::LLM::Settings.config_value(caching_settings, :enabled, true) != false min_tokens = Legion::LLM::Settings.config_value(caching_settings, :min_tokens) || 1024 instructions = opts[:instructions] return unless instructions.is_a?(String) && instructions.length > min_tokens log.debug "[llm][providers] inject_anthropic_cache_control provider=#{resolved_provider} length=#{instructions.length}" opts[:instructions] = { content: instructions, cache_control: { type: 'ephemeral' } } end |
.instance_metadata(config) ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/legion/llm/call/providers.rb', line 112 def (config) = { tier: config[:tier], capabilities: config[:capabilities] || [] } [:default_model] = config[:default_model] if config[:default_model] [:source] = config[:source] if config[:source] [:credential_fingerprint] = config[:credential_fingerprint] if config[:credential_fingerprint] end |
.loaded_provider_modules ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/legion/llm/call/providers.rb', line 49 def loaded_provider_modules return [] unless defined?(Legion::Extensions::Llm) Legion::Extensions::Llm.constants(false).filter_map do |const_name| mod = Legion::Extensions::Llm.const_get(const_name, false) provider_module?(mod) ? mod : nil rescue NameError => e log.debug "[llm][providers] action=discover_provider_modules const=#{const_name} error=#{e.class} — #{e.}" nil end end |
.log_available ⇒ Object
129 130 131 132 133 134 135 136 137 |
# File 'lib/legion/llm/call/providers.rb', line 129 def log_available instances = Call::Registry.all_instances if instances.any? summary = instances.map { |i| "#{i[:provider]}/#{i[:instance]}" }.join(', ') log.info "[llm][providers] available instances=#{summary}" else log.error '[llm][providers] no providers available — no instances registered' end end |
.normalize_instance_config(config) ⇒ Object
102 103 104 |
# File 'lib/legion/llm/call/providers.rb', line 102 def normalize_instance_config(config) config.to_h.transform_keys { |key| key.respond_to?(:to_sym) ? key.to_sym : key } end |
.provider_aliases(provider_module) ⇒ Object
96 97 98 99 100 |
# File 'lib/legion/llm/call/providers.rb', line 96 def provider_aliases(provider_module) return [] unless provider_module.respond_to?(:provider_aliases) Array(provider_module.provider_aliases).compact.map(&:to_sym) end |
.provider_module?(mod) ⇒ Boolean
61 62 63 64 65 66 |
# File 'lib/legion/llm/call/providers.rb', line 61 def provider_module?(mod) mod.is_a?(Module) && mod.const_defined?(:PROVIDER_FAMILY, false) && mod.respond_to?(:provider_class) && mod.respond_to?(:discover_instances) end |
.rediscover_all_providers ⇒ Object
– private helpers ————————————————–
43 44 45 46 47 |
# File 'lib/legion/llm/call/providers.rb', line 43 def rediscover_all_providers loaded_provider_modules.each { |provider_module| rediscover_provider_module(provider_module) } rescue StandardError => e handle_exception(e, level: :warn, operation: 'llm.providers.rediscover_all') end |
.rediscover_provider_module(provider_module) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/legion/llm/call/providers.rb', line 68 def rediscover_provider_module(provider_module) family = provider_module::PROVIDER_FAMILY.to_sym aliases = provider_aliases(provider_module) ([family] + aliases).each { |provider| Call::Registry.deregister_provider(provider) } provider_module.discover_instances.each do |instance_id, config| register_provider_instance(provider_module, family, aliases, instance_id, config) end rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: 'llm.providers.rediscover_provider', provider: safe_provider_family(provider_module)) end |
.register_provider_instance(provider_module, family, aliases, instance_id, config) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/legion/llm/call/providers.rb', line 82 def register_provider_instance(provider_module, family, aliases, instance_id, config) normalized_config = normalize_instance_config(config) return if normalized_config[:enabled] == false registry_config = adapter_instance_config(normalized_config, instance_id) = (normalized_config) adapter = Call::LexLLMAdapter.new(family, provider_module.provider_class, instance_config: registry_config) Call::Registry.register(family, adapter, instance: instance_id, metadata: ) aliases.each do |provider_alias| Call::Registry.register(provider_alias, adapter, instance: instance_id, metadata: ) end end |
.safe_provider_family(provider_module) ⇒ Object
120 121 122 123 124 125 126 127 |
# File 'lib/legion/llm/call/providers.rb', line 120 def safe_provider_family(provider_module) return nil unless provider_module&.const_defined?(:PROVIDER_FAMILY, false) provider_module::PROVIDER_FAMILY rescue StandardError => e log.debug "[llm][providers] action=safe_provider_family error=#{e.class} — #{e.}" nil end |
.setup ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/legion/llm/call/providers.rb', line 13 def setup log.debug '[llm][providers] setup.enter' rediscover_all_providers log_available log.debug '[llm][providers] setup.exit' rescue StandardError => e handle_exception(e, level: :error, operation: 'llm.providers.setup') raise end |