Class: OllamaChat::Chat
- Inherits:
-
Object
- Object
- OllamaChat::Chat
- Includes:
- Clipboard, Conversation, Dialog, DocumentCache, History, Information, KramdownANSI, MessageFormat, MessageOutput, ModelHandling, Parsing, ServerSocket, SourceFetching, Switches, ThinkControl, WebSearching, Term::ANSIColor, Tins::GO
- Defined in:
- lib/ollama_chat/chat.rb
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.
Class Attribute Summary collapse
-
.config ⇒ Object
The config attribute accessor provides read and write access to the configuration object associated with this instance.
Instance Attribute Summary collapse
-
#documents ⇒ Documentrix::Documents
readonly
Returns the documents set for this object, initializing it lazily if needed.
-
#messages ⇒ OllamaChat::MessageList
readonly
Returns the messages set for this object, initializing it lazily if needed.
-
#ollama ⇒ Ollama::Client
readonly
The ollama reader returns the Ollama API client instance.
Attributes included from ServerSocket
Attributes included from ThinkControl
Attributes included from Dialog
Attributes included from Switches
#embedding, #embedding_enabled, #embedding_paused, #location, #markdown, #stream, #think_loud, #voice
Instance Method Summary collapse
-
#config ⇒ ComplexConfig::Settings
The config method returns the configuration object associated with the class.
-
#config=(config) ⇒ Object
The config= method assigns a new configuration object to the class.
-
#debug ⇒ TrueClass, FalseClass
The debug method accesses the debug configuration setting.
-
#initialize(argv: ARGV.dup) ⇒ Chat
constructor
Initializes a new OllamaChat::Chat instance with the given command-line arguments.
-
#links ⇒ Set
Returns the links set for this object, initializing it lazily if needed.
-
#start ⇒ Object
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.
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
Methods included from Information
#collection_stats, #display_chat_help, #info, #server_url, #server_version, #usage, #version
Methods included from ThinkControl
#choose_think_mode, #think?, #think_loud?, #think_mode, #think_show
Methods included from Dialog
#ask?, #change_system_prompt, #change_voice, #choose_collection, #choose_document_policy, #choose_model, #choose_prompt, #connect_message, #message_list
Methods included from WebSearching
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 ModelHandling
#model_present?, #pull_model_from_remote, #pull_model_unless_present
Methods included from 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.
80 81 82 83 84 85 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 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ollama_chat/chat.rb', line 80 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 = OllamaChat::MessageList.new(self) @ollama_chat_config = OllamaChat::OllamaChatConfig.new(@opts[?f]) self.config = @ollama_chat_config.config setup_switches(config) base_url = @opts[?u] || OllamaChat::EnvConfig::OLLAMA::URL @ollama = Ollama::Client.new( connect_timeout: config.timeouts.connect_timeout?, read_timeout: config.timeouts.read_timeout?, write_timeout: config.timeouts.write_timeout?, base_url: base_url, debug: , user_agent: ) if server_version.version < '0.9.0'.version raise ArgumentError, 'require ollama API version 0.9.0 or higher' end @document_policy = config.document_policy @model = choose_model(@opts[?m], config.model.name) = Ollama::Options[config.model.] @think = config.think model_system = pull_model_unless_present(@model, ) .set(config..enabled && !@opts[?E]) if @opts[?c] .load_conversation(@opts[?c]) else default = config.system_prompts.default? || model_system if @opts[?s] =~ /\A\?/ change_system_prompt(default, system: @opts[?s]) else system = OllamaChat::Utils::FileArgument.get_file_argument(@opts[?s], default:) system.present? and .set_system_prompt(system) end end @documents = setup_documents @cache = setup_cache @current_voice = config.voice.default @images = [] @kramdown_ansi_styles = configure_kramdown_ansi_styles init_chat_history @opts[?S] and init_server_socket rescue ComplexConfig::AttributeMissing, ComplexConfig::ConfigurationSyntaxError => e fix_config(e) end |
Class Attribute Details
.config ⇒ Object
The config attribute accessor provides read and write access to the configuration object associated with this instance.
173 174 175 |
# File 'lib/ollama_chat/chat.rb', line 173 def config @config end |
Instance Attribute Details
#documents ⇒ Documentrix::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.
148 149 150 |
# File 'lib/ollama_chat/chat.rb', line 148 def documents @documents end |
#messages ⇒ OllamaChat::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.
158 159 160 |
# File 'lib/ollama_chat/chat.rb', line 158 def end |
#ollama ⇒ Ollama::Client (readonly)
The ollama reader returns the Ollama API client instance.
138 139 140 |
# File 'lib/ollama_chat/chat.rb', line 138 def ollama @ollama end |
Instance Method Details
#config ⇒ ComplexConfig::Settings
The config method returns the configuration object associated with the class.
187 188 189 |
# File 'lib/ollama_chat/chat.rb', line 187 def config self.class.config end |
#config=(config) ⇒ Object
The config= method assigns a new configuration object to the class.
179 180 181 |
# File 'lib/ollama_chat/chat.rb', line 179 def config=(config) self.class.config = config end |
#debug ⇒ TrueClass, FalseClass
The debug method accesses the debug configuration setting.
131 132 133 |
# File 'lib/ollama_chat/chat.rb', line 131 def debug OllamaChat::EnvConfig::OLLAMA::CHAT::DEBUG end |
#links ⇒ Set
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.
166 167 168 |
# File 'lib/ollama_chat/chat.rb', line 166 def links @links ||= Set.new end |
#start ⇒ Object
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.
194 195 196 197 198 199 200 201 202 |
# File 'lib/ollama_chat/chat.rb', line 194 def start info if .size > 1 .list_conversation(2) end STDOUT.puts "\nType /help to display the chat help." interact_with_user end |