Class: Regent::Engine::React

Inherits:
Base
  • Object
show all
Defined in:
lib/regent/engine/react.rb,
lib/regent/engine/react/prompt_template.rb

Defined Under Namespace

Modules: PromptTemplate

Constant Summary collapse

SEQUENCES =
{
  answer: "Answer:",
  action: "Action:",
  observation: "Observation:",
  stop: "PAUSE"
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#context, #llm, #max_iterations, #session, #toolchain

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Regent::Engine::Base

Instance Method Details

#reason(task) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/regent/engine/react.rb', line 13

def reason(task)
  session.exec(Span::Type::INPUT, top_level: true, message: task) { task }
  session.add_message({role: :system, content: Regent::Engine::React::PromptTemplate.system_prompt(context, toolchain.to_s)})
  session.add_message({role: :user, content: task})

  with_max_iterations do
    content = llm_call_response(stop: [SEQUENCES[:stop]])
    session.add_message({role: :assistant, content: content })

    return extract_answer(content) if answer_present?(content)

    if action_present?(content)
      tool_name, arguments = parse_tool_signature(content)
      tool = find_tool(tool_name)
      return unless tool
      result = tool_call_response(tool, arguments)
      session.add_message({ role: :user, content: "#{SEQUENCES[:observation]} #{result}" })
    end
  end
end