Class: LLM::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/tool.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

Class Method Summary collapse

Class Method Details

.inherited(klass) ⇒ void

This method returns an undefined value.

Registers the tool as a function when inherited

Parameters:

  • klass (Class)

    The subclass



25
26
27
28
29
30
# File 'lib/llm/tool.rb', line 25

def self.inherited(klass)
  LLM.lock(:inherited) do
    klass.instance_eval { @__monitor ||= Monitor.new }
    klass.function.register(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)


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

def self.name(name = nil)
  lock do
    function.tap { _1.name(name) }
  end
end

.description(desc = nil) ⇒ String

Returns (or sets) the tool description

Parameters:

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

    The tool description

Returns:

  • (String)


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

def self.description(desc = nil)
  lock do
    function.tap { _1.description(desc) }
  end
end

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

Returns (or sets) tool parameters

Yield Parameters:

  • schema (LLM::Schema)

    The schema object to define parameters

Returns:



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

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.



64
65
66
67
68
# File 'lib/llm/tool.rb', line 64

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.



72
73
74
# File 'lib/llm/tool.rb', line 72

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