Class: LLM::Function
- Inherits:
-
Object
- Object
- LLM::Function
- Extended by:
- Registry
- Includes:
- Tracing
- Defined in:
- lib/llm/function.rb,
lib/llm/function/task.rb,
lib/llm/function/array.rb,
lib/llm/function/tracing.rb,
lib/llm/function/registry.rb,
lib/llm/function/task_group.rb,
lib/llm/function/fiber_group.rb,
lib/llm/function/thread_group.rb
Overview
The LLM::Function class represents a local function that can be called by an LLM.
Defined Under Namespace
Modules: Array, Registry, Tracing Classes: 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.
-
#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.
-
#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
73 74 75 |
# File 'lib/llm/function.rb', line 73 def arguments @arguments end |
#id ⇒ String?
Returns the function ID
68 69 70 |
# File 'lib/llm/function.rb', line 68 def id @id end |
#model ⇒ String?
Returns a model name, or nil
83 84 85 |
# File 'lib/llm/function.rb', line 83 def model @model end |
#tracer ⇒ LLM::Tracer?
Returns a tracer, or nil
78 79 80 |
# File 'lib/llm/function.rb', line 78 def tracer @tracer end |
Instance Method Details
#adapt(provider) ⇒ Hash
238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/llm/function.rb', line 238 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
150 151 152 153 154 |
# File 'lib/llm/function.rb', line 150 def call call_function ensure @called = true end |
#called? ⇒ Boolean
Returns true when a function has been called
218 219 220 |
# File 'lib/llm/function.rb', line 218 def called? @called end |
#cancel(reason: "function call cancelled") ⇒ LLM::Function::Return
Returns a value that communicates that the function call was cancelled
209 210 211 212 213 |
# File 'lib/llm/function.rb', line 209 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
225 226 227 |
# File 'lib/llm/function.rb', line 225 def cancelled? @cancelled end |
#define(klass = nil, &b) ⇒ void Also known as: register
This method returns an undefined value.
Set the function implementation
142 143 144 |
# File 'lib/llm/function.rb', line 142 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
112 113 114 115 116 117 118 |
# File 'lib/llm/function.rb', line 112 def description(desc = nil) if desc @description = desc else @description end end |
#name(name = nil) ⇒ void
This method returns an undefined value.
Set (or get) the function name
100 101 102 103 104 105 106 |
# File 'lib/llm/function.rb', line 100 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
124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/llm/function.rb', line 124 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
232 233 234 |
# File 'lib/llm/function.rb', line 232 def pending? !@called && !@cancelled 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.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/llm/function.rb', line 180 def spawn(strategy) task = case strategy when :task require "async" unless defined?(::Async) Async { call_function } when :thread Thread.new { call_function } when :fiber Fiber.new do call_function ensure Fiber.yield end.tap(&:resume) else raise ArgumentError, "Unknown strategy: #{strategy.inspect}. Expected :thread, :task, or :fiber" end Task.new(task, self) ensure @called = true end |