Class: Legion::Extensions::Llm::Anthropic::Provider

Inherits:
Provider
  • Object
show all
Includes:
Logging::Helper
Defined in:
lib/legion/extensions/llm/anthropic/provider.rb

Overview

Anthropic Messages API provider implementation for the Legion::Extensions::Llm contract.

Defined Under Namespace

Modules: Capabilities

Constant Summary collapse

CONTEXT_WINDOWS =
{
  'claude-opus-4'   => 200_000,
  'claude-sonnet-4' => 200_000,
  'claude-haiku-4'  => 200_000,
  'claude-3-5'      => 200_000,
  'claude-3-opus'   => 200_000,
  'claude-3-sonnet' => 200_000,
  'claude-3-haiku'  => 200_000
}.freeze
COMPLETION_BASE =
[:completion].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.capabilitiesObject



18
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 18

def capabilities = Capabilities

.configuration_optionsObject



16
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 16

def configuration_options = %i[anthropic_api_key anthropic_api_base anthropic_version]

.configuration_requirementsObject



17
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 17

def configuration_requirements = %i[anthropic_api_key]

.slugObject



15
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 15

def slug = 'anthropic'

Instance Method Details

#api_baseObject



36
37
38
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 36

def api_base
  config.anthropic_api_base || settings[:endpoint] || 'https://api.anthropic.com'
end

#completion_urlObject



47
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 47

def completion_url = '/v1/messages'

#discover_offerings(live: false, raise_on_unreachable: false, **filters) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 66

def discover_offerings(live: false, raise_on_unreachable: false, **filters)
  return filter_cached_offerings(Array(@cached_offerings), filters) unless live

  provider_health = health(live:)
  @cached_offerings = Array(list_models(live:, **filters)).filter_map do |model|
    next unless model_matches_filters?(model, filters)
    next unless model_allowed?(model.id)

    log.debug("[#{slug}] instance=#{provider_instance_id} action=model_discovered model=#{model.id} family=#{model.family}")
    offering_from_model(model, health: provider_health)
  end
  log.info("[#{slug}] instance=#{provider_instance_id} action=discover_complete model_count=#{Array(@cached_offerings).size}")
  @cached_offerings
rescue Faraday::ConnectionFailed, Faraday::TimeoutError => e
  log.warn("[#{slug}] instance=#{provider_instance_id} unreachable: #{e.message}")
  raise if raise_on_unreachable

  []
end

#embed(**_provider_options) ⇒ Object

Raises:

  • (NotImplementedError)


55
56
57
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 55

def embed(**_provider_options)
  raise NotImplementedError, 'Anthropic does not expose embeddings through this provider'
end

#headersObject



40
41
42
43
44
45
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 40

def headers
  identity_headers.merge({
    'x-api-key'         => config.anthropic_api_key,
    'anthropic-version' => config.anthropic_version || settings.dig(:instances, :default, :api_version)
  }.compact)
end

#list_modelsObject



59
60
61
62
63
64
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 59

def list_models(**)
  log.debug { 'listing available Anthropic models' }
  super.tap do |models|
    log.debug { "discovered #{Array(models).size} Anthropic model(s)" }
  end
end

#models_urlObject



49
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 49

def models_url = '/v1/models'

#settingsObject



32
33
34
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 32

def settings
  Anthropic.default_settings
end

#stream_urlObject



48
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 48

def stream_url = completion_url

#translatorObject



51
52
53
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 51

def translator
  @translator ||= Translator.new(config)
end