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 Attribute Summary collapse
-
#document_policy ⇒ Object
writeonly
The document_policy method sets the policy for handling document imports.
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
The change_system_prompt method allows the user to select or enter a new system prompt for the chat session.
-
#change_voice ⇒ String
The change_voice method allows the user to select a voice from a list of available options.
-
#choose_collection(current_collection) ⇒ Object
The choose_collection method presents a menu to select or create a document collection.
-
#choose_document_policy ⇒ Object
The choose_document_policy method presents a menu to select a document policy.
-
#choose_model(cli_model, current_model) ⇒ Object
The choose_model method selects a model from the available list based on CLI input or user interaction.
-
#choose_prompt ⇒ Object
The choose_prompt method presents a menu of available prompts for selection.
-
#connect_message(model, base_url) ⇒ Object
The connect_message method displays a connection status message.
-
#message_list ⇒ MessageList
The message_list method creates and returns a new MessageList instance initialized with the current object as its argument.
Instance Attribute Details
#document_policy=(value) ⇒ Object (writeonly)
The document_policy method sets the policy for handling document imports.
110 111 112 |
# File 'lib/ollama_chat/dialog.rb', line 110 def document_policy=(value) @document_policy = value end |
Instance Method Details
#ask?(prompt:) ⇒ String
The ask? method prompts the user with a question and returns their input.
77 78 79 80 |
# File 'lib/ollama_chat/dialog.rb', line 77 def ask?(prompt:) print prompt STDIN.gets.chomp end |
#change_system_prompt(default, system: nil) ⇒ Object
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.
search for
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/ollama_chat/dialog.rb', line 151 def change_system_prompt(default, system: nil) selector = if system =~ /\A\?(.+)\z/ Regexp.new($1) 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('[EXIT]').unshift('[NEW]') 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
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.
203 204 205 206 |
# File 'lib/ollama_chat/dialog.rb', line 203 def change_voice chosen = OllamaChat::Utils::Chooser.choose(config.voice.list) @current_voice = chosen.full? || config.voice.default end |
#choose_collection(current_collection) ⇒ Object
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.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/ollama_chat/dialog.rb', line 89 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_document_policy ⇒ Object
The choose_document_policy method presents a menu to select a document policy. It allows the user to choose from importing, embedding, summarizing, or ignoring documents. The method displays available policies and sets the selected policy as the current document policy. If no valid policy is found, it defaults to the first option. After selection, it outputs the chosen policy and displays the current configuration information.
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/ollama_chat/dialog.rb', line 120 def choose_document_policy policies = %w[ importing embedding summarizing ignoring ].sort current = if policies.index(@document_policy) @document_policy elsif policies.index(config.document_policy) config.document_policy else policies.first end policies.unshift('[EXIT]') policy = OllamaChat::Utils::Chooser.choose(policies) case policy when nil, '[EXIT]' STDOUT.puts "Exiting chooser." policy = current end self.document_policy = policy ensure STDOUT.puts "Using document policy #{bold{@document_policy}}." info end |
#choose_model(cli_model, current_model) ⇒ Object
The choose_model method selects a model from the available list based on CLI input or user interaction. It processes the provided CLI model parameter to determine if a regex selector is used, filters the models accordingly, and prompts the user to choose from the filtered list if needed. The method ensures that a model is selected and displays a connection message with the chosen model and base URL.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ollama_chat/dialog.rb', line 45 def choose_model(cli_model, current_model) selector = if cli_model =~ /\A\?+(.*)\z/ cli_model = '' Regexp.new($1) end models = ollama..models.sort_by(&:name).map { |m| model_with_size(m) } selector and models = models.grep(selector) model = if models.size == 1 models.first elsif cli_model == '' OllamaChat::Utils::Chooser.choose(models) || current_model else cli_model || current_model end ensure (model, ollama.base_url) end |
#choose_prompt ⇒ Object
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.
186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/ollama_chat/dialog.rb', line 186 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 |
#connect_message(model, base_url) ⇒ Object
The connect_message method displays a connection status message.
68 69 70 |
# File 'lib/ollama_chat/dialog.rb', line 68 def (model, base_url) STDOUT.puts green { "Connecting to #{model}@#{base_url} now…" } end |
#message_list ⇒ MessageList
The message_list method creates and returns a new MessageList instance initialized with the current object as its argument.
212 213 214 |
# File 'lib/ollama_chat/dialog.rb', line 212 def MessageList.new(self) end |