Class: LLM::Function
- Inherits:
-
Object
- Object
- LLM::Function
- Defined in:
- lib/llm/shell/internal/llm.rb/lib/llm/function.rb
Overview
The LLM::Function class represents a local function that can be called by an LLM.
Defined Under Namespace
Classes: Return
Instance Attribute Summary collapse
-
#arguments ⇒ Array?
Returns function arguments.
-
#id ⇒ String?
Returns the function ID.
Instance Method Summary collapse
-
#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(provider) ⇒ Hash
- #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
#initialize(name) {|self| ... } ⇒ Function
Returns a new instance of Function.
48 49 50 51 52 53 54 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 48 def initialize(name, &b) @name = name @schema = LLM::Schema.new @called = false @cancelled = false yield(self) if block_given? end |
Instance Attribute Details
#arguments ⇒ Array?
Returns function arguments
43 44 45 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 43 def arguments @arguments end |
#id ⇒ String?
Returns the function ID
38 39 40 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 38 def id @id end |
Instance Method Details
#call ⇒ LLM::Function::Return
Call the function
108 109 110 111 112 113 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 108 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
132 133 134 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 132 def called? @called end |
#cancel(reason: "function call cancelled") ⇒ LLM::Function::Return
Returns a value that communicates that the function call was cancelled
123 124 125 126 127 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 123 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
139 140 141 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 139 def cancelled? @cancelled end |
#define(klass = nil, &b) ⇒ void Also known as: register
This method returns an undefined value.
Set the function implementation
100 101 102 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 100 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
72 73 74 75 76 77 78 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 72 def description(desc = nil) if desc @description = desc else @description end end |
#format(provider) ⇒ Hash
152 153 154 155 156 157 158 159 160 161 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 152 def format(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 |
#format_openai(provider) ⇒ Object
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 163 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
60 61 62 63 64 65 66 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 60 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
84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 84 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
146 147 148 |
# File 'lib/llm/shell/internal/llm.rb/lib/llm/function.rb', line 146 def pending? !@called && !@cancelled end |