Class: ActiveAgent::Agent
- Inherits:
-
Object
- Object
- ActiveAgent::Agent
- Defined in:
- lib/active_agent/agent.rb
Overview
Defines an autonomous AI Agent with persona, tools, and execution capabilities.
Instance Attribute Summary collapse
-
#backstory ⇒ Object
readonly
Returns the value of attribute backstory.
-
#goal ⇒ Object
readonly
Returns the value of attribute goal.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
-
#role ⇒ Object
readonly
Returns the value of attribute role.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Instance Method Summary collapse
- #execute_task(task_description, expected_output: nil, context: nil) ⇒ Object
-
#initialize(role:, goal:, backstory: '', tools: [], provider: :openai, model: nil, api_key: nil, api_url: nil) ⇒ Agent
constructor
A new instance of Agent.
Constructor Details
#initialize(role:, goal:, backstory: '', tools: [], provider: :openai, model: nil, api_key: nil, api_url: nil) ⇒ Agent
Returns a new instance of Agent.
13 14 15 16 17 18 19 |
# File 'lib/active_agent/agent.rb', line 13 def initialize(role:, goal:, backstory: '', tools: [], provider: :openai, model: nil, api_key: nil, api_url: nil) @role = role @goal = goal @backstory = backstory @tools = tools.map { |t| t.is_a?(Class) ? t.new : t } @provider = build_provider(provider, model: model, api_key: api_key, api_url: api_url) end |
Instance Attribute Details
#backstory ⇒ Object (readonly)
Returns the value of attribute backstory.
11 12 13 |
# File 'lib/active_agent/agent.rb', line 11 def backstory @backstory end |
#goal ⇒ Object (readonly)
Returns the value of attribute goal.
11 12 13 |
# File 'lib/active_agent/agent.rb', line 11 def goal @goal end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
11 12 13 |
# File 'lib/active_agent/agent.rb', line 11 def provider @provider end |
#role ⇒ Object (readonly)
Returns the value of attribute role.
11 12 13 |
# File 'lib/active_agent/agent.rb', line 11 def role @role end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
11 12 13 |
# File 'lib/active_agent/agent.rb', line 11 def tools @tools end |
Instance Method Details
#execute_task(task_description, expected_output: nil, context: nil) ⇒ Object
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 57 58 59 60 |
# File 'lib/active_agent/agent.rb', line 21 def execute_task(task_description, expected_output: nil, context: nil) system_prompt = build_system_prompt user_prompt = build_user_prompt(task_description, expected_output: expected_output, context: context) = [ { role: 'system', content: system_prompt }, { role: 'user', content: user_prompt } ] max_tool_loops = 5 loop_count = 0 while loop_count < max_tool_loops response = @provider.chat(messages: , tools: @tools) content = response[:content] tool_calls = response[:tool_calls] return content unless tool_calls && !tool_calls.empty? << { role: 'assistant', content: content, tool_calls: tool_calls } tool_calls.each do |tool_call| func_name = tool_call.dig('function', 'name') raw_args = tool_call.dig('function', 'arguments') args = raw_args.is_a?(String) ? JSON.parse(raw_args, symbolize_names: true) : raw_args matching_tool = @tools.find { |t| t.class.name.split('::').last.downcase == func_name.to_s.downcase } tool_result = if matching_tool matching_tool.perform(**args) else "Tool #{func_name} not found." end << { role: 'tool', tool_call_id: tool_call['id'], content: tool_result.to_s } end loop_count += 1 end .last[:content] || 'Execution completed.' end |