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:) ⇒ String
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.
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.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/ollama_chat/dialog.rb', line 107 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.
162 163 164 |
# File 'lib/ollama_chat/dialog.rb', line 162 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.
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/ollama_chat/dialog.rb', line 80 def choose_collection(current_collection) collections = [ current_collection ] + @documents.collections 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.
53 54 55 56 57 58 59 60 61 |
# File 'lib/ollama_chat/dialog.rb', line 53 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.
145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/ollama_chat/dialog.rb', line 145 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:) ⇒ String
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.
32 33 34 35 36 37 38 39 |
# File 'lib/ollama_chat/dialog.rb', line 32 def confirm?(prompt:) print prompt system 'stty raw' c = STDIN.getc system 'stty cooked' puts c end |
#connect_message(model, base_url) ⇒ Object (private)
The connect_message method displays a connection status message.
67 68 69 70 71 |
# File 'lib/ollama_chat/dialog.rb', line 67 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)
174 175 176 |
# File 'lib/ollama_chat/dialog.rb', line 174 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.
170 171 172 |
# File 'lib/ollama_chat/dialog.rb', line 170 def MessageList.new(self) end |