Class: LLM::Repl

Inherits:
Object
  • Object
show all
Defined in:
lib/llm/repl.rb,
lib/llm/repl/bar.rb,
lib/llm/repl/node.rb,
lib/llm/repl/input.rb,
lib/llm/repl/buffer.rb,
lib/llm/repl/status.rb,
lib/llm/repl/stream.rb,
lib/llm/repl/walker.rb,
lib/llm/repl/window.rb,
lib/llm/repl/command.rb,
lib/llm/repl/markdown.rb,
lib/llm/repl/commands/exit.rb,
lib/llm/repl/commands/help.rb,
lib/llm/repl/commands/compact.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, Buffer, Command, Help, Input, Markdown, Node, Status, Stream, Walker, Window

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(agent:, name: nil, tools: [], skills: [], path: nil) ⇒ LLM::Repl

Parameters:

  • agent (LLM::Agent)
  • name (String, nil) (defaults to: nil)

    The agent's name (optional)

  • path (String, nil) (defaults to: nil)

    The path where to maintain runtime state

  • tools (Array<LLM::Tool>) (defaults to: [])

    Zero or more tools

  • skills (Array<String>) (defaults to: [])

    Zero or more skills



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/llm/repl.rb', line 45

def initialize(agent:, name: nil, tools: [], skills: [], path: nil)
  @width = 80
  @path = path
  @name = name || "agent"
  @agent = configure(agent:, path:)
  @provider = agent.llm.name
  @status = Status.new(self)
  @buffer = Buffer.new(self)
  @input = Input.new(self, height: 3)
  @window = Window.new(self)
  @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

#agentObject (readonly)

Returns the value of attribute agent.



29
30
31
# File 'lib/llm/repl.rb', line 29

def agent
  @agent
end

#bufferObject (readonly)

Returns the value of attribute buffer.



29
30
31
# File 'lib/llm/repl.rb', line 29

def buffer
  @buffer
end

#inputObject (readonly)

Returns the value of attribute input.



29
30
31
# File 'lib/llm/repl.rb', line 29

def input
  @input
end

#nameObject (readonly)

Returns the value of attribute name.



29
30
31
# File 'lib/llm/repl.rb', line 29

def name
  @name
end

#pathObject (readonly)

Returns the value of attribute path.



29
30
31
# File 'lib/llm/repl.rb', line 29

def path
  @path
end

#providerObject (readonly)

Returns the value of attribute provider.



29
30
31
# File 'lib/llm/repl.rb', line 29

def provider
  @provider
end

#statusObject

Returns the value of attribute status.



29
30
31
# File 'lib/llm/repl.rb', line 29

def status
  @status
end

#streamObject (readonly)

Returns the value of attribute stream.



29
30
31
# File 'lib/llm/repl.rb', line 29

def stream
  @stream
end

#threadObject (readonly)

Returns the value of attribute thread.



29
30
31
# File 'lib/llm/repl.rb', line 29

def thread
  @thread
end

#toolsObject (readonly)

Returns the value of attribute tools.



29
30
31
# File 'lib/llm/repl.rb', line 29

def tools
  @tools
end

#widthObject (readonly)

Returns the value of attribute width.



29
30
31
# File 'lib/llm/repl.rb', line 29

def width
  @width
end

#windowObject (readonly)

Returns the value of attribute window.



29
30
31
# File 'lib/llm/repl.rb', line 29

def window
  @window
end

Instance Method Details

#markdown(chars) ⇒ Array<Node>

Returns an AST

Parameters:

  • chars (String)

Returns:



114
115
116
# File 'lib/llm/repl.rb', line 114

def markdown(chars)
  LLM::Repl::Markdown.new(chars, width).ast
end

#startvoid

This method returns an undefined value.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/llm/repl.rb', line 72

def start
  window.open do
    catch(:exit) do
      write tree(agent.messages)
      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

#thinking_textString

Returns:

  • (String)


128
129
130
# File 'lib/llm/repl.rb', line 128

def thinking_text
  "thinking • Esc to cancel"
end

#write(chars, attrs = nil, method: :append) ⇒ void

This method returns an undefined value.

Parameters:

  • chars (String, Array)
  • attrs (Object) (defaults to: nil)
  • method (Symbol) (defaults to: :append)

    (:append, :replace)



96
97
98
99
# File 'lib/llm/repl.rb', line 96

def write(chars, attrs = nil, method: :append)
  buffer.write(chars, attrs, method:)
  window.redraw
end

#write_message(user, content, method: :append) ⇒ void

This method returns an undefined value.

Parameters:

  • user (String)
  • content (String)


105
106
107
108
# File 'lib/llm/repl.rb', line 105

def write_message(user, content, method: :append)
  buffer.write_message(user, content, method:)
  window.redraw
end