Class: LLM::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/function.rb

Overview

The LLM::Function class represents a function that can be called by an LLM. It comes in two forms: a Proc-based function, or a Class-based function.

Examples:

# Proc-based
LLM.function(:system) do |fn|
  fn.description "Runs system commands, emits their output"
  fn.params do |schema|
    schema.object(command: schema.string.required)
  end
  fn.define do |params|
    Kernel.system(params.command)
  end
end
# Class-based
class System
  def call(params)
    Kernel.system(params.command)
  end
end

LLM.function(:system) do |fn|
  fn.description "Runs system commands, emits their output"
  fn.params do |schema|
    schema.object(command: schema.string.required)
  end
  fn.register(System)
end

Defined Under Namespace

Classes: Return

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:



57
58
59
60
61
62
63
# File 'lib/llm/function.rb', line 57

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

Instance Attribute Details

#nameString (readonly)

Returns the function name

Returns:

  • (String)


42
43
44
# File 'lib/llm/function.rb', line 42

def name
  @name
end

#argumentsArray?

Returns function arguments

Returns:

  • (Array, nil)


47
48
49
# File 'lib/llm/function.rb', line 47

def arguments
  @arguments
end

#idString?

Returns the function ID

Returns:

  • (String, nil)


52
53
54
# File 'lib/llm/function.rb', line 52

def id
  @id
end

Instance Method Details

#format(provider) ⇒ Hash

Returns:

  • (Hash)


135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/llm/function.rb', line 135

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

Parameters:

  • str (String)

    The function description



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

def description(str)
  @description = str
end

#params {|schema| ... } ⇒ void

This method returns an undefined value.

Yield Parameters:



76
77
78
# File 'lib/llm/function.rb', line 76

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

Parameters:

  • b (Proc, Class)

    The function implementation



84
85
86
# File 'lib/llm/function.rb', line 84

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

#callLLM::Function::Return

Call the function

Returns:



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

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

Examples:

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

Returns:



106
107
108
109
110
# File 'lib/llm/function.rb', line 106

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

Returns:

  • (Boolean)


115
116
117
# File 'lib/llm/function.rb', line 115

def called?
  @called
end

#cancelled?Boolean

Returns true when a function has been cancelled

Returns:

  • (Boolean)


122
123
124
# File 'lib/llm/function.rb', line 122

def cancelled?
  @cancelled
end

#pending?Boolean

Returns true when a function has neither been called nor cancelled

Returns:

  • (Boolean)


129
130
131
# File 'lib/llm/function.rb', line 129

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