Class: Agents::GptAgent

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

Instance Attribute Summary collapse

Attributes inherited from Agent

#actions

Instance Method Summary collapse

Methods inherited from Agent

#register, #unregister

Constructor Details

#initialize(gpt_client:, description: "An AI Chatbot", system_prompt: "", **args) ⇒ GptAgent

Returns a new instance of GptAgent.



7
8
9
10
11
12
# File 'lib/agents/gpt_agents/gpt_agent.rb', line 7

def initialize(gpt_client:, description: "An AI Chatbot", system_prompt: "", **args)
  super(**args)
  @gpt_client = gpt_client
  @description = description
  @system_prompt = system_prompt
end

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#gpt_clientObject (readonly)

Returns the value of attribute gpt_client.



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

def gpt_client
  @gpt_client
end

#system_promptObject (readonly)

Returns the value of attribute system_prompt.



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

def system_prompt
  @system_prompt
end

Instance Method Details

#handle(request:) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/agents/gpt_agents/gpt_agent.rb', line 14

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

#prompt_prefixObject



24
25
26
# File 'lib/agents/gpt_agents/gpt_agent.rb', line 24

def prompt_prefix
  ""
end