Class: LLM::Tool

Inherits:
Object
  • Object
show all
Extended by:
Param
Defined in:
lib/llm/tool.rb,
lib/llm/tool/param.rb

Overview

The LLM::Tool class represents a local tool that can be called by an LLM. Under the hood, it is a wrapper around LLM::Function but allows the definition of a function (also known as a tool) as a class.

Examples:

class System < LLM::Tool
  name "system"
  description "Runs system commands"
  params do |schema|
    schema.object(command: schema.string.required)
  end

  def call(command:)
    {success: Kernel.system(command)}
  end
end

Defined Under Namespace

Modules: Param

Class Method Summary collapse

Methods included from Param

param

Class Method Details

.description(desc = nil) ⇒ String

Returns (or sets) the tool description

Parameters:

  • desc (String, nil) (defaults to: nil)

    The tool description

Returns:

  • (String)


67
68
69
70
71
# File 'lib/llm/tool.rb', line 67

def self.description(desc = nil)
  lock do
    desc ? function.description(desc) : function.description
  end
end

.inherited(klass) ⇒ void

This method returns an undefined value.

Registers the tool as a function when inherited

Parameters:

  • klass (Class)

    The subclass



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

def self.inherited(klass)
  LLM.lock(:inherited) do
    klass.instance_eval { @__monitor ||= Monitor.new }
    klass.function.register(klass)
    klass.superclass.registry << klass
  end
end

.name(name = nil) ⇒ String

Returns (or sets) the tool name

Parameters:

  • name (String, nil) (defaults to: nil)

    The tool name

Returns:

  • (String)


57
58
59
60
61
# File 'lib/llm/tool.rb', line 57

def self.name(name = nil)
  lock do
    name ? function.name(name) : function.name
  end
end

.registryArray<LLM::Tool>

Returns all registered subclasses of LLM::Tool

Returns:



36
37
38
# File 'lib/llm/tool.rb', line 36

def self.registry
  @registry
end

.params {|schema| ... } ⇒ LLM::Schema

Returns (or sets) tool parameters

Yield Parameters:

  • schema (LLM::Schema)

    The schema object to define parameters

Returns:



77
78
79
80
81
# File 'lib/llm/tool.rb', line 77

def self.params(&)
  lock do
    function.tap { _1.params(&) }
  end
end

.functionObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
89
# File 'lib/llm/tool.rb', line 85

def self.function
  lock do
    @function ||= LLM::Function.new(self)
  end
end

.lockObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



93
94
95
# File 'lib/llm/tool.rb', line 93

def self.lock(&)
  @__monitor.synchronize(&)
end