Class: RSmolagent::Agent

Inherits:
Object
  • Object
show all
Defined in:
lib/rsmolagent/agent.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(llm_provider:, tools: [], system_prompt: nil, max_steps: 10) ⇒ Agent

Returns a new instance of Agent.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rsmolagent/agent.rb', line 5

def initialize(llm_provider:, tools: [], system_prompt: nil, max_steps: 10)
  @llm = llm_provider
  @memory = Memory.new
  @max_steps = max_steps
  @dynamic_tools = {}
  
  # Initialize system prompt
  default_system_prompt = "You are a helpful assistant that can use tools to solve tasks. " +
                          "When you need information or want to perform actions, use the provided tools. " +
                          "When you have the final answer, use the final_answer tool."
  
  system_message = system_prompt || default_system_prompt
  @memory.add_system_message(system_message)
  
  # Set up tools
  @tools = {}
  tools.each { |tool| @tools[tool.name] = tool }
  
  # Add final answer tool if not present
  unless @tools["final_answer"]
    final_answer_tool = FinalAnswerTool.new
    @tools[final_answer_tool.name] = final_answer_tool
  end
end

Instance Attribute Details

#llmObject (readonly)

Returns the value of attribute llm.



3
4
5
# File 'lib/rsmolagent/agent.rb', line 3

def llm
  @llm
end

#max_stepsObject (readonly)

Returns the value of attribute max_steps.



3
4
5
# File 'lib/rsmolagent/agent.rb', line 3

def max_steps
  @max_steps
end

#memoryObject (readonly)

Returns the value of attribute memory.



3
4
5
# File 'lib/rsmolagent/agent.rb', line 3

def memory
  @memory
end

#toolsObject (readonly)

Returns the value of attribute tools.



3
4
5
# File 'lib/rsmolagent/agent.rb', line 3

def tools
  @tools
end

Instance Method Details

#register_tool(tool) ⇒ Object

Method to register a new tool during execution



31
32
33
34
35
# File 'lib/rsmolagent/agent.rb', line 31

def register_tool(tool)
  @dynamic_tools[tool.name] = tool
  # Update memory with information about the new tool
  @memory.add_assistant_message("I've created a new tool called '#{tool.name}' that #{tool.description}")
end

#run(task, verbose: false) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rsmolagent/agent.rb', line 37

def run(task, verbose: false)
  @memory.add_user_message(task)
  step_count = 0
  
  while step_count < @max_steps
    step_count += 1
    puts "Step #{step_count}/#{@max_steps}" if verbose
    
    # Get response from LLM
    response = execute_step
    
    # Check if we've reached a final answer
    if response[:final_answer]
      return response[:answer]
    end
    
    puts "Used tool: #{response[:tool_name]}" if verbose
  end
  
  # If we reach max steps without a final answer, return what we have
  "I couldn't complete the task in the allowed number of steps. My progress so far: " +
  @memory.history.map { |step| step[:type] == "tool_call" ? "#{step[:tool_name]}: #{step[:result]}" : "" }.join("\n")
end