Class: Gemini::ToolDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/gemini/tool_definition.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ ToolDefinition

Returns a new instance of ToolDefinition.



3
4
5
6
# File 'lib/gemini/tool_definition.rb', line 3

def initialize(&block)
  @functions = {}
  instance_eval(&block) if block_given?
end

Instance Method Details

#+(other) ⇒ Object

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
# File 'lib/gemini/tool_definition.rb', line 34

def +(other)
  raise ArgumentError, 'can only merge with another ToolDefinition' unless other.is_a?(ToolDefinition)

  new_definition = dup
  other.instance_variable_get(:@functions).each do |name, definition|
    new_definition.instance_variable_get(:@functions)[name] = definition
  end
  new_definition
end

#delete_function(name) ⇒ Object



44
45
46
# File 'lib/gemini/tool_definition.rb', line 44

def delete_function(name)
  @functions.delete(name)
end

#dupObject



62
63
64
65
66
# File 'lib/gemini/tool_definition.rb', line 62

def dup
  new_instance = self.class.new
  new_instance.instance_variable_set(:@functions, @functions.dup)
  new_instance
end

#function(name, description:, &block) ⇒ Object Also known as: add_function



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/gemini/tool_definition.rb', line 8

def function(name, description:, &block)
  @functions[name] = {
    name: name,
    description: description,
    parameters: {
      type: 'object',
      properties: {},
      required: []
    }
  }
  @current_function = name
  instance_eval(&block) if block_given?
  @current_function = nil
end

#list_functionsObject



48
49
50
# File 'lib/gemini/tool_definition.rb', line 48

def list_functions
  @functions.keys
end

#property(name, type:, description:, required: false) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/gemini/tool_definition.rb', line 24

def property(name, type:, description:, required: false)
  raise 'property must be defined within a function block' unless @current_function

  @functions[@current_function][:parameters][:properties][name] = {
    type: type.to_s,
    description: description
  }
  @functions[@current_function][:parameters][:required] << name if required
end

#to_hObject



52
53
54
55
56
# File 'lib/gemini/tool_definition.rb', line 52

def to_h
  {
    function_declarations: @functions.values
  }
end

#to_json(*args) ⇒ Object



58
59
60
# File 'lib/gemini/tool_definition.rb', line 58

def to_json(*args)
  to_h.to_json(*args)
end