Class: LLM::Function
- Inherits:
-
Object
- Object
- LLM::Function
- Defined in:
- 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
-
#id ⇒ String?
Returns the function ID.
-
#name ⇒ String
readonly
Returns the function name.
-
#arguments ⇒ Array?
Returns function arguments.
Instance Method Summary collapse
-
#format_openai(provider) ⇒ Object
-
#description(desc = nil) ⇒ void
Set the function description.
-
#params {|schema| ... } ⇒ void
-
#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.
-
#format(provider) ⇒ Hash
-
#initialize(name) {|self| ... } ⇒ Function
constructor
A new instance of Function.
Constructor Details
Instance Attribute Details
#id ⇒ String?
Returns the function ID
39 40 41 |
# File 'lib/llm/function.rb', line 39 def id @id end |
#name ⇒ String (readonly)
Returns the function name
44 45 46 |
# File 'lib/llm/function.rb', line 44 def name @name end |
#arguments ⇒ Array?
Returns function arguments
49 50 51 |
# File 'lib/llm/function.rb', line 49 def arguments @arguments end |
Instance Method Details
#format_openai(provider) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/llm/function.rb', line 148 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 |
#description(desc = nil) ⇒ void
This method returns an undefined value.
Set the function description
66 67 68 69 70 71 72 |
# File 'lib/llm/function.rb', line 66 def description(desc = nil) if desc @description = desc else @description end end |
#params {|schema| ... } ⇒ void
This method returns an undefined value.
77 78 79 |
# File 'lib/llm/function.rb', line 77 def params @params = yield(@schema) end |
#define(klass = nil, &b) ⇒ void Also known as: register
This method returns an undefined value.
Set the function implementation
85 86 87 |
# File 'lib/llm/function.rb', line 85 def define(klass = nil, &b) @runner = klass || b end |
#call ⇒ LLM::Function::Return
Call the function
93 94 95 96 97 98 |
# File 'lib/llm/function.rb', line 93 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
108 109 110 111 112 |
# File 'lib/llm/function.rb', line 108 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
117 118 119 |
# File 'lib/llm/function.rb', line 117 def called? @called end |
#cancelled? ⇒ Boolean
Returns true when a function has been cancelled
124 125 126 |
# File 'lib/llm/function.rb', line 124 def cancelled? @cancelled end |
#pending? ⇒ Boolean
Returns true when a function has neither been called nor cancelled
131 132 133 |
# File 'lib/llm/function.rb', line 131 def pending? !@called && !@cancelled end |
#format(provider) ⇒ Hash
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/llm/function.rb', line 137 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 |