Class: Agents::DispatchingGptAgent

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

Constant Summary

Constants inherited from Agent

Agent::DEFAULT_SYSTEM_PROMPT

Instance Attribute Summary collapse

Attributes inherited from Agent

#actions, #children, #description, #gpt_client, #name

Instance Method Summary collapse

Methods inherited from Agent

#build_prompt, #delegate_request

Constructor Details

#initialize(agents: [], **args) ⇒ DispatchingGptAgent

Returns a new instance of DispatchingGptAgent.



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

def initialize(agents:[], **args)
  super(**args)
  @agents = agents
end

Instance Attribute Details

#agentsObject (readonly)

Returns the value of attribute agents.



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

def agents
  @agents
end

Instance Method Details

#handle(request:) ⇒ Object



35
36
37
38
39
40
41
42
43
# File 'lib/agents/dispatching_gpt_agent.rb', line 35

def handle(request:)
  response = gpt_client.chat system_prompt: system_prompt, prompt: "#{prompt_prefix}#{request.request_text}"

  response.suggested_actions.each do |action|
    actions.find{ |a| a.name == action["name"] }&.call(action["args"])
  end

  response
end

#system_promptObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/agents/dispatching_gpt_agent.rb', line 11

def system_prompt
  prompt = "    You are a helpful assistant. You will try your best to help answer or delegate each request you receive. If you need\n    additional information to process a request, please ask for it. If you're not quite sure what to do, please ask for\n    help or clarification.\n\n    For each request, please respond ONLY with a JSON object. The JSON object should have two top-level keys: `response` \n    and `actions`. The `response` key should be a short message that summarizes the actions to be taken. The `actions` key \n    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 \n    should be taken, or you don't think there's anything relevant, then respond with an empty array for the `actions` key.\n\n    The actions you can take are:\n\n    `ask_for_clarification`: Ask for help or clarification. The `args` key should be a JSON object with a single key, `request_text`.\n    `delegate`: Delegate the request to another assistant. The `args` key should be a JSON object with a single key, `next_agent`.\n\n    The agents you can delegate to are:\n        - \#{agents.each.collect{ \"    - \#{_1.name}: \#{_1.description}\" }.join(\"\\n\")}\n\n    If you are not sure which Assistant to use, it's okay to ask for more information with the `ask_for_clarification` action.\n  \n  EOS\nend\n"