Module: OllamaChat::Dialog
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/dialog.rb
Overview
A module that provides interactive selection and configuration functionality for OllamaChat.
The Dialog module encapsulates various helper methods for choosing models, system prompts, document policies, and voices, as well as displaying information and managing chat sessions. It leverages user interaction components like choosers and prompts to enable dynamic configuration during runtime.
Instance Method Summary collapse
-
#ask?(prompt:) ⇒ String
The ask? method prompts the user with a question and returns their input.
-
#change_system_prompt(default, system: nil) ⇒ Object
private
The change_system_prompt method allows the user to select or enter a new system prompt for the chat session.
-
#change_voice ⇒ String
private
The change_voice method allows the user to select a voice from a list of available options.
-
#choose_collection(current_collection) ⇒ Object
private
The choose_collection method presents a menu to select or create a document collection.
-
#choose_file_set(patterns) ⇒ Set<Pathname>
private
The choose_file_set method aggregates all files matching the given patterns by repeatedly invoking choose_filename and collecting their expanded paths into a Set.
-
#choose_prompt ⇒ Object
private
The choose_prompt method presents a menu of available prompts for selection.
-
#confirm?(prompt:, timeout: nil, default: nil, yes: nil) ⇒ Object
The confirm? method displays a prompt and reads a single character input from the user in raw mode, then returns that character.
-
#connect_message(model, base_url) ⇒ Object
private
The connect_message method displays a connection status message.
- #go_command(s, opt) ⇒ Object private
-
#message_list ⇒ MessageList
private
The message_list method creates and returns a new MessageList instance initialized with the current object as its argument.
-
#rename_collection(current_collection) ⇒ Object
private
Rename an existing collection to a new, user‑supplied name.
Instance Method Details
#ask?(prompt:) ⇒ String
The ask? method prompts the user with a question and returns their input.
21 22 23 24 |
# File 'lib/ollama_chat/dialog.rb', line 21 def ask?(prompt:) print prompt STDIN.gets.to_s.chomp end |
#change_system_prompt(default, system: nil) ⇒ Object (private)
The change_system_prompt method allows the user to select or enter a new system prompt for the chat session. It provides an interactive chooser when multiple prompts match the given selector, and sets the selected prompt as the current system prompt for the messages.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/ollama_chat/dialog.rb', line 168 def change_system_prompt(default, system: nil) selector = case system when /\A\?(.+)\z/ Regexp.new($1) when ?? /./ else Regexp.new(system.to_s) end prompts = config.system_prompts.attribute_names.compact.grep(selector).sort if prompts.size == 1 system = config.system_prompts.send(prompts.first) else prompts.unshift('[NEW]').unshift('[EXIT]') chosen = OllamaChat::Utils::Chooser.choose(prompts) system = case chosen when '[NEW]' ask?(prompt: "❓ Enter new system prompt to use: ") when '[EXIT]' STDOUT.puts "Exiting chooser." return when nil default when *prompts config.system_prompts.send(chosen) else default end end @messages.set_system_prompt(system) end |
#change_voice ⇒ String (private)
The change_voice method allows the user to select a voice from a list of available options. It uses the chooser to present the options and sets the selected voice as the current voice.
223 224 225 |
# File 'lib/ollama_chat/dialog.rb', line 223 def change_voice @voices.choose end |
#choose_collection(current_collection) ⇒ Object (private)
The choose_collection method presents a menu to select or create a document collection. It displays existing collections along with options to create a new one or exit. The method prompts the user for input and updates the document collection accordingly.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/ollama_chat/dialog.rb', line 120 def choose_collection(current_collection) collections = [ current_collection ] + @documents.collections.to_a collections = collections.compact.map(&:to_s).uniq.sort collections.unshift('[EXIT]').unshift('[NEW]') collection = OllamaChat::Utils::Chooser.choose(collections) || current_collection case collection when '[NEW]' @documents.collection = ask?(prompt: "❓ Enter name of the new collection: ") when nil, '[EXIT]' STDOUT.puts "Exiting chooser." when /./ @documents.collection = collection end ensure STDOUT.puts "Using collection #{bold{@documents.collection}}." info end |
#choose_file_set(patterns) ⇒ Set<Pathname> (private)
The choose_file_set method aggregates all files matching the given patterns by repeatedly invoking choose_filename and collecting their expanded paths into a Set.
93 94 95 96 97 98 99 100 101 |
# File 'lib/ollama_chat/dialog.rb', line 93 def choose_file_set(patterns) patterns ||= '**/*' patterns = Array(patterns) files = Set[] while filename = choose_filename(patterns, chosen: files) files << filename. end files end |
#choose_prompt ⇒ Object (private)
The choose_prompt method presents a menu of available prompts for selection. It retrieves the list of prompt attributes from the configuration, adds an ‘[EXIT]’ option to the list, and displays it to the user. After the user makes a choice, the method either exits the chooser or applies the selected prompt configuration.
206 207 208 209 210 211 212 213 214 215 216 |
# File 'lib/ollama_chat/dialog.rb', line 206 def choose_prompt prompts = config.prompts.attribute_names.sort prompts.unshift('[EXIT]') case chosen = OllamaChat::Utils::Chooser.choose(prompts) when '[EXIT]', nil STDOUT.puts "Exiting chooser." return when *prompts config.prompts.send(chosen) end end |
#confirm?(prompt:, timeout: nil, default: nil, yes: nil) ⇒ Object
The confirm? method displays a prompt and reads a single character input from the user in raw mode, then returns that character. This is best used for confirmation prompts.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/ollama_chat/dialog.rb', line 39 def confirm?(prompt:, timeout: nil, default: nil, yes: nil) return default if timeout&.zero? if prompt.include?('%s') prompt = prompt % (timeout ? ('timeout in %us' % timeout) : 'no timeout') end print prompt system 'stty raw' keypress = nil c = if timeout keypress = !!IO.select([ STDIN ], nil, nil, timeout) keypress ? STDIN.getc : nil else keypress = true STDIN.getc end system 'stty cooked' answer = c || default case when yes.nil? if keypress puts "⌨️ #{answer}" else puts "⌛️ #{answer}" end answer when answer =~ yes if keypress puts "✅ #{answer}" else puts "☑️ #{answer}" end answer else if keypress puts "🚫 #{answer}" else puts "⌛️ #{answer}" end nil end end |
#connect_message(model, base_url) ⇒ Object (private)
The connect_message method displays a connection status message.
107 108 109 110 111 |
# File 'lib/ollama_chat/dialog.rb', line 107 def (model, base_url) msg = "Connecting to #{model}@#{base_url} now…" log(:info, msg) STDOUT.puts green { msg } end |
#go_command(s, opt) ⇒ Object (private)
235 236 237 |
# File 'lib/ollama_chat/dialog.rb', line 235 def go_command(s, opt) Tins::GO.go(s, opt.to_s.strip.split(/\s+/)) end |
#message_list ⇒ MessageList (private)
The message_list method creates and returns a new MessageList instance initialized with the current object as its argument.
231 232 233 |
# File 'lib/ollama_chat/dialog.rb', line 231 def MessageList.new(self) end |
#rename_collection(current_collection) ⇒ Object (private)
Rename an existing collection to a new, user‑supplied name.
This helper prompts the user to provide a new name for the collection identified by current_collection. It then renames the current collection to have the new_name and switches to it.
145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/ollama_chat/dialog.rb', line 145 def rename_collection(current_collection) prompt = 'Rename collection %s to: ' % current_collection if new_collection = ask?(prompt:).full?(:to_sym) begin @documents.rename_collection(new_collection) STDOUT.puts "Renamed current collection #{current_collection} to #{new_collection}." rescue STDERR.puts "Renaming to #{new_collection} failed, it already exists." end else STDOUT.puts "Renaming cancelled." end end |