Module: OllamaChat::PromptManagement
- Included in:
- Chat
- Defined in:
- lib/ollama_chat/prompt_management.rb
Overview
Provides administrative and interactive management for prompt templates stored in the database.
This module handles the user-facing selection process for prompts, allowing users to interactively pick a prompt from the database.
Instance Method Summary collapse
-
#add_new_prompt ⇒ Boolean?
Interactively prompts the user for a name and content (optionally loading from a file) to create a new prompt template.
-
#all_prompts(default: nil) ⇒ Array<SearchUI::Wrapper>
Retrieves all stored prompts, decorated with a heart if they are marked as favourites.
-
#choose_and_delete_prompt ⇒ Object
Interactively selects an existing non-default prompt and deletes it after confirmation.
-
#choose_and_edit_prompt ⇒ self?
Interactively selects an existing prompt and allows the user to edit its content via the integrated editor.
-
#choose_prompt(default: nil, prompt: 'Select a prompt template: ') ⇒ OllamaChat::Database::Models::Prompt?
The choose_prompt method presents a menu of available prompts for selection.
-
#duplicate_prompt ⇒ self?
Duplicates an existing prompt.
-
#export_prompt ⇒ self?
Interactively exports a prompt to a specified file.
-
#import_prompt(filename) ⇒ self?
Interactively imports a prompt from a file.
-
#info_prompt ⇒ self?
Displays detailed information about a selected prompt template.
-
#list_prompts ⇒ Array
Lists all prompt templates in the database, indicating which are defaults and showing a truncated preview of their content.
-
#prepare_conversation_history ⇒ String
Aggregates the current conversation history into a single string for context-aware generation.
-
#reset_prompt_to_default(name) ⇒ Boolean?
Resets a prompt’s content to the default value defined in the configuration.
-
#suggest_prompts ⇒ String?
Interactively generates follow-up prompt suggestions based on the current session.
Instance Method Details
#add_new_prompt ⇒ Boolean?
Interactively prompts the user for a name and content (optionally loading from a file) to create a new prompt template.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/ollama_chat/prompt_management.rb', line 69 def add_new_prompt switch_history(:add_prompt) do name = nil loop do name = ask?( prompt: "❓ Enter new system prompt name to add, C-c ⇒ cancel: " ) if name.nil? STDOUT.puts "Cancelled." return nil end if prompt(name) STDOUT.puts "Prompt named #{bold{name}} already exists." else break end end 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) prompt = edit_text(content) store_prompt(name, prompt).to_s true end end |
#all_prompts(default: nil) ⇒ Array<SearchUI::Wrapper>
Retrieves all stored prompts, decorated with a heart if they are marked as favourites.
16 17 18 19 20 21 |
# File 'lib/ollama_chat/prompt_management.rb', line 16 def all_prompts(default: nil) favs = all_favourited('prompt') each_prompt(default:).sort_by(&:name).map do |p| prompt_with_favourite(p.name, favs[p.name]) end end |
#choose_and_delete_prompt ⇒ Object
Interactively selects an existing non-default prompt and deletes it after confirmation.
101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/ollama_chat/prompt_management.rb', line 101 def choose_and_delete_prompt prompt = choose_prompt(default: false, prompt: 'Which template has outlived its usefulness? ') or return STDOUT.puts kramdown_ansi_parse( prompt.to_s + "\n---" ) confirm?( prompt: "🔔 Really delete the prompt #{bold{prompt.name}}? (y/n) ", yes: /\Ay/i ) or return prompt.destroy end |
#choose_and_edit_prompt ⇒ self?
Interactively selects an existing prompt and allows the user to edit its content via the integrated editor.
117 118 119 120 121 122 |
# File 'lib/ollama_chat/prompt_management.rb', line 117 def choose_and_edit_prompt prompt = choose_prompt(prompt: 'Which spell needs some fine-tuning? ') or return prompt.['content'] = edit_text(prompt.['content'].to_s) prompt.save self end |
#choose_prompt(default: nil, prompt: 'Select a prompt template: ') ⇒ OllamaChat::Database::Models::Prompt?
The choose_prompt method presents a menu of available prompts for selection. It retrieves the list of prompt names from the database, adds an ‘[EXIT]’ option, and displays them via the Chooser utility.
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/ollama_chat/prompt_management.rb', line 33 def choose_prompt(default: nil, prompt: 'Select a prompt template: ') prompts = all_prompts(default: default) prompts.unshift('[EXIT]') case chosen = choose_entry(prompts, prompt:) when '[EXIT]', nil STDOUT.puts "Exiting chooser." return when SearchUI::Wrapper prompt(chosen.value) end end |
#duplicate_prompt ⇒ self?
Duplicates an existing prompt.
This method initiates an interactive workflow:
-
Prompts the user to select a 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.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/ollama_chat/prompt_management.rb', line 135 def duplicate_prompt prompt = choose_prompt(prompt: 'Which prompt shall be the basis for a new one? ') or return STDOUT.puts kramdown_ansi_parse( prompt.to_s + "\n---" ) name = nil loop do name = ask?( prompt: "❓ Enter new prompt name to duplicate as, C-c ⇒ cancel: " ) if name.nil? STDOUT.puts "Cancelled." return nil end if prompt(name) STDOUT.puts "Prompt named #{bold{name}} already exists." else break end end duplicated_prompt = prompt.duplicate duplicated_prompt.name = name duplicated_prompt.['default'] = false duplicated_prompt.save self end |
#export_prompt ⇒ self?
Interactively exports a prompt to a specified file.
The process follows these steps:
-
Prompts the user to select a prompt via ‘choose_prompt`.
-
Displays the prompt’s current content to the terminal.
-
Prompts for a destination filename via
`determine_valid_output_filename`.
-
Writes the prompt content to the chosen file.
206 207 208 209 210 211 212 213 214 215 |
# File 'lib/ollama_chat/prompt_management.rb', line 206 def export_prompt prompt = choose_prompt(prompt: 'Which template are you exporting to disk? ') 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_prompt(filename) ⇒ self?
Interactively imports a prompt from a file.
The process follows these steps:
-
Resolves the source file path (either using the provided filename or prompting the user to choose one).
-
Prompts for a unique name for the new prompt via ‘determine_valid_new_name_for_prompt`.
-
Reads the file content and stores it in the database.
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/ollama_chat/prompt_management.rb', line 174 def import_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 prompt_name = determine_valid_new_name_for_prompt('to import') or return prompt = filename.read store_prompt(prompt_name, prompt) STDOUT.puts "Imported prompt as #{prompt_name.inspect}." self end |
#info_prompt ⇒ self?
Displays detailed information about a selected prompt template.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ollama_chat/prompt_management.rb', line 48 def info_prompt if prompt = choose_prompt(prompt: 'Which blueprint would you like to inspect? ') use_pager do |output| output.puts kramdown_ansi_parse(<<~EOT) # Prompt #{prompt.name} --- #{prompt.to_s} --- EOT end end self end |
#list_prompts ⇒ Array
Lists all prompt templates in the database, indicating which are defaults and showing a truncated preview of their content.
262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
# File 'lib/ollama_chat/prompt_management.rb', line 262 def list_prompts favs = all_favourited('prompt') each_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 |
#prepare_conversation_history ⇒ String
Aggregates the current conversation history into a single string for context-aware generation.
Each message is formatted as “Sender Name: Message Content”, skipping messages that contain no content.
224 225 226 227 228 229 230 |
# File 'lib/ollama_chat/prompt_management.rb', line 224 def prepare_conversation_history ..inject('') do |result, | = .content.full? or next result sender_name = sender_name_displayed(, template: false) result << "%s: %s" % [ sender_name, ] end end |
#reset_prompt_to_default(name) ⇒ Boolean?
Resets a prompt’s content to the default value defined in the configuration.
281 282 283 284 285 286 |
# File 'lib/ollama_chat/prompt_management.rb', line 281 def reset_prompt_to_default(name) if content = config.prompts[name.to_s] store_prompt(name, content) true end end |
#suggest_prompts ⇒ String?
Interactively generates follow-up prompt suggestions based on the current session.
This method prompts the user to select a suggestion strategy (e.g., coding or roleplaying), constructs a prompt containing the conversation history, and requests a generation from the AI model. The resulting suggestions are then opened in the editor for final refinement before being returned.
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 |
# File 'lib/ollama_chat/prompt_management.rb', line 242 def suggest_prompts # Let the user pick a prompt template (e.g., suggest_coding, suggest_roleplaying) instruction = choose_prompt(prompt: 'Which suggestion strategy shall we employ? ') or return # Build the context by gathering all current conversation messages history = prepare_conversation_history full_prompt = "Conversation History:\n#{history}\n\n#{instruction}" # Execute a silent generation call (doesn't add to history) response = generate(prompt: full_prompt) suggestions = response.response # Pass the AI's suggestions through the editor for final refinement edit_text(suggestions) end |