Class: LLM::Function

Inherits:
Object
  • Object
show all
Includes:
Tracing
Defined in:
lib/llm/function.rb,
lib/llm/function/array.rb,
lib/llm/function/tracing.rb,
lib/llm/function/task_group.rb,
lib/llm/function/fiber_group.rb,
lib/llm/function/thread_group.rb

Overview

The LLM::Function class represents a local function that can be called by an LLM.

Examples:

example #1

LLM.function(:system) do |fn|
  fn.name "system"
  fn.description "Runs system commands"
  fn.params do |schema|
    schema.object(command: schema.string.required)
  end
  fn.define do |command:|
    {success: Kernel.system(command)}
  end
end

example #2

class System < LLM::Tool
  name "system"
  description "Runs system commands"
  params do |schema|
    schema.object(command: schema.string.required)
  end

  def call(command:)
    {success: Kernel.system(command)}
  end
end

Defined Under Namespace

Modules: Array, Tracing Classes: FiberGroup, Return, TaskGroup, ThreadGroup

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) {|self| ... } ⇒ Function

Returns a new instance of Function.

Parameters:

  • name (String)

    The function name

Yield Parameters:



78
79
80
81
82
83
84
# File 'lib/llm/function.rb', line 78

def initialize(name, &b)
  @name = name
  @schema = LLM::Schema.new
  @called = false
  @cancelled = false
  yield(self) if block_given?
end

Instance Attribute Details

#idString?

Returns the function ID

Returns:

  • (String, nil)


58
59
60
# File 'lib/llm/function.rb', line 58

def id
  @id
end

#argumentsArray?

Returns function arguments

Returns:



63
64
65
# File 'lib/llm/function.rb', line 63

def arguments
  @arguments
end

#tracerLLM::Tracer?

Returns a tracer, or nil

Returns:



68
69
70
# File 'lib/llm/function.rb', line 68

def tracer
  @tracer
end

#modelString?

Returns a model name, or nil

Returns:

  • (String, nil)


73
74
75
# File 'lib/llm/function.rb', line 73

def model
  @model
end

Instance Method Details

#adapt(provider) ⇒ Hash

Returns:

  • (Hash)


227
228
229
230
231
232
233
234
235
236
# File 'lib/llm/function.rb', line 227

def adapt(provider)
  case provider.class.to_s
  when "LLM::Google"
    {name: @name, description: @description, parameters: @params}.compact
  when "LLM::Anthropic"
    {name: @name, description: @description, input_schema: @params}.compact
  else
    format_openai(provider)
  end
end

#name(name = nil) ⇒ void

This method returns an undefined value.

Set (or get) the function name

Parameters:

  • name (String) (defaults to: nil)

    The function name



90
91
92
93
94
95
96
# File 'lib/llm/function.rb', line 90

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

Parameters:

  • desc (String) (defaults to: nil)

    The function description



102
103
104
105
106
107
108
# File 'lib/llm/function.rb', line 102

def description(desc = nil)
  if desc
    @description = desc
  else
    @description
  end
end

#params {|schema| ... } ⇒ LLM::Schema::Leaf?

Set (or get) the function parameters

Yield Parameters:

Returns:



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/llm/function.rb', line 114

def params
  if block_given?
    params = yield(@schema)
    params = LLM::Schema.parse(params) if Hash === params
    if @params
      @params.merge!(params)
    else
      @params = params
    end
  else
    @params
  end
end

#define(klass = nil, &b) ⇒ void Also known as: register

This method returns an undefined value.

Set the function implementation

Parameters:

  • b (Proc, Class)

    The function implementation



132
133
134
# File 'lib/llm/function.rb', line 132

def define(klass = nil, &b)
  @runner = klass || b
end

#callLLM::Function::Return

Call the function

Returns:



140
141
142
143
144
# File 'lib/llm/function.rb', line 140

def call
  call_function
ensure
  @called = true
end

#spawn(strategy) ⇒ Thread, ...

Calls the function in a separate thread.

This is the low-level method that powers concurrent tool execution. Prefer the collection methods on Context#functions for most use cases: LLM::Function::Array#call, LLM::Function::Array#wait, or LLM::Function::Array#spawn.

Examples:

# Normal usage (via collection)
ctx.talk(ctx.functions.wait)

# Direct usage (uncommon)
thread = tool.spawn
result = thread.value

Parameters:

  • strategy (Symbol)

    Controls concurrency strategy:

    • :thread: Use threads
    • :task: Use async tasks (requires async gem)
    • :fiber: Use raw fibers

Returns:

  • (Thread, Async::Task, Fiber)

    Returns a thread, async task, or fiber whose #value is an Return.



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/llm/function.rb', line 170

def spawn(strategy)
  case strategy
  when :task
    require "async" unless defined?(::Async)
    Async { call_function }
  when :thread
    Thread.new { call_function }
  when :fiber
    Fiber.new do
      call_function
    ensure
      Fiber.yield
    end.tap(&:resume)
  else
    raise ArgumentError, "Unknown strategy: #{strategy.inspect}. Expected :thread, :task, or :fiber"
  end
ensure
  @called = true
end

#cancel(reason: "function call cancelled") ⇒ LLM::Function::Return

Returns a value that communicates that the function call was cancelled

Examples:

llm = LLM.openai(key: ENV["KEY"])
ctx = LLM::Context.new(llm, tools: [fn1, fn2])
ctx.talk "I want to run the functions"
ctx.talk ctx.functions.map(&:cancel)

Returns:



198
199
200
201
202
# File 'lib/llm/function.rb', line 198

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

Returns:

  • (Boolean)


207
208
209
# File 'lib/llm/function.rb', line 207

def called?
  @called
end

#cancelled?Boolean

Returns true when a function has been cancelled

Returns:

  • (Boolean)


214
215
216
# File 'lib/llm/function.rb', line 214

def cancelled?
  @cancelled
end

#pending?Boolean

Returns true when a function has neither been called nor cancelled

Returns:

  • (Boolean)


221
222
223
# File 'lib/llm/function.rb', line 221

def pending?
  !@called && !@cancelled
end