Class: Lluminary::Providers::Anthropic

Inherits:
Base
  • Object
show all
Defined in:
lib/lluminary/providers/anthropic.rb

Overview

Provider for Anthropic’s models. Implements the Base provider interface for Anthropic’s API.

Constant Summary collapse

NAME =
:anthropic
DEFAULT_MODEL =
Models::Anthropic::Claude35Sonnet

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**config_overrides) ⇒ Anthropic

Returns a new instance of Anthropic.



17
18
19
20
21
# File 'lib/lluminary/providers/anthropic.rb', line 17

def initialize(**config_overrides)
  super
  @config = { model: DEFAULT_MODEL }.merge(config)
  @client = ::Anthropic::Client.new(api_key: config[:api_key])
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



15
16
17
# File 'lib/lluminary/providers/anthropic.rb', line 15

def client
  @client
end

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/lluminary/providers/anthropic.rb', line 15

def config
  @config
end

Instance Method Details

#call(prompt, _task) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/lluminary/providers/anthropic.rb', line 23

def call(prompt, _task)
  message =
    client.messages.create(
      max_tokens: 1024, # TODO: make this configurable
      messages: [{ role: "user", content: prompt }],
      model: model.class::NAME
    )

  content = message.content.first.text

  {
    raw: content,
    parsed:
      begin
        JSON.parse(content) if content
      rescue JSON::ParserError
        nil
      end
  }
end

#modelObject



44
45
46
# File 'lib/lluminary/providers/anthropic.rb', line 44

def model
  @model ||= config[:model].new
end

#modelsObject



48
49
50
51
# File 'lib/lluminary/providers/anthropic.rb', line 48

def models
  response = @client.models.list
  response.data.map { |model| model.id }
end