Class: OllamaChat::Chat
- Inherits:
-
Object
- Object
- OllamaChat::Chat
- Includes:
- Clipboard, ConfigHandling, Conversation, Dialog, DocumentCache, History, Information, InputContent, KramdownANSI, LocationHandling, MessageEditing, MessageFormat, MessageOutput, ModelHandling, Parsing, ServerSocket, SourceFetching, StateSelectors, Switches, ThinkControl, ToolCalling, 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.
Instance Attribute Summary collapse
-
#document_policy ⇒ OllamaChat::StateSelector
readonly
The document_policy reader returns the document policy selector for the chat session.
-
#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.
-
#think_mode ⇒ OllamaChat::StateSelector
readonly
The think_mode reader returns the think mode selector for the chat session.
Attributes included from ToolCalling
#enabled_tools, #tool_call_results
Attributes included from Switches
#embedding, #embedding_enabled, #embedding_paused, #location, #markdown, #stream, #think_loud, #tools_support, #voice
Instance Method Summary collapse
-
#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.
-
#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.
-
#vim(server_name = nil) ⇒ OllamaChat::Vim
The vim method creates and returns a new Vim instance for interacting with a Vim server.
Methods included from ConfigHandling
Methods included from ToolCalling
#configured_tools, #default_enabled_tools, #disable_tool, #enable_tool, #list_tools, #tool_configured?, #tool_enabled?, #tool_function, #tool_registered?, #tools
Methods included from LocationHandling
#location_data, #location_description
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 MessageFormat
#message_type, #talk_annotate, #think_annotate
Methods included from MessageOutput
Methods included from Information
#collection_stats, #display_chat_help, #info, #server_url, #server_version, #usage, #version
Methods included from ThinkControl
Methods included from Dialog
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 Utils::AnalyzeDirectory
Methods included from StateSelectors
Methods included from 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.
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 |
# File 'lib/ollama_chat/chat.rb', line 87 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.] @model_system = pull_model_unless_present(@model, @model_options) if @opts[?c] .load_conversation(@opts[?c]) else setup_system_prompt end .set(config..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 |
Instance Attribute Details
#document_policy ⇒ OllamaChat::StateSelector (readonly)
The document_policy reader returns the document policy selector for the chat session.
122 123 124 |
# File 'lib/ollama_chat/chat.rb', line 122 def document_policy @document_policy end |
#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.
143 144 145 |
# File 'lib/ollama_chat/chat.rb', line 143 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.
153 154 155 |
# File 'lib/ollama_chat/chat.rb', line 153 def @messages end |
#ollama ⇒ Ollama::Client (readonly)
The ollama reader returns the Ollama API client instance.
133 134 135 |
# File 'lib/ollama_chat/chat.rb', line 133 def ollama @ollama end |
#think_mode ⇒ OllamaChat::StateSelector (readonly)
The think_mode reader returns the think mode selector for the chat session.
128 129 130 |
# File 'lib/ollama_chat/chat.rb', line 128 def think_mode @think_mode end |
Instance Method Details
#debug ⇒ TrueClass, FalseClass
The debug method accesses the debug configuration setting.
190 191 192 |
# File 'lib/ollama_chat/chat.rb', line 190 def debug OllamaChat::EnvConfig::OLLAMA::CHAT::DEBUG 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.
158 159 160 161 162 163 164 165 166 |
# File 'lib/ollama_chat/chat.rb', line 158 def start info if .size > 1 .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.
182 183 184 185 |
# File 'lib/ollama_chat/chat.rb', line 182 def vim(server_name = nil) clientserver = config.vim?&.clientserver OllamaChat::Vim.new(server_name, clientserver:) end |