Class: LLM::Function
- Inherits:
-
Object
- Object
- LLM::Function
- Includes:
- Tracing
- Defined in:
- lib/llm/function.rb,
lib/llm/function/array.rb,
lib/llm/function/tracing.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, Tracing Classes: FiberGroup, Return, TaskGroup, ThreadGroup
Instance Attribute Summary collapse
-
#id ⇒
String?
Returns the function ID.
-
#arguments ⇒
Array?
Returns function arguments.
-
#tracer ⇒
LLM::Tracer?
Returns a tracer, or nil.
-
#model ⇒
String?
Returns a model name, or nil.
Instance Method Summary collapse
- #adapt(provider) ⇒ Hash
-
#name(name = nil) ⇒
void
Set (or get) the function name.
-
#description(desc =
nil) ⇒ void
Set (or get) the function description.
-
#params {|schema|
... } ⇒ LLM::Schema::Leaf?
Set (or get) the function parameters.
-
#define(klass =
nil, &b) ⇒ void (also: #register)
Set the function implementation.
-
#call ⇒
LLM::Function::Return
Call the function.
-
#spawn(strategy) ⇒
Thread, ...
Calls the function in a separate thread.
-
#cancel(reason:
"function call cancelled") ⇒ LLM::Function::Return
Returns a value that communicates that the function call was cancelled.
-
#called? ⇒
Boolean
Returns true when a function has been called.
-
#cancelled? ⇒
Boolean
Returns true when a function has been cancelled.
-
#pending? ⇒
Boolean
Returns true when a function has neither been called nor cancelled.
-
#initialize(name)
{|self| ... } ⇒ Function constructor
A new instance of Function.
Constructor Details
Instance Attribute Details
#id ⇒ String?
Returns the function ID
58 59 60 |
# File 'lib/llm/function.rb', line 58 def id @id end |
#arguments ⇒ Array?
Returns function arguments
63 64 65 |
# File 'lib/llm/function.rb', line 63 def arguments @arguments end |
#tracer ⇒ LLM::Tracer?
Returns a tracer, or nil
68 69 70 |
# File 'lib/llm/function.rb', line 68 def tracer @tracer end |
#model ⇒ String?
Returns a model name, or nil
73 74 75 |
# File 'lib/llm/function.rb', line 73 def model @model end |
Instance Method Details
#adapt(provider) ⇒ Hash
227 228 229 230 231 232 233 234 235 236 |
# File 'lib/llm/function.rb', line 227 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}.compact else format_openai(provider) end end |
#name(name = nil) ⇒ void
This method returns an undefined value.
Set (or get) the function name
90 91 92 93 94 95 96 |
# File 'lib/llm/function.rb', line 90 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
102 103 104 105 106 107 108 |
# File 'lib/llm/function.rb', line 102 def description(desc = nil) if desc @description = desc else @description end end |
#params {|schema| ... } ⇒ LLM::Schema::Leaf?
Set (or get) the function parameters
114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/llm/function.rb', line 114 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 |
#define(klass = nil, &b) ⇒ void Also known as: register
This method returns an undefined value.
Set the function implementation
132 133 134 |
# File 'lib/llm/function.rb', line 132 def define(klass = nil, &b) @runner = klass || b end |
#call ⇒ LLM::Function::Return
Call the function
140 141 142 143 144 |
# File 'lib/llm/function.rb', line 140 def call call_function ensure @called = true end |
#spawn(strategy) ⇒ Thread, ...
Calls the function in a separate thread.
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.
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/llm/function.rb', line 170 def spawn(strategy) 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 ensure @called = true end |
#cancel(reason: "function call cancelled") ⇒ LLM::Function::Return
Returns a value that communicates that the function call was cancelled
198 199 200 201 202 |
# File 'lib/llm/function.rb', line 198 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
207 208 209 |
# File 'lib/llm/function.rb', line 207 def called? @called end |
#cancelled? ⇒ Boolean
Returns true when a function has been cancelled
214 215 216 |
# File 'lib/llm/function.rb', line 214 def cancelled? @cancelled end |
#pending? ⇒ Boolean
Returns true when a function has neither been called nor cancelled
221 222 223 |
# File 'lib/llm/function.rb', line 221 def pending? !@called && !@cancelled end |