Module: OllamaChat::SystemPromptManagement
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/system_prompt_management.rb
Overview
Provides advanced system prompt management capabilities for the OllamaChat session.
This module encapsulates the logic for initializing, changing, and loading system prompts, allowing for dynamic and interactive configuration within the chat lifecycle.
Instance Method Summary collapse
-
#add_new_system_prompt ⇒ Boolean?
Interactively prompts the user for a name and content to create a new system prompt.
-
#all_system_prompts ⇒ Array<SearchUI::Wrapper>
Retrieves all stored system prompts, decorated with a heart if they are marked as favourites.
-
#change_system_prompt(default) ⇒ Object?
The change_system_prompt method allows the user to select or enter a new system prompt for the chat session.
-
#choose_and_delete_system_prompt ⇒ self?
Interactively selects an existing system prompt and deletes it after confirmation.
-
#choose_and_edit_system_prompt ⇒ self?
Interactively selects an existing system prompt and allows the user to edit its content.
-
#choose_system_prompt(prompt: 'Select a system prompt: %s') ⇒ Object?
Presents an interactive menu to select a stored system prompt.
-
#current_system_prompt ⇒ String?
Retrieves the content of the system prompt currently active in the chat session.
-
#current_system_prompt_name ⇒ String?
Returns the name of the system prompt currently active in the chat session.
-
#duplicate_system_prompt ⇒ self?
Duplicates an existing system prompt.
-
#export_system_prompt ⇒ self?
Interactively exports a system prompt to a specified file.
-
#import_system_prompt(filename) ⇒ self?
Imports a system prompt from a file.
-
#info_system_prompt ⇒ self?
Displays detailed information about a selected system prompt.
-
#list_system_prompts ⇒ Array
Lists all stored system prompts in a formatted view, displaying their default status and a truncated preview of their content.
-
#model_default_system_prompt ⇒ String?
Retrieves the default system prompt associated with the current model.
-
#raw_system_prompt ⇒ String
Resolves the currently active system prompt name to its raw text content.
-
#reset_system_prompt_to_default(name) ⇒ Boolean?
Resets a system prompt's content to the default value defined in the configuration.
-
#set_current_system_prompt(system_prompt_name) ⇒ Object
Sets the current system prompt for the chat session.
-
#setup_system_prompt ⇒ Object
Sets up the system prompt for the chat session.
Instance Method Details
#add_new_system_prompt ⇒ Boolean?
Interactively prompts the user for a name and content to create a new system prompt. Optionally sets the new prompt as the current one.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 172 def add_new_system_prompt switch_history(:add_system_prompt) do system_prompt_name = determine_valid_new_name_for_system_prompt('to add') or return patterns = ask?( prompt: "❓ Enter file patterns to load file, C-u ⇒ new, C-c ⇒ cancel: ", prefill: '**/*.{txt,md}' ) patterns.nil? and return content = nil patterns.present? and content = load_prompt_from_file(patterns) system_prompt = edit_text(content) store_system_prompt(system_prompt_name, system_prompt).to_s ask_to_set_current_system_prompt(system_prompt_name) end end |
#all_system_prompts ⇒ Array<SearchUI::Wrapper>
Retrieves all stored system prompts, decorated with a heart if they are marked as favourites.
13 14 15 16 17 18 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 13 def all_system_prompts favs = all_favourited('system_prompt') each_system_prompt.sort_by(&:name).map do |p| system_prompt_with_favourite(p.name, favs[p.name]) end end |
#change_system_prompt(default) ⇒ 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.
The user can choose from all stored system prompts, the model's default, or exit the chooser. If the selection is cancelled or returns nil, the provided default is used.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 106 def change_system_prompt(default) prompts = all_system_prompts prompts.unshift('[MODEL DEFAULT]').unshift('[EXIT]') chosen = choose_entry( prompts, prompt: 'Which governing law shall we enact? %s' ) system_prompt_name = case chosen when '[EXIT]' STDOUT.puts "Exiting chooser." return when '[MODEL DEFAULT]' 'model_default' when nil default when SearchUI::Wrapper chosen.value.to_s else default end set_current_system_prompt(system_prompt_name) end |
#choose_and_delete_system_prompt ⇒ self?
Interactively selects an existing system prompt and deletes it after confirmation.
206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 206 def choose_and_delete_system_prompt system_prompt = choose_system_prompt(prompt: 'Which old rule is now obsolete? %s') or return STDOUT.puts kramdown_ansi_parse( system_prompt.to_s + "\n---" ) confirm?( prompt: "🔔 Really delete the system prompt #{bold{system_prompt.name}}? (y/n) ", yes: /\Ay/i ) or return system_prompt.destroy self end |
#choose_and_edit_system_prompt ⇒ self?
Interactively selects an existing system prompt and allows the user to edit its content.
192 193 194 195 196 197 198 199 200 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 192 def choose_and_edit_system_prompt system_prompt = choose_system_prompt( prompt: 'Which system directive needs rewriting? %s' ) or return system_prompt.['content'] = edit_text(system_prompt.['content'].to_s) system_prompt.save ask_to_set_current_system_prompt(system_prompt.name) self end |
#choose_system_prompt(prompt: 'Select a system prompt: %s') ⇒ Object?
Presents an interactive menu to select a stored system prompt.
136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 136 def choose_system_prompt(prompt: 'Select a system prompt: %s') prompts = all_system_prompts prompts.unshift('[EXIT]') case chosen = choose_entry(prompts, prompt:) when '[EXIT]', nil STDOUT.puts "Exiting chooser." return when SearchUI::Wrapper system_prompt(chosen.value) end end |
#current_system_prompt ⇒ String?
Retrieves the content of the system prompt currently active in the chat session.
70 71 72 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 70 def current_system_prompt .system end |
#current_system_prompt_name ⇒ String?
Returns the name of the system prompt currently active in the chat session.
40 41 42 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 40 def current_system_prompt_name .system_name end |
#duplicate_system_prompt ⇒ self?
Duplicates an existing system prompt.
This method initiates an interactive workflow:
- Prompts the user to select a system prompt to clone.
- Displays the content of the selected prompt for verification.
- Requests a new name for the duplicate, validating that it does not already exist in the database.
- Creates and saves the new prompt record using the Database::Duplicatable mixin.
249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 249 def duplicate_system_prompt system_prompt = choose_system_prompt(prompt: 'Which core logic shall be cloned? %s') or return STDOUT.puts kramdown_ansi_parse( system_prompt.to_s + "\n---" ) system_prompt_name = determine_valid_new_name_for_system_prompt('to ducplicate as') or return duplicated_prompt = system_prompt.duplicate duplicated_prompt.name = system_prompt_name duplicated_prompt.['default'] = false duplicated_prompt.save self end |
#export_system_prompt ⇒ self?
Interactively exports a system prompt to a specified file.
The process follows these steps:
- Prompts the user to select a system prompt via
choose_system_prompt. - Displays the system prompt's current content to the terminal.
- Prompts for a destination filename via
determine_valid_output_filename. - Writes the system prompt content to the chosen file.
304 305 306 307 308 309 310 311 312 313 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 304 def export_system_prompt prompt = choose_system_prompt(prompt: 'Which system prompt are you archiving to disk? %s') or return STDOUT.puts kramdown_ansi_parse( prompt.to_s + "\n---" ) filename = determine_valid_output_filename('to write to') or return filename.write(prompt.to_s) STDOUT.puts "Prompt #{prompt.name.inspect} was exported as #{filename.to_path.inspect}?" self end |
#import_system_prompt(filename) ⇒ self?
Imports a system prompt from a file.
This method prompts the user for a name for the imported prompt, reads the content from the specified file, stores it, and then asks whether to set it as the current system prompt.
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 271 def import_system_prompt(filename) if filename if File.exist?(filename) filename = Pathname.new(filename) else filename = choose_filename(filename) end else filename = choose_filename('**/*.md') end unless filename STDOUT.puts "Cancelled." return end system_prompt_name = determine_valid_new_name_for_system_prompt('to import') or return system_prompt = filename.read store_system_prompt(system_prompt_name, system_prompt) ask_to_set_current_system_prompt(system_prompt_name) STDOUT.puts "Imported system prompt as #{system_prompt_name.inspect}." self end |
#info_system_prompt ⇒ self?
Displays detailed information about a selected system prompt.
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 151 def info_system_prompt if system_prompt = choose_system_prompt(prompt: 'Which system law would you like to review? %s') use_pager do |output| output.puts kramdown_ansi_parse(<<~EOT) # System Prompt #{system_prompt.name} --- #{system_prompt.to_s} --- EOT end end self end |
#list_system_prompts ⇒ Array
Lists all stored system prompts in a formatted view, displaying their default status and a truncated preview of their content.
223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 223 def list_system_prompts favs = all_favourited('system_prompt') each_system_prompt.sort_by(&:name).map do |prompt| default = prompt.['default'] ? '⛭' : '✎' start = '%s %s' % [ default, bold { prompt.name } ] start = prefix_favourite(start, favs[prompt.name]) content = prompt.to_s.inspect[1..-2] content = Kramdown::ANSI::Width.truncate( content, length: 0.9 * (Tins::Terminal.columns - start.size) ) STDOUT.print start STDOUT.puts ' %s' % italic { content } end end |
#model_default_system_prompt ⇒ String?
Retrieves the default system prompt associated with the current model.
24 25 26 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 24 def model_default_system_prompt @model_metadata&.system end |
#raw_system_prompt ⇒ String
Resolves the currently active system prompt name to its raw text content.
If the current system prompt is set to 'model_default', this method retrieves the default prompt associated with the loaded model. Otherwise, it fetches the content of the specifically named system prompt from the database.
The returned string represents the base template; any dynamic placeholders (e.g., %persona and %runtime_info) are nullified to ensure only the raw structure is returned.
56 57 58 59 60 61 62 63 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 56 def raw_system_prompt system_name = current_system_prompt_name if system_name == 'model_default' model_default_system_prompt.to_s else system_prompt(system_name).to_s end % { persona: nil, runtime_info: nil } end |
#reset_system_prompt_to_default(name) ⇒ Boolean?
Resets a system prompt's content to the default value defined in the configuration.
320 321 322 323 324 325 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 320 def reset_system_prompt_to_default(name) if content = config.system_prompts[name.to_s] store_system_prompt(name, content) true end end |
#set_current_system_prompt(system_prompt_name) ⇒ Object
Sets the current system prompt for the chat session.
31 32 33 34 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 31 def set_current_system_prompt(system_prompt_name) .set_system_prompt(system_prompt_name) session.update(current_system_prompt: system_prompt_name) end |
#setup_system_prompt ⇒ Object
Sets up the system prompt for the chat session.
This method determines whether to use a default system prompt or a custom one specified via command-line options. If a custom system prompt is provided with a regexp selector (starting with ?), it invokes the change_system_prompt method to handle the selection. Otherwise, it retrieves the system prompt from a file or uses the default value, then sets it in the message history.
82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 82 def setup_system_prompt system_prompt_name = session.current_system_prompt.full? || ('default' if system_prompt(:default)) || 'model_default' if system_prompt_name.full? set_current_system_prompt(system_prompt_name) else change_system_prompt(system_prompt_name) end end |