Class: AiAgent::Claude

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

Instance Attribute Summary

Attributes inherited from Base

#agent, #api_key, #endpoint, #timeout

Instance Method Summary collapse

Methods inherited from Base

#analyze_sentiment, #recognize_entities, #summarize_text

Constructor Details

#initialize(api_key: nil, endpoint: nil, timeout: 60) ⇒ Claude

Returns a new instance of Claude.



12
13
14
15
16
17
# File 'lib/ai_agent/ai_agent/claude.rb', line 12

def initialize(api_key: nil, endpoint: nil, timeout: 60)
  self.agent = CLAUDE
  self.api_key = api_key || ENV['ANTHROPIC_API_KEY']
  self.endpoint = endpoint # nil for default as defined in claude/client
  self.timeout = timeout
end

Instance Method Details

#chat(messages, options: {}) ⇒ Object

When using the ‘chat’ interface we need to do a bit of rejigging because Claude expects the system message to be in options instead of messages.



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

def chat(messages, options: {})
  system_content = nil
  messages.reject! do |hash|
    if hash[:role] == 'system'
      system_content = hash[:content]
      true
    else
      false
    end
  end

  send_messages(messages, options: options.reverse_merge(system: system_content))
end

#clientObject



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

def client
  claude if agent == CLAUDE
end

#format_response(response) ⇒ Object



43
44
45
# File 'lib/ai_agent/ai_agent/claude.rb', line 43

def format_response(response)
  client.parse_response(response) rescue "ERROR: Couldn't extract text from Claude response"
end

#send_messages(messages, options: {}) ⇒ Object



39
40
41
# File 'lib/ai_agent/ai_agent/claude.rb', line 39

def send_messages(messages, options: {})
  client.messages(messages, options)
end