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 =
%w[
  claude-3-5-sonnet-latest claude-3-5-haiku-latest claude-3-opus-latest
  claude-3-5-sonnet-20240620 claude-3-5-haiku-20241022
  claude-3-opus-20240229 claude-3-sonnet-20240229 claude-3-haiku-20240307
  claude-2.1 claude-2.0 claude-instant-1.2
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings) ⇒ Claude

Returns a new instance of Claude.



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

def initialize(settings)
  @settings = settings
end

Instance Attribute Details

#settingsObject (readonly)

Returns the value of attribute settings.



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

def settings
  @settings
end

Instance Method Details

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

Generate a response with support for structured output



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

def chat(user_message, system_message = nil, stream = nil)
  response = client.messages(
    parameters: {
      system: system_message,
      model: settings[: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