Class: Luogu::AgentRunner

Inherits:
Base
  • Object
show all
Defined in:
lib/luogu/agent_runner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#config, #logger

Constructor Details

#initializeAgentRunner

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

#agentsObject (readonly)

Returns the value of attribute agents.



29
30
31
# File 'lib/luogu/agent_runner.rb', line 29

def agents
  @agents
end

#historiesObject (readonly)

Returns the value of attribute histories.



29
30
31
# File 'lib/luogu/agent_runner.rb', line 29

def histories
  @histories
end

#request_paramsObject (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 create_messages(messages)
  [
    { role: "system", content: templates.system.result(binding) }
  ] + @histories.to_a + messages
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

#providerObject



38
39
40
# File 'lib/luogu/agent_runner.rb', line 38

def provider
  config.provider
end

#register(agent) ⇒ Object

Raises:



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(messages, run_agent_retries: 0)
  logger.debug "request chat: #{messages}"
  @request_params.messages = messages
  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.message
    return "error! #{error.message}"
  end


  logger.debug content

  if (answer = self.find_and_save_final_answer(content))
    logger.info "final answer: #{answer}"
    answer
  else
    run_agents(content, messages, 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
  messages = create_messages(
    [{role: "user", content: templates.user.result(binding)}]
  )
  request(messages)
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, _messages_, 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
  messages = _messages_ + [
    { role: "assistant", content: agents.to_json },
    { role: "user", content: templates.tool.result(binding) }
  ]
  if config.mode == :agent
    request messages, run_agent_retries: run_agent_retries
  else
    config.route_mode_response_handle.call @tools_response
  end
end

#templatesObject



42
43
44
# File 'lib/luogu/agent_runner.rb', line 42

def templates
  config.templates
end