Class: RubyPi::LLM::ToolCall

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_pi/llm/tool_call.rb

Overview

A tool call extracted from an LLM response. Contains the unique call ID, the function name, and the parsed arguments hash. Provider-specific formats are normalized into this common structure.

Examples:

Handling a tool call

response.tool_calls.each do |tool_call|
  result = dispatch(tool_call.name, tool_call.arguments)
  # Feed result back into conversation
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, name:, arguments: {}) ⇒ ToolCall

Creates a new ToolCall instance.

Parameters:

  • id (String)

    unique call identifier

  • name (String)

    tool/function name

  • arguments (Hash) (defaults to: {})

    parsed arguments hash



36
37
38
39
40
# File 'lib/ruby_pi/llm/tool_call.rb', line 36

def initialize(id:, name:, arguments: {})
  @id = id
  @name = name
  @arguments = arguments.is_a?(Hash) ? arguments : parse_arguments(arguments)
end

Instance Attribute Details

#argumentsHash (readonly)

Returns the parsed arguments to pass to the tool.

Returns:

  • (Hash)

    the parsed arguments to pass to the tool



29
30
31
# File 'lib/ruby_pi/llm/tool_call.rb', line 29

def arguments
  @arguments
end

#idString (readonly)

Returns unique identifier for this tool call, used to match results back to the calling context.

Returns:

  • (String)

    unique identifier for this tool call, used to match results back to the calling context



23
24
25
# File 'lib/ruby_pi/llm/tool_call.rb', line 23

def id
  @id
end

#nameString (readonly)

Returns the name of the tool/function to invoke.

Returns:

  • (String)

    the name of the tool/function to invoke



26
27
28
# File 'lib/ruby_pi/llm/tool_call.rb', line 26

def name
  @name
end

Instance Method Details

#to_hHash

Returns a hash representation of the tool call for serialization.

Returns:

  • (Hash)


45
46
47
48
49
50
51
# File 'lib/ruby_pi/llm/tool_call.rb', line 45

def to_h
  {
    id: @id,
    name: @name,
    arguments: @arguments
  }
end

#to_sString Also known as: inspect

Returns a human-readable string representation of the tool call.

Returns:

  • (String)


56
57
58
# File 'lib/ruby_pi/llm/tool_call.rb', line 56

def to_s
  "#<RubyPi::LLM::ToolCall id=#{@id.inspect} name=#{@name.inspect} arguments=#{@arguments.inspect}>"
end