Class: LLM::Function
- Inherits:
-
Object
- Object
- LLM::Function
- Extended by:
- Registry
- Includes:
- Tracing
- Defined in:
- lib/llm/function.rb,
lib/llm/function/fork.rb,
lib/llm/function/task.rb,
lib/llm/function/array.rb,
lib/llm/function/group.rb,
lib/llm/function/ractor.rb,
lib/llm/function/tracing.rb,
lib/llm/function/fork/job.rb,
lib/llm/function/registry.rb,
lib/llm/function/fork/task.rb,
lib/llm/function/ractor/job.rb,
lib/llm/function/ractor/task.rb,
lib/llm/function/ractor/mailbox.rb
Overview
The LLM::Function class represents a local function that can be called by an LLM. Most users should define tools as subclasses of Tool instead — Function is the lower-level building block that Tool wraps.
Defined Under Namespace
Modules: Array, Async, Fiber, Fork, Ractor, Registry, Sequential, Thread, Tracing Classes: Group, Return, Task
Instance Attribute Summary collapse
-
#arguments ⇒ Hash, ...
Returns function arguments.
-
#guard ⇒ Class<LLM::Guard>?
Returns the guard class that protects this function, or nil.
-
#id ⇒ String?
Returns the function ID.
-
#model ⇒ String?
Returns a model name, or nil.
-
#tracer ⇒ LLM::Tracer?
Returns a tracer, or nil.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
(also: #eql?)
Compares functions by tool call ID when both sides have one.
- #adapt(provider) ⇒ Hash
-
#budget_spent ⇒ LLM::Function::Return
Returns an in-band error that indicates the tool call budget has been spent.
-
#call ⇒ LLM::Function::Return
Call the function.
-
#called? ⇒ Boolean
Returns true when a function has been called.
-
#cancel(reason: "function call cancelled") ⇒ LLM::Function::Return
Returns a value that communicates that the function call was cancelled.
-
#cancelled? ⇒ Boolean
Returns true when a function has been cancelled.
-
#define(klass = nil, &b) ⇒ void
(also: #def)
Set the function implementation.
-
#description(desc = nil) ⇒ void
Set (or get) the function description.
-
#hash ⇒ Integer
Returns a hash value compatible with #==.
-
#initialize(name) {|self| ... } ⇒ Function
constructor
A new instance of Function.
-
#interrupt! ⇒ nil
(also: #cancel!)
Notifies the function runner that the call was interrupted.
-
#name(name = nil) ⇒ void
Set (or get) the function name.
-
#params {|schema| ... } ⇒ LLM::Schema::Leaf?
Set (or get) the function parameters.
-
#pending? ⇒ Boolean
Returns true when a function has neither been called nor cancelled.
-
#return ⇒ LLM::Function::Return
Builds an LLM::Function::Return for this function, using its own id and name.
-
#runner ⇒ Object
Returns the bound function runner instance.
-
#skill? ⇒ Boolean
Returns true when this function is backed by a skill tool.
-
#task(strategy, options = {}) ⇒ LLM::Function::Task
Returns a function as a LLM::Function::Task.
-
#unavailable ⇒ LLM::Function::Return
Returns an in-band error for an unresolved function call.
Methods included from Registry
clear_registry!, extended, find_by_name, lock, register, registry, registry_key, tool_name, unregister
Constructor Details
Instance Attribute Details
#arguments ⇒ Hash, ...
Returns function arguments
119 120 121 |
# File 'lib/llm/function.rb', line 119 def arguments @arguments end |
#guard ⇒ Class<LLM::Guard>?
Returns the guard class that protects this function, or nil. The context stamps the guard onto the functions it binds, so any task built from this function checks it before the tool runs.
163 164 165 |
# File 'lib/llm/function.rb', line 163 def guard @guard end |
#id ⇒ String?
Returns the function ID
114 115 116 |
# File 'lib/llm/function.rb', line 114 def id @id end |
#model ⇒ String?
Returns a model name, or nil
156 157 158 |
# File 'lib/llm/function.rb', line 156 def model @model end |
#tracer ⇒ LLM::Tracer?
Returns a tracer, or nil
151 152 153 |
# File 'lib/llm/function.rb', line 151 def tracer @tracer end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
Compares functions by tool call ID when both sides have one.
133 134 135 136 137 138 |
# File 'lib/llm/function.rb', line 133 def ==(other) return true if equal?(other) return false unless self.class === other return false unless id && other.id id == other.id end |
#adapt(provider) ⇒ Hash
383 384 385 |
# File 'lib/llm/function.rb', line 383 def adapt(provider) provider.adapt_function(self) end |
#budget_spent ⇒ LLM::Function::Return
Returns an in-band error that indicates the tool call budget has been spent.
358 359 360 361 362 363 364 365 |
# File 'lib/llm/function.rb', line 358 def budget_spent LLM::Function::Return.new(id, name, { error: true, type: "LLM::BudgetSpentError", message: "the tool call budget for this turn has been spent. " \ "try to solve the problem with less tool calls." }) end |
#call ⇒ LLM::Function::Return
Call the function
230 231 232 233 234 235 |
# File 'lib/llm/function.rb', line 230 def call llm = @tracer&.llm llm ? llm.with_tracer(@tracer) { call_function } : call_function ensure @called = true end |
#called? ⇒ Boolean
Returns true when a function has been called
318 319 320 |
# File 'lib/llm/function.rb', line 318 def called? @called end |
#cancel(reason: "function call cancelled") ⇒ LLM::Function::Return
Returns a value that communicates that the function call was cancelled
297 298 299 300 301 |
# File 'lib/llm/function.rb', line 297 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
325 326 327 |
# File 'lib/llm/function.rb', line 325 def cancelled? @cancelled end |
#define(klass = nil, &b) ⇒ void Also known as: def
This method returns an undefined value.
Set the function implementation
222 223 224 |
# File 'lib/llm/function.rb', line 222 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
192 193 194 195 196 197 198 |
# File 'lib/llm/function.rb', line 192 def description(desc = nil) if desc @description = desc else @description end end |
#hash ⇒ Integer
Returns a hash value compatible with #==.
144 145 146 |
# File 'lib/llm/function.rb', line 144 def hash id ? id.hash : object_id.hash end |
#interrupt! ⇒ nil Also known as: cancel!
Notifies the function runner that the call was interrupted.
This is cooperative and only applies to runners that implement
on_interrupt.
308 309 310 311 312 |
# File 'lib/llm/function.rb', line 308 def interrupt! hook = %i[on_cancel on_interrupt].find { @runner.respond_to?(_1) } @runner.public_send(hook) if hook nil end |
#name(name = nil) ⇒ void
This method returns an undefined value.
Set (or get) the function name
180 181 182 183 184 185 186 |
# File 'lib/llm/function.rb', line 180 def name(name = nil) if name @name = name.to_s else @name end end |
#params {|schema| ... } ⇒ LLM::Schema::Leaf?
Set (or get) the function parameters
204 205 206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/llm/function.rb', line 204 def params if block_given? params = yield(@schema) params = LLM::Schema.parse(params) if Hash === params if @params @params.merge!(params) else @params = params end else @params || LLM::Schema::Object.new({}) end end |
#pending? ⇒ Boolean
Returns true when a function has neither been called nor cancelled
339 340 341 |
# File 'lib/llm/function.rb', line 339 def pending? !@called && !@cancelled end |
#return ⇒ LLM::Function::Return
return is a Ruby keyword, so this is defined via
Kernel#define_method.
Builds an LLM::Function::Return for this function, using its own id and name. The given keywords become the return's value.
377 378 379 |
# File 'lib/llm/function.rb', line 377 define_method(:return) do |value| Return.new(id, name, value) end |
#runner ⇒ Object
Returns the bound function runner instance.
390 391 392 393 394 |
# File 'lib/llm/function.rb', line 390 def runner runner = Class === @runner ? @runner.new : @runner runner.tracer = @tracer if runner.respond_to?(:tracer=) runner end |
#skill? ⇒ Boolean
Returns true when this function is backed by a skill tool.
332 333 334 |
# File 'lib/llm/function.rb', line 332 def skill? @runner.respond_to?(:skill?) and @runner.skill? end |
#task(strategy, options = {}) ⇒ LLM::Function::Task
Returns a function as a LLM::Function::Task.
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/llm/function.rb', line 259 def task(strategy, = {}) ## # Check the function's guard on the calling thread before handing # the tool to the strategy. The task carries the blocked result and # returns it without running if the guard intervenes. = .merge(guarded: @guard&.call(function: self)) case strategy when :sequential Sequential::Task.new(self, ) when :async LLM.require "async" unless defined?(::Async) Async::Task.new(self, ) when :thread Thread::Task.new(self, ) when :fiber Fiber::Task.new(self, ) when :fork LLM.require "xchan", "~> 0.22" unless defined?(::Chan::UNIXSocket) Fork::Task.new(self, .merge(tracer: @tracer)) when :ractor raise LLM::RactorError, "Ractor concurrency only supports class-based tools" unless Class === @runner if @runner.respond_to?(:skill?) && @runner.skill? raise LLM::RactorError, "Ractor concurrency does not support skill-backed tools" end Ractor::Task.new(self, .merge(runner_class: @runner, id:, name:, arguments:, tracer: @tracer, model:)) else raise ArgumentError, "Unknown strategy: #{strategy.inspect}. Expected :sequential, :thread, :fiber, :async, :fork, or :ractor" end end |
#unavailable ⇒ LLM::Function::Return
Returns an in-band error for an unresolved function call.
346 347 348 349 350 351 352 |
# File 'lib/llm/function.rb', line 346 def unavailable Return.new(id, name, { error: true, type: LLM::NoSuchToolError.name, message: "tool not found" }) end |