Class: LLM::Function

Inherits:
Object
  • Object
show all
Includes:
Tracing
Defined in:
lib/llm/function.rb,
lib/llm/function/tracing.rb

Overview

The LLM::Function class represents a local function that can be called by an LLM.

Examples:

example #1

LLM.function(:system) do |fn|
  fn.name "system"
  fn.description "Runs system commands"
  fn.params do |schema|
    schema.object(command: schema.string.required)
  end
  fn.define do |command:|
    {success: Kernel.system(command)}
  end
end

example #2

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: Tracing Classes: Return

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) {|self| ... } ⇒ Function

Returns a new instance of Function.

Parameters:

  • name (String)

    The function name

Yield Parameters:



61
62
63
64
65
66
67
# File 'lib/llm/function.rb', line 61

def initialize(name, &b)
  @name = name
  @schema = LLM::Schema.new
  @called = false
  @cancelled = false
  yield(self) if block_given?
end

Instance Attribute Details

#argumentsArray?

Returns function arguments

Returns:

  • (Array, nil)


46
47
48
# File 'lib/llm/function.rb', line 46

def arguments
  @arguments
end

#idString?

Returns the function ID

Returns:

  • (String, nil)


41
42
43
# File 'lib/llm/function.rb', line 41

def id
  @id
end

#modelString?

Returns a model name, or nil

Returns:

  • (String, nil)


56
57
58
# File 'lib/llm/function.rb', line 56

def model
  @model
end

#tracerLLM::Tracer?

Returns a tracer, or nil

Returns:



51
52
53
# File 'lib/llm/function.rb', line 51

def tracer
  @tracer
end

Instance Method Details

#adapt(provider) ⇒ Hash

Returns:

  • (Hash)


165
166
167
168
169
170
171
172
173
174
# File 'lib/llm/function.rb', line 165

def adapt(provider)
  case provider.class.to_s
  when "LLM::Gemini"
    {name: @name, description: @description, parameters: @params}.compact
  when "LLM::Anthropic"
    {name: @name, description: @description, input_schema: @params}.compact
  else
    format_openai(provider)
  end
end

#callLLM::Function::Return

Call the function

Returns:



121
122
123
124
125
126
# File 'lib/llm/function.rb', line 121

def call
  runner = ((Class === @runner) ? @runner.new : @runner)
  Return.new(id, name, runner.call(**arguments))
ensure
  @called = true
end

#called?Boolean

Returns true when a function has been called

Returns:

  • (Boolean)


145
146
147
# File 'lib/llm/function.rb', line 145

def called?
  @called
end

#cancel(reason: "function call cancelled") ⇒ LLM::Function::Return

Returns a value that communicates that the function call was cancelled

Examples:

llm = LLM.openai(key: ENV["KEY"])
ses = LLM::Session.new(llm, tools: [fn1, fn2])
ses.talk "I want to run the functions"
ses.talk ses.functions.map(&:cancel)

Returns:



136
137
138
139
140
# File 'lib/llm/function.rb', line 136

def cancel(reason: "function call cancelled")
  Return.new(id, name, {cancelled: true, reason:})
ensure
  @cancelled = true
end

#cancelled?Boolean

Returns true when a function has been cancelled

Returns:

  • (Boolean)


152
153
154
# File 'lib/llm/function.rb', line 152

def cancelled?
  @cancelled
end

#define(klass = nil, &b) ⇒ void Also known as: register

This method returns an undefined value.

Set the function implementation

Parameters:

  • b (Proc, Class)

    The function implementation



113
114
115
# File 'lib/llm/function.rb', line 113

def define(klass = nil, &b)
  @runner = klass || b
end

#description(desc = nil) ⇒ void

This method returns an undefined value.

Set (or get) the function description

Parameters:

  • desc (String) (defaults to: nil)

    The function description



85
86
87
88
89
90
91
# File 'lib/llm/function.rb', line 85

def description(desc = nil)
  if desc
    @description = desc
  else
    @description
  end
end

#format_openai(provider) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/llm/function.rb', line 176

def format_openai(provider)
  case provider.class.to_s
  when "LLM::OpenAI::Responses"
    {
      type: "function", name: @name, description: @description,
      parameters: @params.to_h.merge(additionalProperties: false), strict: true
    }.compact
  else
    {
      type: "function", name: @name,
      function: {name: @name, description: @description, parameters: @params}
    }.compact
  end
end

#name(name = nil) ⇒ void

This method returns an undefined value.

Set (or get) the function name

Parameters:

  • name (String) (defaults to: nil)

    The function name



73
74
75
76
77
78
79
# File 'lib/llm/function.rb', line 73

def name(name = nil)
  if name
    @name = name.to_s
  else
    @name
  end
end

#params {|schema| ... } ⇒ void

This method returns an undefined value.

Set (or get) the function parameters

Yield Parameters:



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/llm/function.rb', line 97

def params
  if block_given?
    if @params
      @params.merge!(yield(@schema))
    else
      @params = yield(@schema)
    end
  else
    @params
  end
end

#pending?Boolean

Returns true when a function has neither been called nor cancelled

Returns:

  • (Boolean)


159
160
161
# File 'lib/llm/function.rb', line 159

def pending?
  !@called && !@cancelled
end