Class: Anthemic::Agent
- Inherits:
-
Object
- Object
- Anthemic::Agent
- Defined in:
- lib/anthemic/agent.rb
Instance Attribute Summary collapse
-
#instructions ⇒ Object
readonly
Returns the value of attribute instructions.
-
#memory ⇒ Object
readonly
Returns the value of attribute memory.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
Instance Method Summary collapse
-
#initialize(name:, instructions:, provider: nil, memory_type: nil, tools: []) ⇒ Agent
constructor
Initialize a new agent.
-
#plan(goal) ⇒ Array<String>
Create a plan to achieve a goal.
-
#respond(message) ⇒ String
Respond to a message in a conversation.
-
#run(task) ⇒ String
Run the agent with a specific task.
Constructor Details
#initialize(name:, instructions:, provider: nil, memory_type: nil, tools: []) ⇒ Agent
Initialize a new agent
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/anthemic/agent.rb', line 14 def initialize(name:, instructions:, provider: nil, memory_type: nil, tools: []) @name = name @instructions = instructions # Set up the LLM provider provider_sym = provider || Anthemic.configuration.default_provider @provider = if provider_sym.is_a?(Symbol) Providers.const_get(provider_sym.to_s.capitalize).new else provider end # Set up memory memory_type_sym = memory_type || Anthemic.configuration.default_memory_type @memory = if memory_type_sym.is_a?(Symbol) Memory.const_get(memory_type_sym.to_s.capitalize).new else memory_type end # Set up tools @tools = tools end |
Instance Attribute Details
#instructions ⇒ Object (readonly)
Returns the value of attribute instructions.
5 6 7 |
# File 'lib/anthemic/agent.rb', line 5 def instructions @instructions end |
#memory ⇒ Object (readonly)
Returns the value of attribute memory.
5 6 7 |
# File 'lib/anthemic/agent.rb', line 5 def memory @memory end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/anthemic/agent.rb', line 5 def name @name end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
5 6 7 |
# File 'lib/anthemic/agent.rb', line 5 def provider @provider end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
5 6 7 |
# File 'lib/anthemic/agent.rb', line 5 def tools @tools end |
Instance Method Details
#plan(goal) ⇒ Array<String>
Create a plan to achieve a goal
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/anthemic/agent.rb', line 67 def plan(goal) planning_prompt = <<~PROMPT You are #{name}, an AI assistant. Your goal is: #{goal} Create a step-by-step plan to achieve this goal. Return your plan as a JSON array of steps. PROMPT response = provider.generate(planning_prompt, format: "json") JSON.parse(response) rescue JSON::ParserError # If JSON parsing fails, extract steps manually as a fallback response.split("\n").map(&:strip).reject(&:empty?) end |
#respond(message) ⇒ String
Respond to a message in a conversation
59 60 61 |
# File 'lib/anthemic/agent.rb', line 59 def respond() run() # Delegates to run for now end |
#run(task) ⇒ String
Run the agent with a specific task
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/anthemic/agent.rb', line 42 def run(task) # Add the task to memory memory.add(role: "user", content: task) # Generate response based on the task and memory response = generate_response(task) # Add the response to memory memory.add(role: "assistant", content: response) response end |