Class: LLM::Repl
- Inherits:
-
Object
- Object
- LLM::Repl
- Defined in:
- lib/llm/repl.rb,
lib/llm/repl/bar.rb,
lib/llm/repl/node.rb,
lib/llm/repl/color.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
Modules: Color Classes: Bar, Buffer, Command, Help, Input, Markdown, Node, Status, Stream, Walker, Window
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#input ⇒ Object
readonly
Returns the value of attribute input.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#provider ⇒ Object
readonly
Returns the value of attribute provider.
-
#status ⇒ Object
Returns the value of attribute status.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#thread ⇒ Object
readonly
Returns the value of attribute thread.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
-
#window ⇒ Object
readonly
Returns the value of attribute window.
Instance Method Summary collapse
- #connect_text ⇒ String
- #initialize(agent:, name: nil, tools: [], skills: [], path: nil) ⇒ LLM::Repl constructor
-
#markdown(chars) ⇒ Array<Node>
Returns an AST.
-
#sender ⇒ String
Returns a label for the person who sent a message.
- #start ⇒ void
- #think_text ⇒ String
- #write(chars, attrs = nil, method: :append) ⇒ void
- #write_message(user, content, method: :append) ⇒ void
Constructor Details
#initialize(agent:, name: nil, tools: [], skills: [], path: nil) ⇒ LLM::Repl
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/llm/repl.rb', line 46 def initialize(agent:, name: nil, tools: [], skills: [], path: nil) @path = path @name = name || "agent" @sender = "You" @agent = configure(agent:, path:) @provider = agent.llm.name @input = Input.new(self, height: 3) @buffer = Buffer.new(self) @status = Status.new(self) @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
#agent ⇒ Object (readonly)
Returns the value of attribute agent.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def agent @agent end |
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def buffer @buffer end |
#input ⇒ Object (readonly)
Returns the value of attribute input.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def input @input end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def name @name end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def path @path end |
#provider ⇒ Object (readonly)
Returns the value of attribute provider.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def provider @provider end |
#status ⇒ Object
Returns the value of attribute status.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def status @status end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def stream @stream end |
#thread ⇒ Object (readonly)
Returns the value of attribute thread.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def thread @thread end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def tools @tools end |
#window ⇒ Object (readonly)
Returns the value of attribute window.
30 31 32 |
# File 'lib/llm/repl.rb', line 30 def window @window end |
Instance Method Details
#connect_text ⇒ String
135 136 137 |
# File 'lib/llm/repl.rb', line 135 def connect_text " 🌐 • Connecting • Esc to cancel" end |
#markdown(chars) ⇒ Array<Node>
Returns an AST
115 116 117 |
# File 'lib/llm/repl.rb', line 115 def markdown(chars) LLM::Repl::Markdown.new(chars, buffer.width).ast end |
#sender ⇒ String
Returns a label for the person who sent a message.
142 143 144 |
# File 'lib/llm/repl.rb', line 142 def sender @sender end |
#start ⇒ void
This method returns an undefined value.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/llm/repl.rb', line 73 def start window.open do catch(:exit) do write tree(agent.) 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 |
#think_text ⇒ String
129 130 131 |
# File 'lib/llm/repl.rb', line 129 def think_text " 🧠 • Thinking • Esc to cancel" end |
#write(chars, attrs = nil, method: :append) ⇒ void
This method returns an undefined value.
97 98 99 100 |
# File 'lib/llm/repl.rb', line 97 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.
106 107 108 109 |
# File 'lib/llm/repl.rb', line 106 def (user, content, method: :append) buffer.(user, content, method:) window.redraw end |