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:



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

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

Instance Attribute Details

#idString?

Returns the function ID

Returns:

  • (String, nil)


53
54
55
# File 'lib/llm/function.rb', line 53

def id
  @id
end

#argumentsArray?

Returns function arguments

Returns:

  • (Array, nil)


58
59
60
# File 'lib/llm/function.rb', line 58

def arguments
  @arguments
end

#tracerLLM::Tracer?

Returns a tracer, or nil

Returns:



63
64
65
# File 'lib/llm/function.rb', line 63

def tracer
  @tracer
end

#modelString?

Returns a model name, or nil

Returns:

  • (String, nil)


68
69
70
# File 'lib/llm/function.rb', line 68

def model
  @model
end

Instance Method Details

#format_openai(provider) ⇒ Object



188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/llm/function.rb', line 188

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



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

def name(name = nil)
  if name
    @name = name.to_s
  else
    @name
  end
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



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

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

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

This method returns an undefined value.

Set (or get) the function parameters

Yield Parameters:



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/llm/function.rb', line 109

def params
  if block_given?
    if @params
      @params.merge!(yield(@schema))
    else
      @params = yield(@schema)
    end
  else
    @params
  end
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



125
126
127
# File 'lib/llm/function.rb', line 125

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

#callLLM::Function::Return

Call the function

Returns:



133
134
135
136
137
138
# File 'lib/llm/function.rb', line 133

def call
  runner = ((Class === @runner) ? @runner.new : @runner)
  Return.new(id, name, runner.call(**arguments))
ensure
  @called = true
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:



148
149
150
151
152
# File 'lib/llm/function.rb', line 148

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

#called?Boolean

Returns true when a function has been called

Returns:

  • (Boolean)


157
158
159
# File 'lib/llm/function.rb', line 157

def called?
  @called
end

#cancelled?Boolean

Returns true when a function has been cancelled

Returns:

  • (Boolean)


164
165
166
# File 'lib/llm/function.rb', line 164

def cancelled?
  @cancelled
end

#pending?Boolean

Returns true when a function has neither been called nor cancelled

Returns:

  • (Boolean)


171
172
173
# File 'lib/llm/function.rb', line 171

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

#adapt(provider) ⇒ Hash

Returns:

  • (Hash)


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

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