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

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.registry_publisherObject



22
23
24
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 22

def registry_publisher
  @registry_publisher ||= RegistryPublisher.new
end

Class Method Details

.capabilitiesObject



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

def capabilities = Capabilities

.configuration_optionsObject



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

def configuration_options = %i[anthropic_api_key anthropic_api_base anthropic_version]

.configuration_requirementsObject



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

def configuration_requirements = %i[anthropic_api_key]

.slugObject



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

def slug = 'anthropic'

Instance Method Details

#api_baseObject



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

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

#completion_urlObject



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

def completion_url = '/v1/messages'

#embed(**_provider_options) ⇒ Object

Raises:

  • (NotImplementedError)


57
58
59
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 57

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

#headersObject



46
47
48
49
50
51
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 46

def headers
  identity_headers.merge({
    'x-api-key' => config.anthropic_api_key,
    'anthropic-version' => config.anthropic_version || settings[:api_version] || '2023-10-02'
  }.compact)
end

#list_modelsObject



61
62
63
64
65
66
67
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 61

def list_models(**)
  log.debug { 'listing available Anthropic models' }
  super.tap do |models|
    log.debug { "discovered #{Array(models).size} Anthropic model(s); publishing to registry" }
    self.class.registry_publisher.publish_models_async(models, readiness: readiness(live: false))
  end
end

#models_urlObject



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

def models_url = '/v1/models'

#settingsObject



38
39
40
# File 'lib/legion/extensions/llm/anthropic/provider.rb', line 38

def settings
  Anthropic.default_settings
end

#stream_urlObject



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

def stream_url = completion_url