Class: RCrewAI::LLMClients::Anthropic

Inherits:
Base
  • Object
show all
Defined in:
lib/rcrewai/llm_clients/anthropic.rb

Constant Summary collapse

BASE_URL =
'https://api.anthropic.com/v1'
API_VERSION =
'2023-06-01'
STOP_REASON_MAP =
{
  'tool_use' => :tool_calls,
  'end_turn' => :stop,
  'stop_sequence' => :stop,
  'max_tokens' => :length
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#config, #logger

Instance Method Summary collapse

Methods inherited from Base

#complete

Constructor Details

#initialize(config = RCrewAI.configuration) ⇒ Anthropic

Returns a new instance of Anthropic.



24
25
26
27
# File 'lib/rcrewai/llm_clients/anthropic.rb', line 24

def initialize(config = RCrewAI.configuration)
  super
  @base_url = BASE_URL
end

Instance Method Details

#chat(messages:, tools: nil, tool_choice: :auto, stream: nil, **options) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rcrewai/llm_clients/anthropic.rb', line 29

def chat(messages:, tools: nil, tool_choice: :auto, stream: nil, **options)
  system_message = extract_system_message(messages)
  non_system = messages.reject { |m| m.is_a?(Hash) && m[:role] == 'system' }

  payload = {
    model: config.model,
    messages: format_messages(non_system),
    max_tokens: options[:max_tokens] || config.max_tokens || 1000,
    temperature: options[:temperature] || config.temperature
  }.compact

  if system_message
    payload[:system] = if options[:cache_system]
                         [{ type: 'text', text: system_message,
                            cache_control: { type: 'ephemeral' } }]
                       else
                         system_message
                       end
  end

  if tools && !tools.empty?
    payload[:tools] = ProviderSchema.for_many(:anthropic, tools)
    payload[:tool_choice] = { type: tool_choice.to_s } if tool_choice != :auto && tool_choice.is_a?(Symbol)
  end

  payload[:top_p] = options[:top_p] if options[:top_p]
  payload[:top_k] = options[:top_k] if options[:top_k]
  payload[:stop_sequences] = options[:stop_sequences] if options[:stop_sequences]

  if stream
    payload[:stream] = true
    stream_chat(payload, stream)
  else
    plain_chat(payload)
  end
end

#modelsObject



70
71
72
73
74
75
# File 'lib/rcrewai/llm_clients/anthropic.rb', line 70

def models
  %w[
    claude-opus-4-7 claude-sonnet-4-6 claude-haiku-4-5
    claude-3-5-sonnet-20241022 claude-3-haiku-20240307
  ]
end

#supports_native_tools?(model: config.model) ⇒ Boolean

rubocop:disable Lint/UnusedMethodArgument

Returns:

  • (Boolean)


66
67
68
# File 'lib/rcrewai/llm_clients/anthropic.rb', line 66

def supports_native_tools?(model: config.model) # rubocop:disable Lint/UnusedMethodArgument
  true
end