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
-
#name ⇒ String
readonly
Returns the function name.
-
#arguments ⇒ Array?
Returns function arguments.
-
#id ⇒ String?
Returns the function ID.
Instance Method Summary collapse
-
#format(provider) ⇒ Hash
-
#description(str) ⇒ 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.
-
#initialize(name) {|self| ... } ⇒ Function
constructor
A new instance of Function.
Constructor Details
Instance Attribute Details
#name ⇒ String (readonly)
Returns the function name
39 40 41 |
# File 'lib/llm/function.rb', line 39 def name @name end |
#arguments ⇒ Array?
Returns function arguments
44 45 46 |
# File 'lib/llm/function.rb', line 44 def arguments @arguments end |
#id ⇒ String?
Returns the function ID
49 50 51 |
# File 'lib/llm/function.rb', line 49 def id @id end |
Instance Method Details
#format(provider) ⇒ Hash
132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/llm/function.rb', line 132 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 { type: "function", name: @name, function: {name: @name, description: @description, parameters: @params} }.compact end end |
#description(str) ⇒ void
This method returns an undefined value.
Set the function description
66 67 68 |
# File 'lib/llm/function.rb', line 66 def description(str) @description = str end |
#params {|schema| ... } ⇒ void
This method returns an undefined value.
73 74 75 |
# File 'lib/llm/function.rb', line 73 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
81 82 83 |
# File 'lib/llm/function.rb', line 81 def define(klass = nil, &b) @runner = klass || b end |
#call ⇒ LLM::Function::Return
Call the function
89 90 91 92 93 |
# File 'lib/llm/function.rb', line 89 def call Return.new id, (Class === @runner) ? @runner.new.call(arguments) : @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
103 104 105 106 107 |
# File 'lib/llm/function.rb', line 103 def cancel(reason: "function call cancelled") Return.new(id, {cancelled: true, reason:}) ensure @cancelled = true end |
#called? ⇒ Boolean
Returns true when a function has been called
112 113 114 |
# File 'lib/llm/function.rb', line 112 def called? @called end |
#cancelled? ⇒ Boolean
Returns true when a function has been cancelled
119 120 121 |
# File 'lib/llm/function.rb', line 119 def cancelled? @cancelled end |
#pending? ⇒ Boolean
Returns true when a function has neither been called nor cancelled
126 127 128 |
# File 'lib/llm/function.rb', line 126 def pending? !@called && !@cancelled end |