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, context: nil) ⇒ Boolean

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



41
42
43
44
45
46
47
48
# File 'lib/ollama_chat/prompt_handling.rb', line 41

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

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

Iterates over all prompts in the 'prompt' context.

Yields:

  • (prompt)

    yields each prompt model instance

Returns:

  • (Enumerator)

    an enumerator if no block is given



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ollama_chat/prompt_handling.rb', line 21

def each_prompt(context: nil, default: nil, &block)
  context ||= 'prompt'
  block or return enum_for(__method__, context:, default:)
  prompts = models::Prompt.where(context:)
  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

#load_prompt_from_file(patterns = nil) ⇒ String?

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



87
88
89
90
91
92
93
94
95
96
# File 'lib/ollama_chat/prompt_handling.rb', line 87

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

  content = filename.read if filename&.exist?
  log(:info, "Prompt loaded from file", data: {
    file: filename&.to_s, bytes: content&.size
  })
  content
end

#prompt(name, context: nil) ⇒ OllamaChat::Database::Models::Prompt?

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

Parameters:

  • name (String, Symbol)

    the name of the prompt to retrieve

Returns:



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

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

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

Stores a prompt in the 'prompt' context.

Parameters:

  • name (String, Symbol)

    the name of the prompt

  • content (String)

    the content of the prompt

Returns:



56
57
58
59
# File 'lib/ollama_chat/prompt_handling.rb', line 56

def store_prompt(name, content, context: nil)
  context ||= 'prompt'
  write_prompt(name, content, context:)
end

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

Creates or updates a prompt in the specified context.

Parameters:

  • context (String) (defaults to: nil)

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

  • name (String)

    the name of the prompt

  • content (String)

    the content of the prompt

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ollama_chat/prompt_handling.rb', line 68

def write_prompt(name, content, context: nil)
  context ||= 'prompt'
  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