Module: OllamaChat::Dialog

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#document_policy=(value) ⇒ Object (writeonly)

The document_policy method sets the policy for handling document imports.

Parameters:

  • value (String)

    the document policy to be set



82
83
84
# File 'lib/ollama_chat/dialog.rb', line 82

def document_policy=(value)
  @document_policy = value
end

Instance Method Details

#ask?(prompt:) ⇒ String

The ask? method prompts the user with a question and returns their input.

Parameters:

  • prompt (String)

    the message to display to the user

Returns:

  • (String)

    the user’s response with trailing newline removed



49
50
51
52
# File 'lib/ollama_chat/dialog.rb', line 49

def ask?(prompt:)
  print prompt
  STDIN.gets.chomp
end

#change_system_prompt(default, system: nil) ⇒ 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.

search for

Parameters:

  • default (String)

    the default system prompt to fall back to

  • system (String) (defaults to: nil)

    the system prompt identifier or pattern to



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/ollama_chat/dialog.rb', line 123

def change_system_prompt(default, system: nil)
  selector = if system =~ /\A\?(.+)\z/
               Regexp.new($1)
             else
               Regexp.new(system.to_s)
             end
  prompts = config.system_prompts.attribute_names.compact.grep(selector).sort
  if prompts.size == 1
    system = config.system_prompts.send(prompts.first)
  else
    prompts.unshift('[EXIT]').unshift('[NEW]')
    chosen = OllamaChat::Utils::Chooser.choose(prompts)
    system =
      case chosen
      when '[NEW]'
        ask?(prompt: "Enter new system prompt to use: ")
      when '[EXIT]'
        STDOUT.puts "Exiting chooser."
        return
      when nil
        default
      when *prompts
        config.system_prompts.send(chosen)
      else
        default
      end
  end
  @messages.set_system_prompt(system)
end

#change_voiceString

The change_voice method allows the user to select a voice from a list of available options. It uses the chooser to present the options and sets the selected voice as the current voice.

Returns:

  • (String)

    the full name of the chosen voice



175
176
177
178
# File 'lib/ollama_chat/dialog.rb', line 175

def change_voice
  chosen  = OllamaChat::Utils::Chooser.choose(config.voice.list)
  @current_voice = chosen.full? || config.voice.default
end

#choose_collection(current_collection) ⇒ Object

The choose_collection method presents a menu to select or create a document collection. It displays existing collections along with options to create a new one or exit. The method prompts the user for input and updates the document collection accordingly.

Parameters:

  • current_collection (String, nil)

    the name of the currently active collection



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ollama_chat/dialog.rb', line 61

def choose_collection(current_collection)
  collections = [ current_collection ] + @documents.collections
  collections = collections.compact.map(&:to_s).uniq.sort
  collections.unshift('[EXIT]').unshift('[NEW]')
  collection = OllamaChat::Utils::Chooser.choose(collections) || current_collection
  case collection
  when '[NEW]'
    @documents.collection = ask?(prompt: "Enter name of the new collection: ")
  when nil, '[EXIT]'
    STDOUT.puts "Exiting chooser."
  when /./
    @documents.collection = collection
  end
ensure
  STDOUT.puts "Using collection #{bold{@documents.collection}}."
  info
end

#choose_document_policyObject

The choose_document_policy method presents a menu to select a document policy. It allows the user to choose from importing, embedding, summarizing, or ignoring documents. The method displays available policies and sets the selected policy as the current document policy. If no valid policy is found, it defaults to the first option. After selection, it outputs the chosen policy and displays the current configuration information.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ollama_chat/dialog.rb', line 92

def choose_document_policy
  policies = %w[ importing embedding summarizing ignoring ].sort
  current  = if policies.index(@document_policy)
               @document_policy
             elsif policies.index(config.document_policy)
               config.document_policy
             else
               policies.first
             end
  policies.unshift('[EXIT]')
  policy = OllamaChat::Utils::Chooser.choose(policies)
  case policy
  when nil, '[EXIT]'
    STDOUT.puts "Exiting chooser."
    policy = current
  end
  self.document_policy = policy
ensure
  STDOUT.puts "Using document policy #{bold{@document_policy}}."
  info
end

#choose_model(cli_model, current_model) ⇒ Object

The choose_model method selects a model from the available list based on CLI input or user interaction. It processes the provided CLI model parameter to determine if a regex selector is used, filters the models accordingly, and prompts the user to choose from the filtered list if needed. The method ensures that a model is selected and displays a connection message with the chosen model and base URL.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ollama_chat/dialog.rb', line 28

def choose_model(cli_model, current_model)
  selector = if cli_model =~ /\A\?+(.*)\z/
               cli_model = ''
               Regexp.new($1)
             end
  models = ollama.tags.models.sort_by(&:name).map { |m| model_with_size(m) }
  selector and models = models.grep(selector)
  model = if cli_model == ''
            OllamaChat::Utils::Chooser.choose(models) || current_model
          else
            cli_model || current_model
          end
ensure
  STDOUT.puts green { "Connecting to #{model}@#{ollama.base_url} now…" }
end

#choose_promptObject

The choose_prompt method presents a menu of available prompts for selection. It retrieves the list of prompt attributes from the configuration, adds an ‘[EXIT]’ option to the list, and displays it to the user. After the user makes a choice, the method either exits the chooser or applies the selected prompt configuration.



158
159
160
161
162
163
164
165
166
167
168
# File 'lib/ollama_chat/dialog.rb', line 158

def choose_prompt
  prompts = config.prompts.attribute_names.sort
  prompts.unshift('[EXIT]')
  case chosen = OllamaChat::Utils::Chooser.choose(prompts)
  when '[EXIT]', nil
    STDOUT.puts "Exiting chooser."
    return
  when *prompts
    config.prompts.send(chosen)
  end
end

#message_listMessageList

The message_list method creates and returns a new MessageList instance initialized with the current object as its argument.

Returns:



184
185
186
# File 'lib/ollama_chat/dialog.rb', line 184

def message_list
  MessageList.new(self)
end