Class: OpenRouter::Tool

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

Defined Under Namespace

Classes: ItemsBuilder, ParametersBuilder, ToolBuilder

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(definition = {}) ⇒ Tool

Returns a new instance of Tool.



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/open_router/tool.rb', line 7

def initialize(definition = {})
  if definition.is_a?(Hash) && definition.key?(:function)
    @type = definition[:type] || "function"
    @function = definition[:function]
  elsif definition.is_a?(Hash)
    @type = "function"
    @function = definition
  else
    raise ArgumentError, "Tool definition must be a hash"
  end

  validate_definition!
end

Instance Attribute Details

#functionObject (readonly)

Returns the value of attribute function.



5
6
7
# File 'lib/open_router/tool.rb', line 5

def function
  @function
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/open_router/tool.rb', line 5

def type
  @type
end

Class Method Details

.define(&block) ⇒ Object

Class method for defining tools with a DSL



22
23
24
25
26
# File 'lib/open_router/tool.rb', line 22

def self.define(&block)
  builder = ToolBuilder.new
  builder.instance_eval(&block) if block_given?
  new(builder.to_h)
end

Instance Method Details

#descriptionObject



43
44
45
# File 'lib/open_router/tool.rb', line 43

def description
  @function[:description]
end

#nameObject



39
40
41
# File 'lib/open_router/tool.rb', line 39

def name
  @function[:name]
end

#parametersObject



47
48
49
# File 'lib/open_router/tool.rb', line 47

def parameters
  @function[:parameters]
end

#to_hObject



28
29
30
31
32
33
# File 'lib/open_router/tool.rb', line 28

def to_h
  {
    type: @type,
    function: @function
  }
end

#to_json(*args) ⇒ Object



35
36
37
# File 'lib/open_router/tool.rb', line 35

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