Class: SmartAgent::Tool

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Tool



5
6
7
8
9
# File 'lib/smart_agent/tool.rb', line 5

def initialize(name)
  SmartAgent.logger.info "Create tool's name is #{name}"
  @name = name
  @context = ToolContext.new(self)
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



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

def context
  @context
end

#tool_procObject

Returns the value of attribute tool_proc.



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

def tool_proc
  @tool_proc
end

Class Method Details

.define(name, &block) ⇒ Object



52
53
54
55
56
# File 'lib/smart_agent/tool.rb', line 52

def define(name, &block)
  tool = Tool.new(name)
  tools[name] = tool
  tool.context.instance_eval(&block)
end

.define_group(name, &block) ⇒ Object



58
59
60
61
62
# File 'lib/smart_agent/tool.rb', line 58

def define_group(name, &block)
  tool_group = ToolGroup.new(name)
  tool_groups[name] = tool_group
  tool_group.context.instance_eval(&block)
end

.find_tool(name) ⇒ Object



64
65
66
# File 'lib/smart_agent/tool.rb', line 64

def find_tool(name)
  tools[name]
end

.tool_groupsObject



48
49
50
# File 'lib/smart_agent/tool.rb', line 48

def tool_groups
  @tool_groups ||= {}
end

.toolsObject



44
45
46
# File 'lib/smart_agent/tool.rb', line 44

def tools
  @tools ||= {}
end

Instance Method Details

#call(params, agent = nil) ⇒ Object



11
12
13
14
15
16
17
18
# File 'lib/smart_agent/tool.rb', line 11

def call(params, agent = nil)
  if agent
    agent.processor(:tool).call({ :content => "ToolName is `#{@name}`\n" }) if agent.processor(:tool)
    agent.processor(:tool).call({ :content => "params is `#{params}`\n" }) if agent.processor(:tool)
  end
  @context.input_params = params
  @context.instance_eval(&@context.proc)
end

#to_jsonObject



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

def to_json
  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: @context.description,
             parameters: {
               type: "object",
               properties: properties,
               required: params.keys,
             },
           },
         }
end