Class: Lammy::Claude

Inherits:
Object
  • Object
show all
Defined in:
lib/lammy/claude.rb

Overview

Use the Claude API’s Ruby library

Constant Summary collapse

MODELS =
[
  /\Aclaude-3-7-(?:sonnet|haiku|opus)(?:-\d{8}|-latest)?\z/,
  /\Aclaude-3-5-(?:sonnet|haiku|opus)(?:-\d{8}|-latest)?\z/,
  /\Aclaude-3-(?:sonnet|haiku|opus)(?:-\d{8}|-latest)?\z/,
  /\Aclaude-2(?:\.\d)?\z/,
  /\Aclaude-instant-(?:\d\.\d)?\z/
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Claude



19
20
21
# File 'lib/lammy/claude.rb', line 19

def initialize(settings)
  @settings = settings
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



17
18
19
# File 'lib/lammy/claude.rb', line 17

def settings
  @settings
end

Instance Method Details

#chat(user_message, system_message = nil, stream = nil) ⇒ Object

Generate a response with support for structured output



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/lammy/claude.rb', line 24

def chat(user_message, system_message = nil, stream = nil)
  response = client.messages(
    parameters: {
      system: system_message,
      model: settings[:model] || Lammy.configuration.model,
      max_tokens: settings[:max_tokens] || 4096,
      stream: stream ? ->(chunk) { stream.call(stream_content(chunk)) } : nil,
      messages: user_message.is_a?(Array) ? user_message : [vision(L.user(user_message))]
    }.compact
  )

  stream || response.dig('content', 0, 'text')
end