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.
30 31 32 33 34 35 36 |
# File 'lib/luogu/agent_runner.rb', line 30 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.
29 30 31 |
# File 'lib/luogu/agent_runner.rb', line 29 def agents @agents end |
#histories ⇒ Object (readonly)
Returns the value of attribute histories.
29 30 31 |
# File 'lib/luogu/agent_runner.rb', line 29 def histories @histories end |
#request_params ⇒ Object (readonly)
Returns the value of attribute request_params.
29 30 31 |
# File 'lib/luogu/agent_runner.rb', line 29 def request_params @request_params end |
Instance Method Details
#create_messages(messages) ⇒ Object
61 62 63 64 65 |
# File 'lib/luogu/agent_runner.rb', line 61 def () [ { role: "system", content: templates.system.result(binding) } ] + @histories.to_a + end |
#find_and_save_final_answer(content) ⇒ Object
94 95 96 97 98 99 100 101 102 |
# File 'lib/luogu/agent_runner.rb', line 94 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
38 39 40 |
# File 'lib/luogu/agent_runner.rb', line 38 def provider config.provider end |
#register(agent) ⇒ Object
46 47 48 49 50 |
# File 'lib/luogu/agent_runner.rb', line 46 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
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/luogu/agent_runner.rb', line 67 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.to_s raise RequestError, response.body.to_s end begin content = Luogu.wrap_array(provider.parse.call(response)) rescue => error logger.error error. return "error! #{error.}" end logger.debug content if (answer = self.find_and_save_final_answer(content)) logger.info "final answer: #{answer}" answer else run_agents(content, , run_agent_retries: run_agent_retries) end end |
#run(text) ⇒ Object Also known as: chat
52 53 54 55 56 57 58 |
# File 'lib/luogu/agent_runner.rb', line 52 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
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/luogu/agent_runner.rb', line 104 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) } ] if config.mode == :agent request , run_agent_retries: run_agent_retries else config.route_mode_response_handle.call @tools_response end end |
#templates ⇒ Object
42 43 44 |
# File 'lib/luogu/agent_runner.rb', line 42 def templates config.templates end |