Module: RubyLLM::Protocols::Interactions::Tools

Defined in:
lib/ruby_llm/protocols/interactions/tools.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.parse_interaction_arguments(arguments) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/ruby_llm/protocols/interactions/tools.rb', line 33

def parse_interaction_arguments(arguments)
  return {} if arguments.nil? || (arguments.is_a?(String) && arguments.empty?)

  arguments.is_a?(String) ? JSON.parse(arguments) : arguments
rescue JSON::ParserError => e
  raise ToolCallParseError.new(finish_reason: :tool_calls), cause: e
end

.parse_interaction_calls(steps) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/ruby_llm/protocols/interactions/tools.rb', line 25

def parse_interaction_calls(steps)
  steps.select { |step| step['type'] == 'function_call' }.to_h do |step|
    [step.fetch('id'), ToolCall.new(id: step.fetch('id'), name: step.fetch('name'),
                                    arguments: parse_interaction_arguments(step['arguments']),
                                    thought_signature: step['signature'])]
  end
end

.parse_interaction_server_calls(steps) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/ruby_llm/protocols/interactions/tools.rb', line 41

def parse_interaction_server_calls(steps)
  steps.filter_map do |step|
    type = step['type'].to_s
    next unless type.end_with?('_call', '_result') && !type.start_with?('function_')

    ServerToolCall.new(type: type, id: step['id'] || step['call_id'], name: step['name'],
                       input: step['arguments'], result: step['result'], raw: step)
  end
end

.render_interaction_choice(choice) ⇒ Object



18
19
20
21
22
23
# File 'lib/ruby_llm/protocols/interactions/tools.rb', line 18

def render_interaction_choice(choice)
  return 'any' if choice == :required
  return choice.to_s if %i[auto none].include?(choice)

  { allowed_tools: { mode: 'any', tools: [choice.to_s] } }
end

.render_interaction_tools(tools) ⇒ Object



9
10
11
12
13
14
15
16
# File 'lib/ruby_llm/protocols/interactions/tools.rb', line 9

def render_interaction_tools(tools)
  tools.values.map do |tool|
    parameters = tool.parameters_schema ||
                 Tool::SchemaDefinition.from_parameters(tool.declared_parameters)&.json_schema
    Support::Utils.deep_merge({ type: 'function', name: tool.name, description: tool.description,
                                parameters: parameters }.compact, tool.provider_options)
  end
end