Class: OllamaChat::Chat

Overview

A chat client for interacting with Ollama models through a terminal interface.

The Chat class provides a complete command-line interface for chatting with language models via the Ollama API. It handles configuration management, message history, document processing, web searching, and various interactive features including voice output, markdown rendering, and embedding capabilities.

Examples:

Initializing a chat session

chat = OllamaChat::Chat.new(argv: ['-m', 'llama3.1'])

Starting an interactive chat

chat.start

Constant Summary

Constants included from ThinkControl

ThinkControl::THINK_MODE_STATES

Constants included from Parsing

Parsing::DOCUMENT_POLICY_STATES

Instance Attribute Summary collapse

Attributes included from Utils::Chooser

#current_search_state Stores the

Attributes included from ToolCalling

#tool_call_results

Attributes included from StateSelectors

#document_policy, #think_mode, #voices

Attributes included from Switches

#embedding, #embedding_enabled, #embedding_paused, #location, #markdown, #runtime_info, #stream, #think_loud, #think_strip, #tools_support, #voice

Instance Method Summary collapse

Methods included from Utils::ValueFormatter

#format_bytes, #format_tokens

Methods included from Utils::Chooser

#choose_entry, #choose_with_state

Methods included from PromptManagement

#add_new_prompt, #all_prompts, #choose_and_delete_prompt, #choose_and_edit_prompt, #choose_prompt, #duplicate_prompt, #export_prompt, #import_prompt, #info_prompt, #list_prompts, #reset_prompt_to_default

Methods included from SystemPromptManagement

#add_new_system_prompt, #all_system_prompts, #change_system_prompt, #choose_and_delete_system_prompt, #choose_and_edit_system_prompt, #choose_system_prompt, #current_system_prompt, #current_system_prompt_name, #duplicate_system_prompt, #export_system_prompt, #import_system_prompt, #info_system_prompt, #list_system_prompts, #model_default_system_prompt, #reset_system_prompt_to_default, #set_current_system_prompt, #setup_system_prompt

Methods included from PromptHandling

#system_prompt

Methods included from SessionManagement

#load_links_from_session, #store_links_in_session, #store_messages_in_session

Methods included from PersonaeManagement

#assistant, #default_persona_profile, #initial_persona_name

Methods included from ToolCalling

#configured_tools, #default_enabled_tools, #disable_tool, #enable_tool, #enabled_tools, #list_tools, #tool_configured?, #tool_enabled?, #tool_function, #tool_paths_allowed, #tool_registered?, #tools

Methods included from LocationHandling

#location_data, #location_description, #location_description?

Methods included from InputContent

#file_set_each

Methods included from FileEditing

#determine_valid_output_filename, #edit_file, #edit_text, #edit_text_block, #perform_insert, #vim

Methods included from KramdownANSI

#configure_kramdown_ansi_styles, #kramdown_ansi_parse

Methods included from ServerSocket

create_socket_server, send_to_server_socket

Methods included from Pager

#use_pager

Methods included from MessageFormat

#chat, #display_sender, #message_type, #role_color, #role_template, #sender_name_displayed, #talk_annotate, #think_annotate

Methods included from Clipboard

#perform_copy_to_clipboard, #perform_paste_from_clipboard

Methods included from MessageOutput

#output, #pipe

Methods included from Information

#client, #collection_stats, #display_chat_help, #dynamic_runtime_information, #dynamic_runtime_information_values, #info, #info_model, #info_rag, #info_runtime, #info_session, #infobar_message, #server_url, #server_version, #static_runtime_information, #static_runtime_information_values, #usage, #user, #user_name, #version

Methods included from ThinkControl

#think, #think?, #think_loud?

Methods included from Dialog

#ask?, #confirm?

Methods included from WebSearching

#manage_links, #search_web, #web

Methods included from SourceFetching

#add_image, #embed, #embed_source, #fetch_source, #import, #import_source, #summarize, #summarize_source

Methods included from Parsing

#parse_atom, #parse_character_json, #parse_content, #parse_rss, #parse_source, #pdf_read, #ps_read, #reverse_markdown

Methods included from Utils::AnalyzeDirectory

#generate_structure

Methods included from ModelHandling

#get_stored_model_options

Methods included from StateSelectors

#setup_state_selectors

Methods included from Switches

#setup_switches

Methods included from Logging

#log, #logger

Methods included from HTTPHandling

#get_url, #http_options, #links

Constructor Details

#initialize(argv: ARGV.dup) ⇒ Chat

Initializes a new OllamaChat::Chat instance with the given command-line arguments.

Sets up the chat environment including configuration parsing, Ollama client initialization, model selection, system prompt handling, document processing setup, and history management. This method handles all the bootstrapping necessary to create a functional chat session that can communicate with an Ollama server and process various input types including text, documents, web content, and images.

The initialization process includes parsing command-line options using Tins::GO for robust argument handling, setting up the Ollama client with configurable timeouts (connect, read, write), validating Ollama API version compatibility (requires >= 0.9.0 for features used), configuring model selection based on command-line or configuration defaults, initializing system prompts from files or inline content, setting up document processing pipeline with embedding capabilities through Documentrix::Documents, creating message history management through OllamaChat::MessageList, initializing cache systems for document embeddings, setting up voice support and image handling for multimodal interactions, enabling optional server socket functionality for remote input, and handling configuration errors with interactive recovery mechanisms.

Parameters:

  • argv (Array<String>) (defaults to: ARGV.dup)

    Command-line arguments to parse (defaults to ARGV.dup)

Raises:

  • (RuntimeError)

    If the Ollama API version is less than 0.9.0, indicating incompatibility with required API features



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ollama_chat/chat.rb', line 100

def initialize(argv: ARGV.dup)
  @opts               = go 'f:u:m:c:C:D:l:nMESVh', argv
  @opts[?h] and exit usage
  @opts[?V] and exit version
  @ollama_chat_config = OllamaChat::OllamaChatConfig.new(@opts[?f])
  self.config         = @ollama_chat_config.config
  @messages           = OllamaChat::MessageList.new(self)
  OllamaChat::Database.setup_models.each { _1.ask_and_send(:seed, self) }
  setup_session
  setup_switches
  setup_state_selectors(config)
  connect_ollama
  if conversation_file = @opts[?c]
    messages.load_conversation(conversation_file)
  else
    messages.read_conversation_jsonl(session.messages.to_s)
  end
  embedding_enabled.set(config.embedding.enabled && !@opts[?E])
  @documents            = setup_documents
  @cache                = setup_cache
  @images               = []
  @kramdown_ansi_styles = configure_kramdown_ansi_styles
  @tool_call_results    = Hash.new { |h, name| h[name] = [] }
  setup_personae_directory
  @opts[?S] and init_server_socket
rescue ComplexConfig::AttributeMissing, ComplexConfig::ConfigurationSyntaxError => e
  fix_config(e)
end

Instance Attribute Details

#cacheObject (readonly)

Provides read-only access to the cache instance used by the object



163
164
165
# File 'lib/ollama_chat/chat.rb', line 163

def cache
  @cache
end

#documentsDocumentrix::Documents (readonly)

Returns the documents set for this object, initializing it lazily if needed.

The documents set is memoized, meaning it will only be created once per object instance and subsequent calls will return the same Documentrix::Documents instance.

Returns:

  • (Documentrix::Documents)

    A Documentrix::Documents object containing all documents associated with this instance



143
144
145
# File 'lib/ollama_chat/chat.rb', line 143

def documents
  @documents
end

#imagesArray (readonly)

Returns the list of images currently queued for the next message.

Returns:

  • (Array)

    a list of images to be sent with the next prompt



158
159
160
# File 'lib/ollama_chat/chat.rb', line 158

def images
  @images
end

#messagesOllamaChat::MessageList (readonly)

Returns the messages set for this object, initializing it lazily if needed.

The messages set is memoized, meaning it will only be created once per object instance and subsequent calls will return the same OllamaChat::MessageList instance.

Returns:



153
154
155
# File 'lib/ollama_chat/chat.rb', line 153

def messages
  @messages
end

#ollamaOllama::Client (readonly)

The ollama reader returns the Ollama API client instance.

Returns:

  • (Ollama::Client)

    the configured Ollama API client



132
133
134
# File 'lib/ollama_chat/chat.rb', line 132

def ollama
  @ollama
end

Instance Method Details

#debugTrueClass, FalseClass

The debug method accesses the debug configuration setting.

Returns:

  • (TrueClass, FalseClass)

    the current debug mode status



193
194
195
# File 'lib/ollama_chat/chat.rb', line 193

def debug
  OC::OLLAMA::CHAT::DEBUG
end

#initial_collectionSymbol

The initial_collection method determines the collection name to be used for embeddings in the RAG system.

Returns:

  • (Symbol)

    the collection name symbol



213
214
215
216
217
218
219
220
# File 'lib/ollama_chat/chat.rb', line 213

def initial_collection
  (
    @opts[?C] ||
    session&.current_collection.full? ||
    config.embedding.collection.full? ||
    :default
  ).to_sym
end

#initial_modelString

Returns the model name to be used for the chat session.

The resolution priority is:

  1. The current session’s model (if present).

  2. The model specified via the command line option ‘-m`.

  3. The default model name defined in the configuration.

Returns:

  • (String)

    the model name to be used for the chat session



205
206
207
# File 'lib/ollama_chat/chat.rb', line 205

def initial_model
  session&.current_model.full? || @opts[?m].full? || config.model.name
end

#initial_system_promptString

The initial_system_prompt method returns the system prompt for the initial message.

Returns:

  • (String)

    the system prompt for the initial message



226
227
228
# File 'lib/ollama_chat/chat.rb', line 226

def initial_system_prompt
  @messages.system_name
end

#startObject

The start method initializes the chat session by displaying information, then prompts the user for input to begin interacting with the chat.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ollama_chat/chat.rb', line 167

def start
  begin
    if model = session.current_model.full?
      use_model(model, keep_options: true)
    else
      use_model(initial_model)
    end
  rescue OllamaChat::UnknownModelError => e
    abort "Failed to use to model: #{e}"
  end

  STDOUT.puts

  setup_persona_from_session
  setup_system_prompt

  info_session

  STDOUT.puts "\nType /help to display the chat help."

  interact_with_user
end