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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Attributes included from ToolCalling

#tool_call_results

Attributes included from ServerSocket

#server_socket_message

Attributes included from Switches

#embedding, #embedding_enabled, #embedding_paused, #location, #markdown, #stream, #think_loud, #voice

Instance Method Summary collapse

Methods included from ToolCalling

#configured_tools, #default_enabled_tools, #disable_tool, #enable_tool, #list_tools, #tools

Methods included from LocationHandling

#location_data, #location_description

Methods included from MessageEditing

#revise_last

Methods included from InputContent

#choose_filename, #compose, #context_spook, #input

Methods included from Conversation

#load_conversation, #save_conversation

Methods included from KramdownANSI

#configure_kramdown_ansi_styles, #kramdown_ansi_parse

Methods included from ServerSocket

create_socket_server, #init_server_socket, send_to_server_socket

Methods included from History

#chat_history_filename, #clear_history, #init_chat_history, #save_history

Methods included from MessageFormat

#message_type, #talk_annotate, #think_annotate

Methods included from Clipboard

#copy_to_clipboard, #paste_from_input

Methods included from MessageOutput

#output, #pipe

Methods included from Information

#collection_stats, #display_chat_help, #info, #server_url, #server_version, #usage, #version

Methods included from ThinkControl

#think, #think?, #think_loud?

Methods included from Dialog

#ask?, #change_system_prompt, #change_voice, #choose_collection, #choose_model, #choose_prompt, #connect_message, #message_list

Methods included from WebSearching

#search_web

Methods included from SourceFetching

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

Methods included from Parsing

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

Methods included from Utils::AnalyzeDirectory

#generate_structure

Methods included from ModelHandling

#model_present?, #pull_model_from_remote, #pull_model_unless_present

Methods included from StateSelectors

#setup_state_selectors

Methods included from Switches

#setup_switches

Methods included from DocumentCache

#configure_cache, #document_cache_class

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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ollama_chat/chat.rb', line 86

def initialize(argv: ARGV.dup)
  @opts               = go 'f:u:m:s:c:C:D:MESVh', argv
  @opts[?h] and exit usage
  @opts[?V] and exit version
  @messages           = OllamaChat::MessageList.new(self)
  @ollama_chat_config = OllamaChat::OllamaChatConfig.new(@opts[?f])
  self.config         = @ollama_chat_config.config
  setup_switches(config)
  setup_state_selectors(config)
  connect_ollama
  @model           = choose_model(@opts[?m], config.model.name)
  @model_options   = Ollama::Options[config.model.options]
  @model_system    = pull_model_unless_present(@model, @model_options)
  if @opts[?c]
    messages.load_conversation(@opts[?c])
  else
    setup_system_prompt
  end
  embedding_enabled.set(config.embedding.enabled && !@opts[?E])
  @documents            = setup_documents
  @cache                = setup_cache
  @images               = []
  @kramdown_ansi_styles = configure_kramdown_ansi_styles
  @enabled_tools        = default_enabled_tools
  @tool_call_results    = {}
  init_chat_history
  @opts[?S] and init_server_socket
rescue ComplexConfig::AttributeMissing, ComplexConfig::ConfigurationSyntaxError => e
  fix_config(e)
end

Class Attribute Details

.configObject

The config attribute accessor provides read and write access to the configuration object associated with this instance.



174
175
176
# File 'lib/ollama_chat/chat.rb', line 174

def config
  @config
end

Instance Attribute Details

#document_policyOllamaChat::StateSelector (readonly)

The document_policy reader returns the document policy selector for the chat session.

Returns:

  • (OllamaChat::StateSelector)

    the document policy selector object that manages the policy for handling document references in user text



121
122
123
# File 'lib/ollama_chat/chat.rb', line 121

def document_policy
  @document_policy
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



149
150
151
# File 'lib/ollama_chat/chat.rb', line 149

def documents
  @documents
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:



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

def messages
  @messages
end

#ollamaOllama::Client (readonly)

The ollama reader returns the Ollama API client instance.

Returns:

  • (Ollama::Client)

    the configured Ollama API client



139
140
141
# File 'lib/ollama_chat/chat.rb', line 139

def ollama
  @ollama
end

#think_modeOllamaChat::StateSelector (readonly)

The think_mode reader returns the think mode selector for the chat session.

Returns:

  • (OllamaChat::StateSelector)

    the think mode selector object that manages the thinking mode setting for the Ollama model interactions



127
128
129
# File 'lib/ollama_chat/chat.rb', line 127

def think_mode
  @think_mode
end

Instance Method Details

#configComplexConfig::Settings

The config method returns the configuration object associated with the class.

Returns:

  • (ComplexConfig::Settings)

    the configuration instance



188
189
190
# File 'lib/ollama_chat/chat.rb', line 188

def config
  self.class.config
end

#config=(config) ⇒ Object

The config= method assigns a new configuration object to the class.

Parameters:

  • config (ComplexConfig::Settings)

    the configuration object to be set



180
181
182
# File 'lib/ollama_chat/chat.rb', line 180

def config=(config)
  self.class.config = config
end

#debugTrueClass, FalseClass

The debug method accesses the debug configuration setting.

Returns:

  • (TrueClass, FalseClass)

    the current debug mode status



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

def debug
  OllamaChat::EnvConfig::OLLAMA::CHAT::DEBUG
end

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

The links set is memoized, meaning it will only be created once per object instance and subsequent calls will return the same Set instance.

Returns:

  • (Set)

    A Set object containing all links associated with this instance



167
168
169
# File 'lib/ollama_chat/chat.rb', line 167

def links
  @links ||= Set.new
end

#startObject

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



195
196
197
198
199
200
201
202
203
# File 'lib/ollama_chat/chat.rb', line 195

def start
  info
  if messages.size > 1
    messages.list_conversation(2)
  end
  STDOUT.puts "\nType /help to display the chat help."

  interact_with_user
end

#vim(server_name = nil) ⇒ OllamaChat::Vim

The vim method creates and returns a new Vim instance for interacting with a Vim server.

This method initializes a Vim client that can be used to insert text into Vim buffers or open files in a running Vim server. It derives the server name from the provided argument or uses a default server name based on the current working directory.

Parameters:

  • server_name (String, nil) (defaults to: nil)

    the name of the Vim server to connect to If nil or empty, a default server name is derived from the current working directory

Returns:

  • (OllamaChat::Vim)

    a new Vim instance configured with the specified server name



219
220
221
222
# File 'lib/ollama_chat/chat.rb', line 219

def vim(server_name = nil)
  clientserver = config.vim?&.clientserver
  OllamaChat::Vim.new(server_name, clientserver:)
end