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 ⇒ 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.
-
#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.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 145 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.
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 85 def change_system_prompt(default) prompts = all_system_prompts prompts.unshift('[MODEL DEFAULT]').unshift('[EXIT]') chosen = choose_entry(prompts) 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.
177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 177 def choose_and_delete_system_prompt system_prompt = choose_system_prompt 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.
165 166 167 168 169 170 171 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 165 def choose_and_edit_system_prompt system_prompt = choose_system_prompt 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 ⇒ Object?
Presents an interactive menu to select a stored system prompt.
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 109 def choose_system_prompt prompts = all_system_prompts prompts.unshift('[EXIT]') case chosen = choose_entry(prompts) 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.
49 50 51 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 49 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.
220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 220 def duplicate_system_prompt system_prompt = choose_system_prompt 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.
275 276 277 278 279 280 281 282 283 284 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 275 def export_system_prompt prompt = choose_system_prompt 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.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 242 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.
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 124 def info_system_prompt if system_prompt = choose_system_prompt 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.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 194 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 |
#reset_system_prompt_to_default(name) ⇒ Boolean
Resets a system prompt’s content to the default value defined in the configuration.
290 291 292 293 294 295 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 290 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.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 61 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 |