Class: Helicone::Tool

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context = nil) ⇒ Tool

Initialize a tool instance

Parameters:

  • context (Object) (defaults to: nil) —

    Context object passed from the agent



90
91
92
# File 'lib/helicone/tool.rb', line 90

def initialize(context = nil)
  @context = context
end

Class Attribute Details

.tool_description ⇒ Object (readonly)

Returns the value of attribute tool_description.



6
7
8
# File 'lib/helicone/tool.rb', line 6

def tool_description
  @tool_description
end

.tool_parameters ⇒ Object (readonly)

Returns the value of attribute tool_parameters.



6
7
8
# File 'lib/helicone/tool.rb', line 6

def tool_parameters
  @tool_parameters
end

Instance Attribute Details

#context ⇒ Object (readonly)

Returns the value of attribute context.



85
86
87
# File 'lib/helicone/tool.rb', line 85

def context
  @context
end

Class Method Details

.description(text) ⇒ void

This method returns an undefined value.

Set the tool description

Parameters:

  • text (String) —

    Description of what this tool does



19
20
21
# File 'lib/helicone/tool.rb', line 19

def description(text)
  @tool_description = text.strip
end

.function_name ⇒ String

Generate the function name from class name

Returns:

  • (String) —

    The function name for API calls



46
47
48
# File 'lib/helicone/tool.rb', line 46

def function_name
  @tool_name || derive_function_name
end

.inherited(subclass) ⇒ Object



8
9
10
11
12
13
# File 'lib/helicone/tool.rb', line 8

def inherited(subclass)
  super
  # Ensure subclasses get their own class instance variables
  subclass.instance_variable_set(:@tool_description, nil)
  subclass.instance_variable_set(:@tool_parameters, nil)
end

.parameters(schema) ⇒ void

This method returns an undefined value.

Set the tool parameters schema

Parameters:

  • schema (Hash) —

    JSON Schema for the tool parameters



27
28
29
# File 'lib/helicone/tool.rb', line 27

def parameters(schema)
  @tool_parameters = schema
end

.to_openai_tool ⇒ Hash

Generate OpenAI tool definition

Returns:

  • (Hash) —

    Tool definition formatted for OpenAI API



53
54
55
56
57
58
59
60
61
62
# File 'lib/helicone/tool.rb', line 53

def to_openai_tool
  {
    type: "function",
    function: {
      name: function_name,
      description: tool_description,
      parameters: tool_parameters || { type: "object", properties: {}, required: [] }
    }
  }
end

.tool_name(custom_name = nil) ⇒ String

Get or set a custom tool name

Parameters:

  • custom_name (String, nil) (defaults to: nil) —

    Custom name to set, or nil to get current name

Returns:

  • (String) —

    The tool name



35
36
37
38
39
40
41
# File 'lib/helicone/tool.rb', line 35

def tool_name(custom_name = nil)
  if custom_name
    @tool_name = custom_name
  else
    @tool_name || derive_function_name
  end
end

Instance Method Details

#execute(**args) ⇒ Hash

Execute the tool with the given arguments

Parameters:

  • args (Hash) —

    Arguments parsed from the tool call

Returns:

  • (Hash) —

    Result to be returned to the LLM

Raises:

  • (NotImplementedError)


98
99
100
# File 'lib/helicone/tool.rb', line 98

def execute(**args)
  raise NotImplementedError, "Subclasses must implement #execute"
end

#function_name ⇒ String

Delegate to class method for instances

Returns:

  • (String) —

    The function name



112
113
114
# File 'lib/helicone/tool.rb', line 112

def function_name
  self.class.function_name
end

#to_openai_tool ⇒ Hash

Delegate to class method for instances

Returns:

  • (Hash) —

    Tool definition formatted for OpenAI API



105
106
107
# File 'lib/helicone/tool.rb', line 105

def to_openai_tool
  self.class.to_openai_tool
end