Class: Google::ADK::FunctionTool

Inherits:
BaseTool
  • Object
show all
Defined in:
lib/google/adk/tools/function_tool.rb

Overview

Tool that wraps a Ruby callable (Proc, Method, etc.)

Instance Attribute Summary collapse

Attributes inherited from BaseTool

#description, #name

Instance Method Summary collapse

Constructor Details

#initialize(name:, description: nil, callable:, parameters_schema: nil) ⇒ FunctionTool

Initialize a function tool

Parameters:

  • name (String)

    Tool name

  • description (String) (defaults to: nil)

    Tool description (optional)

  • callable (Proc, Method)

    The function to wrap

  • parameters_schema (Hash) (defaults to: nil)

    JSON schema for parameters (optional)



17
18
19
20
21
# File 'lib/google/adk/tools/function_tool.rb', line 17

def initialize(name:, description: nil, callable:, parameters_schema: nil)
  super(name: name, description: description)
  @callable = callable
  @parameters_schema = parameters_schema # Don't default here, let to_gemini_schema handle it
end

Instance Attribute Details

#callableObject (readonly)

Returns the value of attribute callable.



9
10
11
# File 'lib/google/adk/tools/function_tool.rb', line 9

def callable
  @callable
end

#parameters_schemaObject (readonly)

Returns the value of attribute parameters_schema.



9
10
11
# File 'lib/google/adk/tools/function_tool.rb', line 9

def parameters_schema
  @parameters_schema
end

Instance Method Details

#call(params = {}) ⇒ Object

Execute the wrapped function

Parameters:

  • params (Hash) (defaults to: {})

    Function parameters

Returns:

  • (Object)

    Function result



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/google/adk/tools/function_tool.rb', line 27

def call(params = {})
  if @callable.parameters.empty?
    @callable.call
  else
    # Convert hash params to keyword arguments if the callable expects them
    if expects_keyword_args?
      @callable.call(**params)
    else
      @callable.call(params)
    end
  end
end

#schemaHash

Get parameter schema

Returns:

  • (Hash)

    JSON schema



43
44
45
# File 'lib/google/adk/tools/function_tool.rb', line 43

def schema
  @parameters_schema
end

#to_gemini_schemaHash

Convert to Gemini API schema format

Returns:

  • (Hash)

    Gemini function declaration



50
51
52
53
54
55
56
57
58
59
# File 'lib/google/adk/tools/function_tool.rb', line 50

def to_gemini_schema
  # Try to infer parameters from method signature if no schema provided
  schema = @parameters_schema || infer_schema_from_callable
  
  {
    "name" => @name,
    "description" => @description || "Function tool",
    "parameters" => schema
  }
end