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')

Instance Method Summary collapse

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



74
75
76
77
# File 'lib/ollama_chat/dialog.rb', line 74

def ask?(prompt:)
  print prompt
  STDIN.gets.to_s.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.

Parameters:

  • default (String)

    the default system prompt to fall back to

  • system (String) (defaults to: nil)

    the system prompt identifier or pattern to search for



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ollama_chat/dialog.rb', line 113

def change_system_prompt(default, system: nil)
  selector = case system
             when /\A\?(.+)\z/
               Regexp.new($1)
             when ??
               /./
             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('[NEW]').unshift('[EXIT]')
    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



168
169
170
# File 'lib/ollama_chat/dialog.rb', line 168

def change_voice
  @voices.choose
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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ollama_chat/dialog.rb', line 86

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_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.



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

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
  connect_message(model, ollama.base_url)
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.



151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ollama_chat/dialog.rb', line 151

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

#connect_message(model, base_url) ⇒ Object

The connect_message method displays a connection status message.

Parameters:

  • model (String)

    the model name to connect to

  • base_url (String)

    the base URL of the connection



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

def connect_message(model, base_url)
  STDOUT.puts green { "Connecting to #{model}@#{base_url} now…" }
end

#message_listMessageList

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

Returns:



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

def message_list
  MessageList.new(self)
end

#model_with_size(model) ⇒ Object (private)

The model_with_size method formats a model’s size for display by creating a formatted string that includes the model name and its size in a human-readable format with appropriate units.

Parameters:

  • model (Object)

    the model object that has name and size attributes

Returns:

  • (Object)

    a result object with an overridden to_s method that combines the model name and formatted size



24
25
26
27
28
29
30
31
32
33
# File 'lib/ollama_chat/dialog.rb', line 24

private def model_with_size(model)
  result = model.name
  formatted_size = Term::ANSIColor.bold {
    Tins::Unit.format(model.size, unit: ?B, prefix: 1024, format: '%.1f %U')
  }
  result.singleton_class.class_eval do
    define_method(:to_s) { "%s %s" % [ model.name, formatted_size ] }
  end
  result
end