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



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
# File 'lib/llm/shell/repl.rb', line 23

def setup
  Reline.completion_proc = Completion.to_proc
  chat(options.prompt, role: options.default.role).flush
  unread.each(&:read!)
  clear_screen
end

#startvoid

This method returns an undefined value.

Enters the main loop



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

def start
  loop do
    read
    eval while functions.any?
    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