Class: LLM::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/repl.rb,
lib/llm/repl/input.rb,
lib/llm/repl/status.rb,
lib/llm/repl/stream.rb,
lib/llm/repl/window.rb,
lib/llm/repl/transcript.rb

Overview

The LLM::Repl class provides a small read-eval-print loop around an instance of LLM::Agent.

It can be used to keep talking to an agent after it has been set up or has performed a task. This can be useful when you want to confirm the agent handled the task correctly, or for it to correct course after a mistake was made.

Defined Under Namespace

Classes: Input, Status, Stream, Transcript, Window

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent) ⇒ LLM::Repl

Parameters:



26
27
28
29
30
31
32
33
34
# File 'lib/llm/repl.rb', line 26

def initialize(agent)
  @agent = agent
  @provider = agent.llm.name
  @status = Status.new(@provider)
  @transcript = Transcript.new
  @input = Input.new(@provider)
  @window = Window.new(@status, @transcript, @input)
  @stream = Stream.new(self)
end

Instance Attribute Details

#status=(value) ⇒ void

This method returns an undefined value.

Parameters:

  • value (String)


67
68
69
70
# File 'lib/llm/repl.rb', line 67

def status=(value)
  status.text = value
  window.redraw
end

Instance Method Details

#startvoid

This method returns an undefined value.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/llm/repl.rb', line 38

def start
  window.open do
    loop do
      window.redraw
      text = input.readline(window)
      break if text.nil?
      next if text.empty?
      status.text = "thinking"
      write("user: #{text}\n")
      window.redraw
      write("agent: ")
      agent.talk(text, stream:)
      status.text = "idle"
      write("\n\n")
    end
  end
end

#write(chars) ⇒ void

This method returns an undefined value.

Parameters:

  • chars (String)


59
60
61
62
# File 'lib/llm/repl.rb', line 59

def write(chars)
  transcript.write(chars)
  window.redraw
end