Module: LLM::Tool::Param

Included in:
LLM::Tool
Defined in:
lib/llm/tool/param.rb

Overview

The LLM::Tool::Param module extends the LLM::Tool class with a "param" method that can define a parameter for simple types. For complex types, use LLM::Tool.params instead.

Examples:

class Greeter < LLM::Tool
  name "greeter"
  description "Greets the user"
  parameter :name, String, "The user's name"
  required %i[name]

  def call(name:)
    puts "Hello, #{name}!"
  end
end

Defined Under Namespace

Modules: Utils

Instance Method Summary collapse

Instance Method Details

#param(name, type, description, options = {}) ⇒ Object Also known as: parameter

Parameters:

  • name (Symbol) —

    The name of a parameter

  • type (LLM::Schema::Leaf, Class, Proc) —

    The parameter type (eg String). A proc is called for the type when the parameters are read, so a type that can only be known at runtime does not have to be known when the class is defined.

  • description (String) —

    The description of a property

  • options (Hash) (defaults to: {}) —

    A hash of options for the parameter. Each option is applied to the leaf the type resolves to, so any setting a leaf accepts works here, such as required:, default:, enum:, and the range settings min: and max: that an integer, number, or string leaf supports.

Options Hash (options):

  • :required (Boolean) —

    Whether or not the parameter is required

  • :default (Object) —

    The default value for a given property

  • :enum (Array<String>) —

    One or more possible values for a param



42
43
44
45
46
47
48
49
# File 'lib/llm/tool/param.rb', line 42

def param(name, type, description, options = {})
  lock do
    function.params do |schema|
      resolved = Utils.resolve(schema, type)
      schema.object(name => Utils.setup(resolved, description, options))
    end
  end
end

#required(names) ⇒ LLM::Schema::Object

Mark existing parameters as required.

Parameters:

  • names (Array<Symbol,String>)

Returns:



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

def required(names)
  lock do
    function.params.tap do |schema|
      [*names].each { Utils.fetch(schema.properties, _1).required }
    end
  end
end

#defaults(defaults) ⇒ LLM::Schema::Object

Set default values for parameters.

Parameters:

  • defaults (Hash)

Returns:



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/llm/tool/param.rb', line 68

def defaults(defaults)
  lock do
    function.params.tap do |schema|
      defaults.each do |name, value|
        leaf = Utils.fetch(schema.properties, name)
        leaf.owner = self
        leaf.default(value)
      end
    end
  end
end