Class: LLM::Tool

Inherits:
Object
  • Object
show all
Extended by:
Param, Patch
Defined in:
lib/llm/shell/internal/llm.rb/lib/llm/tool.rb,
lib/llm/shell/tool.rb,
lib/llm/shell/internal/llm.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, Patch

Class Method Summary collapse

Methods included from Patch

builtin?, disable!, enable!, enabled?

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)


49
50
51
52
53
# File 'lib/llm/shell/internal/llm.rb/lib/llm/tool.rb', line 49

def self.description(desc = nil)
  lock do
    desc ? function.description(desc) : function.description
  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.



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

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

.inherited(klass) ⇒ void

This method returns an undefined value.

Registers the tool as a function when inherited

Parameters:

  • klass (Class)

    The subclass



28
29
30
31
32
33
# File 'lib/llm/shell/internal/llm.rb/lib/llm/tool.rb', line 28

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



75
76
77
# File 'lib/llm/shell/internal/llm.rb/lib/llm/tool.rb', line 75

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

.name(name = nil) ⇒ String

Returns (or sets) the tool name

Parameters:

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

    The tool name

Returns:

  • (String)


39
40
41
42
43
# File 'lib/llm/shell/internal/llm.rb/lib/llm/tool.rb', line 39

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

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

Returns (or sets) tool parameters

Yield Parameters:

  • schema (LLM::Schema)

    The schema object to define parameters

Returns:



59
60
61
62
63
# File 'lib/llm/shell/internal/llm.rb/lib/llm/tool.rb', line 59

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