Class: LLM::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/repl.rb,
lib/llm/repl/bar.rb,
lib/llm/repl/input.rb,
lib/llm/repl/status.rb,
lib/llm/repl/stream.rb,
lib/llm/repl/window.rb,
lib/llm/repl/command.rb,
lib/llm/repl/markdown.rb,
lib/llm/repl/transcript.rb,
lib/llm/repl/commands/exit.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: Bar, Command, Input, Markdown, Status, Stream, Transcript, Window

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent:, tools:, skills:, path:) ⇒ LLM::Repl

Parameters:

  • agent (LLM::Agent)
  • path (String, nil)

    The path where to maintain runtime state

  • tools (Array<LLM::Tool>)

    Zero or more tools

  • skills (Array<String>)

    Zero or more skills



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

def initialize(agent:, tools:, skills:, path:)
  @path  = path
  @agent = configure(agent:, path:)
  @provider = agent.llm.name
  @status = Status.new(@agent)
  @transcript = Transcript.new
  @input = Input.new(@agent, height: 3)
  @window = Window.new(@status, @transcript, @input)
  @thread = nil
  @queue = Queue.new
  @stream = Stream.new(self, @queue)
  @skills = skills.map do |path|
    ##
    # I'm not sure it would make sense to expose
    # the underlying context or not. In the meantime,
    # this works and meets the expectations of the
    # LLM::Skill class.
    ctx = agent.instance_variable_get(:@ctx)
    LLM::Skill.load(path).to_tool(ctx)
  end
  @tools = [agent.params[:tools], @skills, tools].flatten.compact
end

Instance Attribute Details

#status=(value)

This method returns an undefined value.

Parameters:

  • value (String)


99
100
101
102
# File 'lib/llm/repl.rb', line 99

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

Instance Method Details

#start

This method returns an undefined value.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/llm/repl.rb', line 61

def start
  window.open do
    catch(:exit) do
      loop do
        now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
        case input.on_char(window, input.paste? ? window.read_paste : window.getch, now)
        when :submit then submit
        when Symbol then window.redraw
        else
          window.redraw
          read!
          sleep 0.01
        end
      end
    end
  end
end

#write(chars, attrs = nil)

This method returns an undefined value.

Parameters:

  • chars (String)
  • attrs (Object) (defaults to: nil)


83
84
85
86
# File 'lib/llm/repl.rb', line 83

def write(chars, attrs = nil)
  transcript.write(chars, attrs)
  window.redraw
end

#markdown(chars)

This method returns an undefined value.

Parameters:

  • chars (String)


91
92
93
94
# File 'lib/llm/repl.rb', line 91

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