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
-
#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.
- #format_openai(provider) ⇒ Object
-
#initialize(name) {|self| ... } ⇒ Function
constructor
A new instance of Function.
-
#name(name = nil) ⇒ void
Set (or get) the function name.
-
#params {|schema| ... } ⇒ void
Set (or get) the function parameters.
-
#pending? ⇒ Boolean
Returns true when a function has neither been called nor cancelled.
Constructor Details
Instance Attribute Details
#arguments ⇒ Array?
Returns function arguments
46 47 48 |
# File 'lib/llm/function.rb', line 46 def arguments @arguments end |
#id ⇒ String?
Returns the function ID
41 42 43 |
# File 'lib/llm/function.rb', line 41 def id @id end |
#model ⇒ String?
Returns a model name, or nil
56 57 58 |
# File 'lib/llm/function.rb', line 56 def model @model end |
#tracer ⇒ LLM::Tracer?
Returns a tracer, or nil
51 52 53 |
# File 'lib/llm/function.rb', line 51 def tracer @tracer end |
Instance Method Details
#adapt(provider) ⇒ Hash
165 166 167 168 169 170 171 172 173 174 |
# File 'lib/llm/function.rb', line 165 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 |
#call ⇒ LLM::Function::Return
Call the function
121 122 123 124 125 126 |
# File 'lib/llm/function.rb', line 121 def call runner = ((Class === @runner) ? @runner.new : @runner) Return.new(id, name, runner.call(**arguments)) ensure @called = true end |
#called? ⇒ Boolean
Returns true when a function has been called
145 146 147 |
# File 'lib/llm/function.rb', line 145 def called? @called end |
#cancel(reason: "function call cancelled") ⇒ LLM::Function::Return
Returns a value that communicates that the function call was cancelled
136 137 138 139 140 |
# File 'lib/llm/function.rb', line 136 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
152 153 154 |
# File 'lib/llm/function.rb', line 152 def cancelled? @cancelled end |
#define(klass = nil, &b) ⇒ void Also known as: register
This method returns an undefined value.
Set the function implementation
113 114 115 |
# File 'lib/llm/function.rb', line 113 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
85 86 87 88 89 90 91 |
# File 'lib/llm/function.rb', line 85 def description(desc = nil) if desc @description = desc else @description end end |
#format_openai(provider) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/llm/function.rb', line 176 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
73 74 75 76 77 78 79 |
# File 'lib/llm/function.rb', line 73 def name(name = nil) if name @name = name.to_s else @name end end |
#params {|schema| ... } ⇒ void
This method returns an undefined value.
Set (or get) the function parameters
97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/llm/function.rb', line 97 def params if block_given? if @params @params.merge!(yield(@schema)) else @params = yield(@schema) end else @params end end |
#pending? ⇒ Boolean
Returns true when a function has neither been called nor cancelled
159 160 161 |
# File 'lib/llm/function.rb', line 159 def pending? !@called && !@cancelled end |