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:



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

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

Instance Attribute Details

#argumentsArray?

Returns function arguments

Returns:

  • (Array, nil)


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

def arguments
  @arguments
end

#idString?

Returns the function ID

Returns:

  • (String, nil)


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

def id
  @id
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



53
54
55
56
57
# File 'lib/llm/function.rb', line 53

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

#called?Boolean

Returns true when a function has been called

Returns:

  • (Boolean)


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

def called?
  @called
end

#define(&b) ⇒ void

This method returns an undefined value.

Set the function implementation

Parameters:

  • b (Proc)

    The function implementation



45
46
47
# File 'lib/llm/function.rb', line 45

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



30
31
32
# File 'lib/llm/function.rb', line 30

def description(str)
  @description = str
end

#format(provider) ⇒ Hash

Returns:

  • (Hash)


68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/llm/function.rb', line 68

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:



37
38
39
# File 'lib/llm/function.rb', line 37

def params
  @params = yield(@schema)
end