Class: Geminize::Models::Tool

Inherits:
Object
  • Object
show all
Defined in:
lib/geminize/models/tool.rb

Overview

Represents a tool for function calling or code execution in Gemini API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(function_declaration = nil, code_execution = false) ⇒ Tool

Initialize a new tool

Parameters:

  • function_declaration (Geminize::Models::FunctionDeclaration, nil) (defaults to: nil)

    The function declaration

  • code_execution (Boolean) (defaults to: false)

    Whether this is a code execution tool

Raises:



17
18
19
20
21
# File 'lib/geminize/models/tool.rb', line 17

def initialize(function_declaration = nil, code_execution = false)
  @function_declaration = function_declaration
  @code_execution = code_execution
  validate!
end

Instance Attribute Details

#code_executionBoolean (readonly)

Returns Whether this tool is a code execution tool.

Returns:

  • (Boolean)

    Whether this tool is a code execution tool



11
12
13
# File 'lib/geminize/models/tool.rb', line 11

def code_execution
  @code_execution
end

#function_declarationGeminize::Models::FunctionDeclaration? (readonly)

Returns The function declaration for this tool.

Returns:



8
9
10
# File 'lib/geminize/models/tool.rb', line 8

def function_declaration
  @function_declaration
end

Instance Method Details

#to_hHash

Alias for to_hash

Returns:

  • (Hash)

    The tool as a hash



60
61
62
# File 'lib/geminize/models/tool.rb', line 60

def to_h
  to_hash
end

#to_hashHash

Convert the tool to a hash for API requests

Returns:

  • (Hash)

    The tool as a hash



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/geminize/models/tool.rb', line 46

def to_hash
  if @code_execution
    {
      code_execution: {}
    }
  else
    {
      functionDeclarations: @function_declaration.to_hash
    }
  end
end

#validate!Boolean

Validate the tool

Returns:

  • (Boolean)

    true if validation passes

Raises:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/geminize/models/tool.rb', line 26

def validate!
  if !@code_execution && @function_declaration.nil?
    raise Geminize::ValidationError.new(
      "Either function_declaration or code_execution must be provided",
      "INVALID_ARGUMENT"
    )
  end

  if @function_declaration && !@function_declaration.is_a?(Geminize::Models::FunctionDeclaration)
    raise Geminize::ValidationError.new(
      "Function declaration must be a FunctionDeclaration, got #{@function_declaration.class}",
      "INVALID_ARGUMENT"
    )
  end

  true
end