Class: Agents::DispatchingGptAgent

Inherits:
Dispatcher show all
Defined in:
lib/agents/gpt_agents/dispatching_gpt_agent.rb

Instance Attribute Summary collapse

Attributes inherited from Dispatcher

#agents

Attributes inherited from Agent

#actions

Instance Method Summary collapse

Methods inherited from Dispatcher

#handle, #register, #unregister

Methods inherited from Agent

#handle, #register, #unregister

Constructor Details

#initialize(gpt_client:, **args) ⇒ DispatchingGptAgent

Returns a new instance of DispatchingGptAgent.



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

def initialize(gpt_client:, **args)
  super(**args)
  @gpt_client = gpt_client
end

Instance Attribute Details

#gpt_clientObject (readonly)

Returns the value of attribute gpt_client.



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

def gpt_client
  @gpt_client
end

Instance Method Details

#agent_for_request(request:) ⇒ Object



9
10
11
12
13
14
15
16
17
# File 'lib/agents/gpt_agents/dispatching_gpt_agent.rb', line 9

def agent_for_request(request:)
  categorize_request = Request.new(prompt_for_categorizing_request(request: request))

  response_data = CategorizingGptAgent.new(gpt_client: gpt_client)
                                      .handle(request: categorize_request).data

  agent_class = response_data["category"]
  agents.find{ _1.class.name == agent_class } || UnhandleableRequestAgent.new
end

#prompt_for_categorizing_request(request:) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/agents/gpt_agents/dispatching_gpt_agent.rb', line 19

def prompt_for_categorizing_request(request:)
  prompt = "The following is a list of Assistants and a description of what each can do: \n\n"
  prompt << agents.collect{ "#{_1.class.name}: #{_1.description}" }.join("\n\n")
  prompt << "If you are not sure which Assistant to use, it's okay to ask for more information.\n\n"
  prompt << "Please categorize the request by typing the name of the Assistant that best fits the request.\n\n"
  prompt << "Request: #{request.request_text}"
end