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

Instance Attribute Summary collapse

Attributes included from ToolCalling

#enabled_tools, #tool_call_results

Attributes included from Switches

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

Instance Method Summary collapse

Methods included from ConfigHandling

#config

Methods included from ToolCalling

#configured_tools, #default_enabled_tools, #disable_tool, #enable_tool, #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 FileEditing

#edit_file, #edit_text, #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

#message_type, #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, #info, #server_url, #server_version, #usage, #version

Methods included from ThinkControl

#think, #think?, #think_loud?

Methods included from Dialog

#ask?

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 StateSelectors

#setup_state_selectors

Methods included from Switches

#setup_switches

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



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
116
117
118
119
# File 'lib/ollama_chat/chat.rb', line 89

def initialize(argv: ARGV.dup)
  @opts               = go 'f:u:m:s:p: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
  if conversation_file = @opts[?c]
    messages.load_conversation(conversation_file)
  elsif backup_file = OC::XDG_CACHE_HOME + 'backup.json' and backup_file.exist?
    messages.load_conversation(backup_file)
    FileUtils.rm_f backup_file
  else
    @setup_system_prompt = true
  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
  setup_personae_directory
  @opts[?S] and init_server_socket
rescue ComplexConfig::AttributeMissing, ComplexConfig::ConfigurationSyntaxError => e
  fix_config(e)
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



125
126
127
# File 'lib/ollama_chat/chat.rb', line 125

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



146
147
148
# File 'lib/ollama_chat/chat.rb', line 146

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:



156
157
158
# File 'lib/ollama_chat/chat.rb', line 156

def messages
  @messages
end

#ollamaOllama::Client (readonly)

The ollama reader returns the Ollama API client instance.

Returns:

  • (Ollama::Client)

    the configured Ollama API client



136
137
138
# File 'lib/ollama_chat/chat.rb', line 136

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



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

def think_mode
  @think_mode
end

Instance Method Details

#debugTrueClass, FalseClass

The debug method accesses the debug configuration setting.

Returns:

  • (TrueClass, FalseClass)

    the current debug mode status



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

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

#startObject

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/ollama_chat/chat.rb', line 160

def start
  begin
    use_model(@opts[?m].full? || config.model.name)
  rescue OllamaChat::UnknownModelError => e
    abort "Failed to use to model: #{e}"
  end
  @model_options  = Ollama::Options[config.model.options]

  @setup_system_prompt and setup_system_prompt

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

  interact_with_user
end