Class: LLM::Shell::REPL

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/shell/repl.rb

Overview

The LLM::Shell::REPL class represents a loop that accepts user input, evaluates it via the LLM, and prints the response to stdout.

Instance Method Summary collapse

Constructor Details

#initialize(bot:, options:) ⇒ LLM::Shell::REPL

Parameters:



13
14
15
16
17
18
# File 'lib/llm/shell/repl.rb', line 13

def initialize(bot:, options:)
  @bot = bot
  @console = IO.console
  @options = options
  @io = IO::Line.new($stdout)
end

Instance Method Details

#setupvoid

This method returns an undefined value.

Performs initial setup



23
24
25
26
27
28
29
30
# File 'lib/llm/shell/repl.rb', line 23

def setup
  LLM::Shell.commands.each { |file| require file }
  Readline.completion_proc = Completion.to_proc
  chat options.prompt, role: options.default.role
  files.each { chat ["--- START: #{_1} ---", File.read(_1), "--- END: #{_1} ---"].join("\n") }
  bot.messages.each(&:read!)
  clear_screen
end

#startvoid

This method returns an undefined value.

Enters the main loop



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/llm/shell/repl.rb', line 35

def start
  loop do
    read
    eval
    emit
  rescue LLM::ResponseError => ex
    print Paint[ex.response.class, :red], "\n"
    print ex.response.body, "\n"
  rescue => ex
    print Paint[ex.class, :red], "\n"
    print ex.message, "\n"
    print ex.backtrace[0..5].join("\n")
  rescue Interrupt
    throw(:exit, 0)
  end
end