Class: LLM::Function
- Inherits:
-
Object
- Object
- LLM::Function
- 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.
Defined Under Namespace
Modules: Tracing Classes: Return
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
- #format_openai(provider) ⇒ Object
-
#name(name = nil) ⇒
void
Set (or get) the function name.
-
#description(desc =
nil) ⇒ void
Set (or get) the function description.
-
#params {|schema|
... } ⇒ void
Set (or get) the function parameters.
-
#define(klass =
nil, &b) ⇒ void (also: #register)
Set the function implementation.
-
#call ⇒
LLM::Function::Return
Call the function.
-
#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.
- #adapt(provider) ⇒ Hash
-
#initialize(name)
{|self| ... } ⇒ Function constructor
A new instance of Function.
Constructor Details
Instance Attribute Details
#id ⇒ String?
Returns the function ID
53 54 55 |
# File 'lib/llm/function.rb', line 53 def id @id end |
#arguments ⇒ Array?
Returns function arguments
58 59 60 |
# File 'lib/llm/function.rb', line 58 def arguments @arguments end |
#tracer ⇒ LLM::Tracer?
Returns a tracer, or nil
63 64 65 |
# File 'lib/llm/function.rb', line 63 def tracer @tracer end |
#model ⇒ String?
Returns a model name, or 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
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
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
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
125 126 127 |
# File 'lib/llm/function.rb', line 125 def define(klass = nil, &b) @runner = klass || b end |
#call ⇒ LLM::Function::Return
Call the function
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
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
157 158 159 |
# File 'lib/llm/function.rb', line 157 def called? @called end |
#cancelled? ⇒ Boolean
Returns true when a function has been cancelled
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
171 172 173 |
# File 'lib/llm/function.rb', line 171 def pending? !@called && !@cancelled end |
#adapt(provider) ⇒ 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 |