Class: Anthemic::Agent

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, instructions:, provider: nil, memory_type: nil, tools: []) ⇒ Agent

Initialize a new agent

Parameters:

  • name (String)

    the name of the agent

  • instructions (String)

    the base instructions for the agent

  • provider (Symbol, Provider) (defaults to: nil)

    the LLM provider to use

  • memory_type (Symbol) (defaults to: nil)

    the type of memory to use

  • tools (Array<Tool>) (defaults to: [])

    an array of tools available to the 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

#instructionsObject (readonly)

Returns the value of attribute instructions.



5
6
7
# File 'lib/anthemic/agent.rb', line 5

def instructions
  @instructions
end

#memoryObject (readonly)

Returns the value of attribute memory.



5
6
7
# File 'lib/anthemic/agent.rb', line 5

def memory
  @memory
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/anthemic/agent.rb', line 5

def name
  @name
end

#providerObject (readonly)

Returns the value of attribute provider.



5
6
7
# File 'lib/anthemic/agent.rb', line 5

def provider
  @provider
end

#toolsObject (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

Parameters:

  • goal (String)

    the goal to plan for

Returns:

  • (Array<String>)

    a list of steps to achieve the 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

Parameters:

  • message (String)

    the message to respond to

Returns:

  • (String)

    the agent’s response



59
60
61
# File 'lib/anthemic/agent.rb', line 59

def respond(message)
  run(message) # Delegates to run for now
end

#run(task) ⇒ String

Run the agent with a specific task

Parameters:

  • task (String)

    the task for the agent to perform

Returns:

  • (String)

    the agent’s response



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