Class: OllamaChat::Database::Models::Prompt
- Inherits:
-
Object
- Object
- OllamaChat::Database::Models::Prompt
- Includes:
- Duplicatable
- Defined in:
- lib/ollama_chat/database/models/prompt.rb
Overview
Represents a prompt template stored in the database, allowing for dynamic overrides of default configuration prompts.
This model stores prompts with a context (e.g., 'prompt' or 'system_prompt') and a name, with the actual content residing in a serialized JSON metadata column.
Class Method Summary collapse
-
.seed(chat) ⇒ Object
Seeds the prompt table from the provided chat configuration.
Instance Method Summary collapse
-
#after_destroy ⇒ Object
Hook to clean up associated favourites when a prompt is destroyed.
-
#context(value) ⇒ String
The context of the prompt (e.g., 'prompt' or 'system_prompt').
-
#created_at(value) ⇒ Time?
The timestamp when the prompt was created.
-
#id(value) ⇒ Integer
The primary key for the prompt entry.
-
#metadata(value) ⇒ Hash?
A JSON-serialized hash containing prompt metadata, including the actual content.
-
#name(value) ⇒ String
The name of the prompt.
-
#to_s ⇒ String
Returns the actual prompt text stored within the metadata JSON.
-
#updated_at(value) ⇒ Time?
The timestamp of the last update to the prompt.
-
#validate ⇒ Object
Validates the prompt template.
Methods included from Duplicatable
Class Method Details
.seed(chat) ⇒ Object
Seeds the prompt table from the provided chat configuration.
This method iterates through both general and system prompts in the chat configuration, ensuring that every default prompt has a corresponding record in the database for later override.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ollama_chat/database/models/prompt.rb', line 67 def self.seed(chat) chat.config.prompts.each do |name, content| where( context: 'prompt', name: name.to_s, ).first and next create( context: 'prompt', name: name.to_s, metadata: { default: true, content: }.stringify_keys_recursive ) end chat.config.system_prompts.each do |name, content| where( context: 'system_prompt', name: name.to_s, ).first and next create( context: 'system_prompt', name: name.to_s, metadata: { default: true, content: }.stringify_keys_recursive ) end end |
Instance Method Details
#after_destroy ⇒ Object
Hook to clean up associated favourites when a prompt is destroyed.
This ensures that we don't leave orphaned favourite entries in the database when the underlying prompt is removed.
53 54 55 56 57 58 |
# File 'lib/ollama_chat/database/models/prompt.rb', line 53 def after_destroy super OllamaChat::Database::Models::Favourite. where(context: context, name: name). destroy end |
#context=(value) ⇒ String
Returns The context of the prompt (e.g., 'prompt' or 'system_prompt').
|
|
# File 'lib/ollama_chat/database/models/prompt.rb', line 23
|
#created_at=(value) ⇒ Time?
Returns The timestamp when the prompt was created.
|
|
# File 'lib/ollama_chat/database/models/prompt.rb', line 23
|
#id=(value) ⇒ Integer
Returns The primary key for the prompt entry.
|
|
# File 'lib/ollama_chat/database/models/prompt.rb', line 23
|
#metadata=(value) ⇒ Hash?
Returns A JSON-serialized hash containing prompt metadata, including the actual content.
|
|
# File 'lib/ollama_chat/database/models/prompt.rb', line 23
|
#name=(value) ⇒ String
Returns The name of the prompt.
|
|
# File 'lib/ollama_chat/database/models/prompt.rb', line 23
|
#to_s ⇒ String
Returns the actual prompt text stored within the metadata JSON.
45 46 47 |
# File 'lib/ollama_chat/database/models/prompt.rb', line 45 def to_s ['content'].to_s end |
#updated_at=(value) ⇒ Time?
Returns The timestamp of the last update to the prompt.
|
|
# File 'lib/ollama_chat/database/models/prompt.rb', line 23
|
#validate ⇒ Object
Validates the prompt template.
Ensures that both the context and name are present.
16 17 18 19 20 21 |
# File 'lib/ollama_chat/database/models/prompt.rb', line 16 def validate super validates_presence :context validates_presence :name validates_unique %i[ context name ] end |