Class: Agents::Agent

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

Constant Summary collapse

DEFAULT_SYSTEM_PROMPT =
"You are a helpful assistant. You will try your best to help answer or delegate each request you receive. If you need\nadditional information to process a request, please ask for it. If you're not quite sure what to do, please ask for\nhelp or clarification.\n\nFor each request, please respond ONLY with a JSON object. The JSON object should have a single (one) top-level key: `actions`. \nThe `actions` key should be an array of JSON objects, each of which has a `name` key and an `args` key. If you aren't sure what actions \nshould be taken, or you don't think there's anything relevant, then respond with an empty object for the `actions` key. The `args`\nkey should be a JSON object with the arguments for the action.\n\n"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(gpt_client:, name: "", description: "", system_prompt: DEFAULT_SYSTEM_PROMPT, actions: [Actions::AskForClarificationAction.new], children: []) ⇒ Agent

Returns a new instance of Agent.

Parameters:

  • actions (Array<Actions::Action>) (defaults to: [Actions::AskForClarificationAction.new])


23
24
25
26
27
28
29
30
31
32
# File 'lib/agents/agent.rb', line 23

def initialize(gpt_client:, name: "", description: "", system_prompt: DEFAULT_SYSTEM_PROMPT, actions: [Actions::AskForClarificationAction.new], children: [])
  @gpt_client = gpt_client
  @name = name
  @description = description
  @system_prompt = system_prompt
  @children = children

  @actions = actions
  @actions << Actions::DelegateAction.new(handler: self) if children.any?
end

Instance Attribute Details

#actionsObject (readonly)

Returns the value of attribute actions.



7
8
9
# File 'lib/agents/agent.rb', line 7

def actions
  @actions
end

#childrenObject (readonly)

Returns the value of attribute children.



8
9
10
# File 'lib/agents/agent.rb', line 8

def children
  @children
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#gpt_clientObject (readonly)

Returns the value of attribute gpt_client.



3
4
5
# File 'lib/agents/agent.rb', line 3

def gpt_client
  @gpt_client
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/agents/agent.rb', line 4

def name
  @name
end

#system_promptObject (readonly)

Returns the value of attribute system_prompt.



6
7
8
# File 'lib/agents/agent.rb', line 6

def system_prompt
  @system_prompt
end

Instance Method Details

#build_promptObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/agents/agent.rb', line 47

def build_prompt
  prompt = system_prompt.dup

  if actions.any?
    prompt << "The ONLY actions you can take are: #{actions.collect(&:name).join(", ")}. Here are the details on those actions: \n"
    prompt << actions.collect(&:for_prompt).join("\n\n")
  end

  if children.any?
    prompt << "\nIf you don't know how to handle this request, please `delegate` it to another agent. The agents you can `delegate` to are:\n\#{children.each.collect{ \"  - \#{_1.name}: \#{_1.description}\" }.join(\"\\n\")}\n\nIf you choose to delegate this request, please be sure to include `agent` as a key in your `args` object.\n\n"
  end

  prompt
end

#delegate_request(request:, args:) ⇒ Object



69
70
71
# File 'lib/agents/agent.rb', line 69

def delegate_request(request:, args:)
  children.find{ |c| c.name == args.dig("agent") }&.handle(request: request)
end

#handle(request:) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/agents/agent.rb', line 34

def handle(request:)
  gpt_response = gpt_client.chat system_prompt: build_prompt, prompt: "#{request.request_text}"
  results = gpt_response.suggested_actions.map{ |sa|
    actions.find{ |a| a.name == sa.dig("name") }&.perform(request: request, args: sa.dig("args"))
  }.compact

  if results.empty?
    "I'm sorry, I don't know how to handle that request."
  else
    results.reduce("", :+)
  end
end