Module: RubyLLM::Providers::Anthropic::Tools

Included in:
RubyLLM::Providers::Anthropic, Bedrock
Defined in:
lib/ruby_llm/providers/anthropic/tools.rb

Overview

Tools methods of the Anthropic API integration

Class Method Summary collapse

Class Method Details

.default_input_schemaObject



97
98
99
100
101
102
103
104
105
# File 'lib/ruby_llm/providers/anthropic/tools.rb', line 97

def default_input_schema
  {
    'type' => 'object',
    'properties' => {},
    'required' => [],
    'additionalProperties' => false,
    'strict' => true
  }
end

.extract_tool_calls(data) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/ruby_llm/providers/anthropic/tools.rb', line 70

def extract_tool_calls(data)
  if json_delta?(data)
    { nil => ToolCall.new(id: nil, name: nil, arguments: data.dig('delta', 'partial_json')) }
  else
    parse_tool_calls(data['content_block'])
  end
end

.find_tool_uses(blocks) ⇒ Object



10
11
12
# File 'lib/ruby_llm/providers/anthropic/tools.rb', line 10

def find_tool_uses(blocks)
  blocks.select { |c| c['type'] == 'tool_use' }
end

.format_tool_call(msg) ⇒ Object



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

def format_tool_call(msg)
  return { role: 'assistant', content: msg.content.value } if msg.content.is_a?(RubyLLM::Content::Raw)

  content = []

  content << Media.format_text(msg.content) unless msg.content.nil? || msg.content.empty?

  msg.tool_calls.each_value do |tool_call|
    content << format_tool_use_block(tool_call)
  end

  {
    role: 'assistant',
    content:
  }
end

.format_tool_result(msg) ⇒ Object



31
32
33
34
35
36
# File 'lib/ruby_llm/providers/anthropic/tools.rb', line 31

def format_tool_result(msg)
  {
    role: 'user',
    content: msg.content.is_a?(RubyLLM::Content::Raw) ? msg.content.value : [format_tool_result_block(msg)]
  }
end

.format_tool_result_block(msg) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/ruby_llm/providers/anthropic/tools.rb', line 47

def format_tool_result_block(msg)
  {
    type: 'tool_result',
    tool_use_id: msg.tool_call_id,
    content: Media.format_content(msg.content)
  }
end

.format_tool_use_block(tool_call) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/ruby_llm/providers/anthropic/tools.rb', line 38

def format_tool_use_block(tool_call)
  {
    type: 'tool_use',
    id: tool_call.id,
    name: tool_call.name,
    input: tool_call.arguments
  }
end

.function_for(tool) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ruby_llm/providers/anthropic/tools.rb', line 55

def function_for(tool)
  input_schema = tool.params_schema ||
                 RubyLLM::Tool::SchemaDefinition.from_parameters(tool.parameters)&.json_schema

  declaration = {
    name: tool.name,
    description: tool.description,
    input_schema: input_schema || default_input_schema
  }

  return declaration if tool.provider_params.empty?

  RubyLLM::Utils.deep_merge(declaration, tool.provider_params)
end

.parse_tool_calls(content_blocks) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ruby_llm/providers/anthropic/tools.rb', line 78

def parse_tool_calls(content_blocks)
  return nil if content_blocks.nil?

  content_blocks = [content_blocks] unless content_blocks.is_a?(Array)

  tool_calls = {}
  content_blocks.each do |block|
    next unless block && block['type'] == 'tool_use'

    tool_calls[block['id']] = ToolCall.new(
      id: block['id'],
      name: block['name'],
      arguments: block['input']
    )
  end

  tool_calls.empty? ? nil : tool_calls
end