Class: LittleGhost::Providers::Anthropic::CatalogSource

Inherits:
Models::Catalog::Source show all
Defined in:
lib/little_ghost/providers/anthropic/catalog_source.rb

Overview

Enriches availability and limits from Anthropic's model list endpoint.

Constant Summary collapse

URL =

:nodoc:

URI("https://api.anthropic.com/v1/models")

Constants inherited from Models::Catalog::Source

Models::Catalog::Source::DEFAULT_ATTRIBUTE_MERGE_STRATEGIES

Instance Attribute Summary

Attributes inherited from Models::Catalog::Source

#name

Instance Method Summary collapse

Methods inherited from Models::Catalog::Source

#attribute_merge_strategies

Constructor Details

#initialize(provider:, credential_resolver:) ⇒ CatalogSource

Creates a source for the named provider connection.



11
12
13
14
15
# File 'lib/little_ghost/providers/anthropic/catalog_source.rb', line 11

def initialize(provider:, credential_resolver:)
  super(name: "anthropic")
  @provider = provider
  @credential_resolver = credential_resolver
end

Instance Method Details

#refresh(target: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/little_ghost/providers/anthropic/catalog_source.rb', line 17

def refresh(target: nil)
  api_key = @credential_resolver.call.fetch("api_key")
  values = JSON.parse(
    Support::HTTPClient.new(open_timeout: 5, read_timeout: 30, max_response_bytes: 25 * 1024 * 1024)
      .request(uri: URL, headers: {"x-api-key" => api_key, "anthropic-version" => "2023-06-01"})
  ).fetch("data")
  values.select! { |value| value["id"] == target.model_id } if target
  values.to_h { |value| ["#{@provider}:#{value.fetch("id")}", {available: true, observed_at: value["created_at"]}.compact] }
rescue JSON::ParserError, KeyError => error
  raise ProviderError, "Anthropic returned an invalid catalog: #{error.message}"
end