Class: DeepAgents::ReactAgent
- Inherits:
-
Object
- Object
- DeepAgents::ReactAgent
- Defined in:
- lib/deepagents/graph.rb
Overview
ReactAgent class for implementing the React pattern
Instance Attribute Summary collapse
-
#instructions ⇒ Object
readonly
Returns the value of attribute instructions.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Instance Method Summary collapse
-
#initialize(tools, instructions, model, state_schema = nil) ⇒ ReactAgent
constructor
A new instance of ReactAgent.
- #run(max_iterations = 10) ⇒ Object
Constructor Details
#initialize(tools, instructions, model, state_schema = nil) ⇒ ReactAgent
Returns a new instance of ReactAgent.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/deepagents/graph.rb', line 12 def initialize(tools, instructions, model, state_schema = nil) @tools = tools @instructions = instructions @model = model @state = DeepAgentState.new @tool_registry = ToolRegistry.new # Register tools tools.each do |tool| @tool_registry.register(tool) end end |
Instance Attribute Details
#instructions ⇒ Object (readonly)
Returns the value of attribute instructions.
10 11 12 |
# File 'lib/deepagents/graph.rb', line 10 def instructions @instructions end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
10 11 12 |
# File 'lib/deepagents/graph.rb', line 10 def model @model end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
10 11 12 |
# File 'lib/deepagents/graph.rb', line 10 def state @state end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
10 11 12 |
# File 'lib/deepagents/graph.rb', line 10 def tools @tools end |
Instance Method Details
#run(max_iterations = 10) ⇒ Object
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 57 58 59 60 61 |
# File 'lib/deepagents/graph.rb', line 25 def run(max_iterations = 10) iterations = 0 = [] while iterations < max_iterations iterations += 1 prompt = build_prompt() 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] begin tool = @tool_registry.get(tool_name) result = execute_tool(tool, tool_args) << { role: "assistant", content: "I'll use the #{tool_name} tool." } << { role: "system", content: "Tool result: #{result}" } rescue => e << { role: "system", content: "Error: #{e.message}" } end 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 |