Class: OllamaChat::Database::Models::Prompt

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Methods included from Duplicatable

#duplicate

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.

Parameters:



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_destroyObject

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’).

Returns:

  • (String)

    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.

Returns:

  • (Time, nil)

    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.

Returns:

  • (Integer)

    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.

Returns:

  • (Hash, nil)

    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.

Returns:

  • (String)

    The name of the prompt.



# File 'lib/ollama_chat/database/models/prompt.rb', line 23

#to_sString

Returns the actual prompt text stored within the metadata JSON.

Returns:

  • (String)

    the prompt content



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.

Returns:

  • (Time, nil)

    The timestamp of the last update to the prompt.



# File 'lib/ollama_chat/database/models/prompt.rb', line 23

#validateObject

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