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
-
#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.
-
#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.
-
#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.
-
#refresh_system_prompt ⇒ Object
Re-interpolates the current system prompt with fresh dynamic values (persona content, persona name, runtime info).
-
#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.
-
#system_prompt_with_favourite(name, favourited) ⇒ SearchUI::Wrapper
private
Helper to wrap a system prompt name with its favourite status for the UI.
Instance Method Details
#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') each_prompt(context: 'system').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.
118 119 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/system_prompt_management.rb', line 118 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]' feedback("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 |
#current_system_prompt ⇒ String?
Retrieves the content of the system prompt currently active in the chat session.
82 83 84 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 82 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 |
#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.
68 69 70 71 72 73 74 75 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 68 def raw_system_prompt system_name = current_system_prompt_name if system_name == 'model_default' model_default_system_prompt.to_s else prompt(system_name, context: 'system').to_s end.named_placeholders_interpolate({}) end |
#refresh_system_prompt ⇒ Object
Re-interpolates the current system prompt with fresh dynamic values (persona content, persona name, runtime info).
Call this after any operation that mutates the sources baked into the system prompt, such as switching personas or toggling runtime info.
Safe to call when no system prompt is set; returns immediately.
52 53 54 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 52 def refresh_system_prompt .set_system_prompt(.system_name) if .system_name 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.
94 95 96 97 98 99 100 101 102 103 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 94 def setup_system_prompt system_prompt_name = session.current_system_prompt.full? || ('default' if prompt(:default, context: 'system')) || 'model_default' if system_prompt_name.full? set_current_system_prompt(system_prompt_name) else change_system_prompt(system_prompt_name) end end |
#system_prompt_with_favourite(name, favourited) ⇒ SearchUI::Wrapper (private)
Helper to wrap a system prompt name with its favourite status for the UI.
151 152 153 154 |
# File 'lib/ollama_chat/system_prompt_management.rb', line 151 def system_prompt_with_favourite(name, favourited) display = prefix_favourite(name, favourited) SearchUI::Wrapper.new(name, display:) end |