Module: OllamaChat::Dialog

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

Overview

A module that provides interactive selection and configuration functionality for OllamaChat.

The Dialog module encapsulates various helper methods for choosing models, system prompts, document policies, and voices, as well as displaying information and managing chat sessions. It leverages user interaction components like choosers and prompts to enable dynamic configuration during runtime.

Examples:

Selecting a model from available options

chat.choose_model('-m llama3.1', 'llama3.1')

Changing the system prompt

chat.change_system_prompt('default_prompt', system: '?sherlock')

Choosing a document policy

chat.choose_document_policy

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



102
103
104
# File 'lib/ollama_chat/dialog.rb', line 102

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



69
70
71
72
# File 'lib/ollama_chat/dialog.rb', line 69

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



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/ollama_chat/dialog.rb', line 143

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



195
196
197
198
# File 'lib/ollama_chat/dialog.rb', line 195

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



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ollama_chat/dialog.rb', line 81

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.



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/ollama_chat/dialog.rb', line 112

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.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ollama_chat/dialog.rb', line 45

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 models.size == 1
      models.first
    elsif 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.



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ollama_chat/dialog.rb', line 178

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:



204
205
206
# File 'lib/ollama_chat/dialog.rb', line 204

def message_list
  MessageList.new(self)
end