Class: DeepAgents::SubAgent
- Inherits:
-
Object
- Object
- DeepAgents::SubAgent
- Defined in:
- lib/deepagents/sub_agent.rb
Overview
SubAgent class for creating and managing sub-agents
Instance Attribute Summary collapse
-
#instructions ⇒ Object
readonly
Returns the value of attribute instructions.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Instance Method Summary collapse
-
#initialize(name, instructions, tools = [], model = nil) ⇒ SubAgent
constructor
A new instance of SubAgent.
- #run(input, max_iterations = 10) ⇒ Object
Constructor Details
#initialize(name, instructions, tools = [], model = nil) ⇒ SubAgent
Returns a new instance of SubAgent.
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/deepagents/sub_agent.rb', line 9 def initialize(name, instructions, tools = [], model = nil) raise ArgumentError, "Sub-agent name cannot be nil or empty" if name.nil? || name.empty? raise ArgumentError, "Sub-agent instructions cannot be nil or empty" if instructions.nil? || instructions.empty? @name = name @instructions = instructions @tools = tools @model = model @state = DeepAgentState.new end |
Instance Attribute Details
#instructions ⇒ Object (readonly)
Returns the value of attribute instructions.
7 8 9 |
# File 'lib/deepagents/sub_agent.rb', line 7 def instructions @instructions end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
7 8 9 |
# File 'lib/deepagents/sub_agent.rb', line 7 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/deepagents/sub_agent.rb', line 7 def name @name end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
7 8 9 |
# File 'lib/deepagents/sub_agent.rb', line 7 def state @state end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
7 8 9 |
# File 'lib/deepagents/sub_agent.rb', line 7 def tools @tools end |
Instance Method Details
#run(input, max_iterations = 10) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/deepagents/sub_agent.rb', line 20 def run(input, max_iterations = 10) raise ArgumentError, "Model must be provided to run the sub-agent" unless @model prompt = build_prompt(input) iterations = 0 = [] while iterations < max_iterations iterations += 1 response = @model.generate(prompt, @tools) if response[:tool_calls] && !response[:tool_calls].empty? tool_call = response[:tool_calls].first tool_name = tool_call[:name] tool_args = tool_call[:arguments] tool = find_tool(tool_name) result = execute_tool(tool, tool_args) << { role: "assistant", content: "I'll use the #{tool_name} tool." } << { role: "system", content: "Tool result: #{result}" } prompt = build_prompt(input, ) else << { role: "assistant", content: response[:content] } break end end if iterations >= max_iterations raise MaxIterationsError.new(max_iterations) end # Return the final response .last[:content] end |