Module: OllamaChat::PromptHandling

Included in:
Chat
Defined in:
lib/ollama_chat/prompt_handling.rb

Overview

Provides methods for retrieving and iterating over prompt templates stored in the database.

This module is designed to be mixed into the Chat class, allowing it to access prompt overrides stored in the database using the models helper.

Instance Method Summary collapse

Instance Method Details

#delete_prompt(name) ⇒ Boolean (private)

Deletes a prompt by name from the 'prompt' context if it is not a default prompt.

Parameters:

  • name (String, Symbol)

    the name of the prompt to delete

Returns:

  • (Boolean)

    true if deleted, false otherwise



51
52
53
54
55
56
57
# File 'lib/ollama_chat/prompt_handling.rb', line 51

def delete_prompt(name)
  if found = prompt(name) and !found.['default']
    found.destroy
    return true
  end
  false
end

#delete_system_prompt(name) ⇒ Boolean (private)

Deletes a system prompt by name from the 'system_prompt' context if it is not a default prompt.

Parameters:

  • name (String, Symbol)

    the name of the system prompt to delete

Returns:

  • (Boolean)

    true if deleted, false otherwise



74
75
76
77
78
79
80
# File 'lib/ollama_chat/prompt_handling.rb', line 74

def delete_system_prompt(name)
  if found = system_prompt(name) and !found.['default']
    found.destroy
    return true
  end
  false
end

#each_prompt(default: nil) {|prompt| ... } ⇒ Enumerator (private)

Iterates over all prompts in the 'prompt' context.

Yields:

  • (prompt)

    yields each prompt model instance

Returns:

  • (Enumerator)

    an enumerator if no block is given



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ollama_chat/prompt_handling.rb', line 32

def each_prompt(default: nil, &block)
  block or return enum_for(__method__, default:)
  prompts = models::Prompt.where(context: 'prompt')
  case default
  when true
    prompts = prompts.where(Sequel.lit("metadata ->> '$.default' = 1"))
  when false
    prompts = prompts.where(Sequel.lit(<<~SQL))
      metadata ->> '$.default' = 0 OR metadata ->> '$.default' IS NULL
    SQL
  end
  prompts.all.each(&block)
end

#each_system_prompt {|prompt| ... } ⇒ Enumerator (private)

Iterates over all prompts in the 'system_prompt' context.

Yields:

  • (prompt)

    yields each system prompt model instance

Returns:

  • (Enumerator)

    an enumerator if no block is given



96
97
98
99
100
# File 'lib/ollama_chat/prompt_handling.rb', line 96

def each_system_prompt(&block)
  block or return enum_for(__method__)

  models::Prompt.where(context: 'system_prompt').all.each(&block)
end

#load_prompt_from_file(patterns = nil) ⇒ String? (private)

Interactively selects a file based on patterns and reads its content.

Parameters:

  • patterns (String, Array<String>) (defaults to: nil)

    file patterns to filter the selection

Returns:

  • (String, nil)

    the content of the file or nil if no file was selected or doesn't exist



127
128
129
130
131
132
# File 'lib/ollama_chat/prompt_handling.rb', line 127

def load_prompt_from_file(patterns = nil)
  patterns = Array(patterns.full? || '**/*.{txt,md}')
  filename = choose_filename(patterns)

  filename.read if filename&.exist?
end

#prompt(name) ⇒ OllamaChat::Database::Models::Prompt? (private)

Retrieves a specific prompt by name from the 'prompt' context.

Parameters:

  • name (String, Symbol)

    the name of the prompt to retrieve

Returns:



24
25
26
# File 'lib/ollama_chat/prompt_handling.rb', line 24

def prompt(name)
  models::Prompt.where(context: 'prompt', name: name.to_s).first
end

#store_prompt(name, content) ⇒ OllamaChat::Database::Models::Prompt (private)

Stores a prompt in the 'prompt' context.

Parameters:

  • name (String, Symbol)

    the name of the prompt

  • content (String)

    the content of the prompt

Returns:



65
66
67
# File 'lib/ollama_chat/prompt_handling.rb', line 65

def store_prompt(name, content)
  write_prompt('prompt', name, content)
end

#store_system_prompt(name, content) ⇒ OllamaChat::Database::Models::Prompt (private)

Stores a system prompt in the 'system_prompt' context.

Parameters:

  • name (String, Symbol)

    the name of the system prompt

  • content (String)

    the content of the system prompt

Returns:



88
89
90
# File 'lib/ollama_chat/prompt_handling.rb', line 88

def store_system_prompt(name, content)
  write_prompt('system_prompt', name, content)
end

#system_prompt(name) ⇒ OllamaChat::Database::Models::Prompt?

Retrieves a specific system prompt by name from the 'system_prompt' context.

Parameters:

  • name (String, Symbol)

    the name of the system prompt to retrieve

Returns:



13
14
15
# File 'lib/ollama_chat/prompt_handling.rb', line 13

def system_prompt(name)
  models::Prompt.where(context: 'system_prompt', name: name.to_s).first
end

#write_prompt(context, name, content) ⇒ OllamaChat::Database::Models::Prompt (private)

Creates or updates a prompt in the specified context.

Parameters:

  • context (String)

    the context (e.g., 'prompt' or 'system_prompt')

  • name (String)

    the name of the prompt

  • content (String)

    the content of the prompt

Returns:



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/ollama_chat/prompt_handling.rb', line 109

def write_prompt(context, name, content)
  obj = nil
  if found = models::Prompt.where(context:, name:).first
    found.['content'] = content
    obj = found
  else
    obj = models::Prompt.create(name:, context:)
    obj. = { default: false, content: }.stringify_keys_recursive
  end
  obj.tap(&:save)
end