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/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/call_group.rb,
lib/llm/function/fork_group.rb,
lib/llm/function/ractor/job.rb,
lib/llm/function/task_group.rb,
lib/llm/function/fiber_group.rb,
lib/llm/function/ractor/task.rb,
lib/llm/function/ractor_group.rb,
lib/llm/function/thread_group.rb,
lib/llm/function/ractor/mailbox.rb
Overview
The LLM::Function class represents a local function that can be called by an LLM.
Defined Under Namespace
Modules: Array, Fork, Ractor, Registry, Tracing Classes: CallGroup, FiberGroup, Return, Task, TaskGroup, ThreadGroup
Instance Attribute Summary collapse
-
#arguments ⇒ Array?
Returns function arguments.
-
#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
- #adapt(provider) ⇒ Hash
-
#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: #register)
Set the function implementation.
-
#description(desc = nil) ⇒ void
Set (or get) the function description.
-
#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.
-
#runner ⇒ Object
Returns the bound function runner instance.
-
#spawn(strategy) ⇒ LLM::Function::Task
Calls the function concurrently.
Methods included from Registry
clear_registry!, extended, find_by_name, lock, registry, registry_key, tool_name, unregister
Constructor Details
Instance Attribute Details
#arguments ⇒ Array?
Returns function arguments
112 113 114 |
# File 'lib/llm/function.rb', line 112 def arguments @arguments end |
#id ⇒ String?
Returns the function ID
107 108 109 |
# File 'lib/llm/function.rb', line 107 def id @id end |
#model ⇒ String?
Returns a model name, or nil
122 123 124 |
# File 'lib/llm/function.rb', line 122 def model @model end |
#tracer ⇒ LLM::Tracer?
Returns a tracer, or nil
117 118 119 |
# File 'lib/llm/function.rb', line 117 def tracer @tracer end |
Instance Method Details
#adapt(provider) ⇒ Hash
300 301 302 303 304 305 306 307 308 309 310 311 312 313 |
# File 'lib/llm/function.rb', line 300 def adapt(provider) case provider.class.to_s when "LLM::Google" {name: @name, description: @description, parameters: @params}.compact when "LLM::Anthropic" { name: @name, description: @description, input_schema: @params || {type: "object", properties: {}} }.compact else format_openai(provider) end end |
#call ⇒ LLM::Function::Return
Call the function
189 190 191 192 193 |
# File 'lib/llm/function.rb', line 189 def call call_function ensure @called = true end |
#called? ⇒ Boolean
Returns true when a function has been called
280 281 282 |
# File 'lib/llm/function.rb', line 280 def called? @called end |
#cancel(reason: "function call cancelled") ⇒ LLM::Function::Return
Returns a value that communicates that the function call was cancelled
259 260 261 262 263 |
# File 'lib/llm/function.rb', line 259 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
287 288 289 |
# File 'lib/llm/function.rb', line 287 def cancelled? @cancelled end |
#define(klass = nil, &b) ⇒ void Also known as: register
This method returns an undefined value.
Set the function implementation
181 182 183 |
# File 'lib/llm/function.rb', line 181 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
151 152 153 154 155 156 157 |
# File 'lib/llm/function.rb', line 151 def description(desc = nil) if desc @description = desc else @description end 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`.
270 271 272 273 274 |
# File 'lib/llm/function.rb', line 270 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
139 140 141 142 143 144 145 |
# File 'lib/llm/function.rb', line 139 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
163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/llm/function.rb', line 163 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 end end |
#pending? ⇒ Boolean
Returns true when a function has neither been called nor cancelled
294 295 296 |
# File 'lib/llm/function.rb', line 294 def pending? !@called && !@cancelled end |
#runner ⇒ Object
Returns the bound function runner instance.
318 319 320 321 322 |
# File 'lib/llm/function.rb', line 318 def runner runner = Class === @runner ? @runner.new : @runner runner.tracer = @tracer if runner.respond_to?(:tracer=) runner end |
#spawn(strategy) ⇒ LLM::Function::Task
Calls the function concurrently.
This is the low-level method that powers concurrent tool execution. Prefer the collection methods on Context#functions for most use cases: LLM::Function::Array#call, LLM::Function::Array#wait, or LLM::Function::Array#spawn.
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/llm/function.rb', line 222 def spawn(strategy) task = case strategy when :task LLM.require "async" unless defined?(::Async) Async { call! } when :thread Thread.new { call! } when :fiber raise ArgumentError, "Fiber concurrency requires Fiber.scheduler" unless Fiber.scheduler Fiber.schedule { call! } when :fork LLM.require "xchan" unless defined?(::Chan::UNIXSocket) span = @tracer&.on_tool_start(id:, name:, arguments:, model:) Fork::Task.new(self, tracer: @tracer, span:).spawn 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 span = @tracer&.on_tool_start(id:, name:, arguments:, model:) Ractor::Task.new(@runner, id, name, arguments, tracer: @tracer, span:).spawn else raise ArgumentError, "Unknown strategy: #{strategy.inspect}. Expected :thread, :task, :fiber, :fork, or :ractor" end Task.new(task, self) ensure @called = true end |