Class: LLM::Function

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

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:



25
26
27
28
29
# File 'lib/llm/function.rb', line 25

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

Instance Attribute Details

#argumentsArray?

Returns function arguments

Returns:

  • (Array, nil)


15
16
17
# File 'lib/llm/function.rb', line 15

def arguments
  @arguments
end

#idString?

Returns the function ID

Returns:

  • (String, nil)


20
21
22
# File 'lib/llm/function.rb', line 20

def id
  @id
end

#nameString (readonly)

Returns the function name

Returns:

  • (String)


10
11
12
# File 'lib/llm/function.rb', line 10

def name
  @name
end

Instance Method Details

#callObject

Call the function

Parameters:

  • args (Array)

    The arguments to pass to the function

Returns:

  • (Object)

    The result of the function call



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

def call
  Return.new id, @runner.call(arguments)
ensure
  @called = true
end

#called?Boolean

Returns true when a function has been called

Returns:

  • (Boolean)


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

def called?
  @called
end

#define(&b) ⇒ void

This method returns an undefined value.

Set the function implementation

Parameters:

  • b (Proc)

    The function implementation



50
51
52
# File 'lib/llm/function.rb', line 50

def define(&b)
  @runner = b
end

#description(str) ⇒ void

This method returns an undefined value.

Set the function description

Parameters:

  • str (String)

    The function description



35
36
37
# File 'lib/llm/function.rb', line 35

def description(str)
  @description = str
end

#format(provider) ⇒ Hash

Returns:

  • (Hash)


73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/llm/function.rb', line 73

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

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

This method returns an undefined value.

Yield Parameters:



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

def params
  @params = yield(@schema)
end