Class: Luogu::AgentRunner
Instance Attribute Summary collapse
-
#agents ⇒ Object
readonly
Returns the value of attribute agents.
-
#histories ⇒ Object
readonly
Returns the value of attribute histories.
-
#request_params ⇒ Object
readonly
Returns the value of attribute request_params.
Instance Method Summary collapse
- #create_messages(messages) ⇒ Object
- #find_and_save_final_answer(content) ⇒ Object
-
#initialize ⇒ AgentRunner
constructor
A new instance of AgentRunner.
- #provider ⇒ Object
- #register(agent) ⇒ Object
- #request(messages, run_agent_retries: 0) ⇒ Object
- #run(text) ⇒ Object (also: #chat)
- #run_agents(agents, _messages_, run_agent_retries: 0) ⇒ Object
- #templates ⇒ Object
Methods inherited from Base
Constructor Details
#initialize ⇒ AgentRunner
Returns a new instance of AgentRunner.
28 29 30 31 32 33 34 |
# File 'lib/luogu/agent_runner.rb', line 28 def initialize() @request_params = provider.parameter_model.call @histories = HistoryQueue.new provider.history_limit @last_user_input = '' @agents = [] @tools_response = [] end |
Instance Attribute Details
#agents ⇒ Object (readonly)
Returns the value of attribute agents.
27 28 29 |
# File 'lib/luogu/agent_runner.rb', line 27 def agents @agents end |
#histories ⇒ Object (readonly)
Returns the value of attribute histories.
27 28 29 |
# File 'lib/luogu/agent_runner.rb', line 27 def histories @histories end |
#request_params ⇒ Object (readonly)
Returns the value of attribute request_params.
27 28 29 |
# File 'lib/luogu/agent_runner.rb', line 27 def request_params @request_params end |
Instance Method Details
#create_messages(messages) ⇒ Object
59 60 61 62 63 |
# File 'lib/luogu/agent_runner.rb', line 59 def () [ { role: "system", content: templates.system.result(binding) } ] + @histories.to_a + end |
#find_and_save_final_answer(content) ⇒ Object
83 84 85 86 87 88 89 90 91 |
# File 'lib/luogu/agent_runner.rb', line 83 def find_and_save_final_answer(content) if (answer = provider.find_final_answer.call(content)) @histories.enqueue({role: "user", content: @last_user_input}) @histories.enqueue({role: "assistant", content: answer}) answer else nil end end |
#provider ⇒ Object
36 37 38 |
# File 'lib/luogu/agent_runner.rb', line 36 def provider config.provider end |
#register(agent) ⇒ Object
44 45 46 47 48 |
# File 'lib/luogu/agent_runner.rb', line 44 def register(agent) raise AssertionError.new('agent must inherit from Luogu::Agent') unless agent < Agent @agents << agent self end |
#request(messages, run_agent_retries: 0) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/luogu/agent_runner.rb', line 65 def request(, run_agent_retries: 0) logger.debug "request chat: #{}" @request_params. = response = provider.request.call(@request_params.to_h) unless response.code == 200 logger.error response.body raise RequestError end content = provider.parse.call(response) logger.debug content if (answer = self.find_and_save_final_answer(content)) logger.info "final answer: #{answer}" answer elsif content.is_a?(Array) run_agents(content, , run_agent_retries: run_agent_retries) end end |
#run(text) ⇒ Object Also known as: chat
50 51 52 53 54 55 56 |
# File 'lib/luogu/agent_runner.rb', line 50 def run(text) @last_user_input = text = ( [{role: "user", content: templates.user.result(binding)}] ) request() end |
#run_agents(agents, _messages_, run_agent_retries: 0) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/luogu/agent_runner.rb', line 93 def run_agents(agents, , run_agent_retries: 0) return if run_agent_retries > config.run_agent_retries run_agent_retries += 1 if (answer = find_and_save_final_answer(agents)) logger.info "final answer: #{answer}" return end @tools_response = [] agents.each do |agent| agent_class = Module.const_get(agent['action']) logger.info "#{run_agent_retries} running #{agent_class} input: #{agent['action_input']}" response = agent_class.new.call(agent['action_input']) @tools_response << {name: agent['action'], response: response} end = + [ { role: "assistant", content: agents.to_json }, { role: "user", content: templates.tool.result(binding) } ] request , run_agent_retries: run_agent_retries end |
#templates ⇒ Object
40 41 42 |
# File 'lib/luogu/agent_runner.rb', line 40 def templates config.templates end |