Class: LastLLM::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/last_llm/tool.rb

Overview

Tool class for defining callable functions that can be used with LLM providers

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, description:, parameters:, function:) ⇒ Tool

Initialize a new tool

Parameters:

  • name (String)

    The name of the tool

  • description (String)

    A description of what the tool does

  • parameters (Hash)

    JSON Schema for the tool parameters

  • function (Proc)

    The function to execute when the tool is called



16
17
18
19
20
21
22
23
# File 'lib/last_llm/tool.rb', line 16

def initialize(name:, description:, parameters:, function:)
  validate_initialization_params(name, description, parameters, function)

  @name = name
  @description = description
  @parameters = parameters
  @function = function
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



9
10
11
# File 'lib/last_llm/tool.rb', line 9

def description
  @description
end

#functionObject (readonly)

Returns the value of attribute function.



9
10
11
# File 'lib/last_llm/tool.rb', line 9

def function
  @function
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/last_llm/tool.rb', line 9

def name
  @name
end

#parametersObject (readonly)

Returns the value of attribute parameters.



9
10
11
# File 'lib/last_llm/tool.rb', line 9

def parameters
  @parameters
end

Instance Method Details

#call(params) ⇒ Hash

Call the tool with the provided parameters

Parameters:

  • params (Hash)

    Parameters to pass to the tool function

Returns:

  • (Hash)

    The result of the function call



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/last_llm/tool.rb', line 28

def call(params)
  # Convert string keys to symbols
  params = symbolize_keys(params)

  # Validate parameters against the schema
  validate_parameters(params)

  # Convert parameter types if needed
  converted_params = convert_parameter_types(params)

  # Execute the function
  function.call(converted_params)
end