Class: SmartAgent::Tool

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Tool

Returns a new instance of Tool.



3
4
5
6
7
# File 'lib/smart_agent/tool.rb', line 3

def initialize(name)
  SmartAgent.logger.info "Create tool's name is #{name}"
  @name = name
  @code = self.class.tools[name]
end

Class Method Details

.define(name, &block) ⇒ Object



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

def define(name, &block)
  tools[name] = block
end

.find_tool(name) ⇒ Object



50
51
52
# File 'lib/smart_agent/tool.rb', line 50

def find_tool(name)
  tools[name]
end

.toolsObject



42
43
44
# File 'lib/smart_agent/tool.rb', line 42

def tools
  @tools ||= {}
end

Instance Method Details

#call(params) ⇒ Object



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

def call(params)
  @context = ToolContext.new
  @context.input_params = params
  @context.instance_eval(&@code)
end

#to_jsonObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/smart_agent/tool.rb', line 15

def to_json
  @context = ToolContext.new
  @context.instance_eval(&@code)
  params = @context.params

  properties = params.each_with_object({}) do |(name, details), hash|
    hash[name] = {
      type: details[:type],
      description: details[:description],
    }
  end

  return {
           type: "function",
           function: {
             name: @name,
             description: "",
             parameters: {
               type: "object",
               properties: properties,
               required: params.keys,
             },
           },
         }
end