Class: RailsConsoleAi::Providers::Anthropic

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

Constant Summary collapse

API_URL =
'https://api.anthropic.com'.freeze

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from RailsConsoleAi::Providers::Base

Instance Method Details

#chat(messages, system_prompt: nil) ⇒ Object



6
7
8
9
# File 'lib/rails_console_ai/providers/anthropic.rb', line 6

def chat(messages, system_prompt: nil)
  result = call_api(messages, system_prompt: system_prompt)
  result
end

#chat_with_tools(messages, tools:, system_prompt: nil) ⇒ Object



11
12
13
# File 'lib/rails_console_ai/providers/anthropic.rb', line 11

def chat_with_tools(messages, tools:, system_prompt: nil)
  call_api(messages, system_prompt: system_prompt, tools: tools)
end

#format_assistant_message(result) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_console_ai/providers/anthropic.rb', line 15

def format_assistant_message(result)
  # Rebuild the assistant content blocks from the raw response
  content_blocks = []
  content_blocks << { 'type' => 'text', 'text' => result.text } if result.text && !result.text.empty?
  (result.tool_calls || []).each do |tc|
    content_blocks << {
      'type' => 'tool_use',
      'id' => tc[:id],
      'name' => tc[:name],
      'input' => tc[:arguments]
    }
  end
  { role: 'assistant', content: content_blocks }
end

#format_tool_result(tool_call_id, result_string) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rails_console_ai/providers/anthropic.rb', line 30

def format_tool_result(tool_call_id, result_string)
  {
    role: 'user',
    content: [
      {
        'type' => 'tool_result',
        'tool_use_id' => tool_call_id,
        'content' => result_string.to_s
      }
    ]
  }
end