Class: OllamaChat::Chat
- Inherits:
-
Object
- Object
- OllamaChat::Chat
- Includes:
- Clipboard, Dialog, DocumentCache, Information, MessageType, ModelHandling, Parsing, SourceFetching, Switches, Term::ANSIColor, Tins::GO
- Defined in:
- lib/ollama_chat/chat.rb
Class Attribute Summary collapse
-
.config ⇒ Object
Returns the value of attribute config.
Instance Attribute Summary collapse
-
#documents ⇒ Object
readonly
Returns the value of attribute documents.
-
#ollama ⇒ Object
readonly
Returns the value of attribute ollama.
Attributes included from Dialog
Attributes included from Switches
#embedding, #embedding_enabled, #embedding_paused, #location, #markdown, #stream, #voice
Instance Method Summary collapse
- #config ⇒ Object
- #config=(config) ⇒ Object
-
#initialize(argv: ARGV.dup) ⇒ Chat
constructor
A new instance of Chat.
- #links ⇒ Object
- #start ⇒ Object
Methods included from MessageType
Methods included from Clipboard
#copy_to_clipboard, #paste_from_input
Methods included from Information
#collection_stats, #display_chat_help, #info, #usage, #version
Methods included from Dialog
#ask?, #change_system_prompt, #change_voice, #choose_collection, #choose_document_policy, #choose_model, #message_list
Methods included from SourceFetching
#add_image, #embed, #embed_source, #fetch_source, #http_options, #import, #import_source, #search_web, #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
Returns a new instance of Chat.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/ollama_chat/chat.rb', line 27 def initialize(argv: ARGV.dup) @opts = go 'f:u:m:s:c:C:D:MEVh', 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 setup_switches(config) base_url = @opts[?u] || config.url @ollama = Ollama::Client.new( base_url: base_url, debug: config.debug, user_agent: ) @document_policy = config.document_policy @model = choose_model(@opts[?m], config.model.name) @model_options = Ollama::Options[config.model.] model_system = pull_model_unless_present(@model, @model_options) .set(config..enabled && !@opts[?E]) @messages = OllamaChat::MessageList.new(self) if @opts[?c] @messages.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 @messages.set_system_prompt(system) end end @documents = setup_documents @cache = setup_cache @current_voice = config.voice.default @images = [] end |
Class Attribute Details
.config ⇒ Object
Returns the value of attribute config.
72 73 74 |
# File 'lib/ollama_chat/chat.rb', line 72 def config @config end |
Instance Attribute Details
#documents ⇒ Object (readonly)
Returns the value of attribute documents.
65 66 67 |
# File 'lib/ollama_chat/chat.rb', line 65 def documents @documents end |
#ollama ⇒ Object (readonly)
Returns the value of attribute ollama.
63 64 65 |
# File 'lib/ollama_chat/chat.rb', line 63 def ollama @ollama end |
Instance Method Details
#config ⇒ Object
79 80 81 |
# File 'lib/ollama_chat/chat.rb', line 79 def config self.class.config end |
#config=(config) ⇒ Object
75 76 77 |
# File 'lib/ollama_chat/chat.rb', line 75 def config=(config) self.class.config = config end |
#links ⇒ Object
67 68 69 |
# File 'lib/ollama_chat/chat.rb', line 67 def links @links ||= Set.new end |
#start ⇒ Object
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
# File 'lib/ollama_chat/chat.rb', line 83 def start info STDOUT.puts "\nType /help to display the chat help." loop do parse_content = true input_prompt = bold { color(172) { (@images) + " user" } } + bold { "> " } content = Reline.readline(input_prompt, true)&.chomp case content when %r(^/copy$) copy_to_clipboard next when %r(^/paste$) content = paste_from_input when %r(^/markdown$) markdown.toggle next when %r(^/stream$) stream.toggle next when %r(^/location$) location.toggle next when %r(^/voice(?:\s+(change))?$) if $1 == 'change' change_voice else voice.toggle end next when %r(^/list(?:\s+(\d*))?$) last = 2 * $1.to_i if $1 @messages.list_conversation(last) next when %r(^/clear$) @messages.clear STDOUT.puts "Cleared messages." next when %r(^/clobber$) if ask?(prompt: 'Are you sure to clear messages and collection? (y/n) ') =~ /\Ay/i @messages.clear @documents.clear links.clear STDOUT.puts "Cleared messages and collection #{bold{@documents.collection}}." else STDOUT.puts 'Cancelled.' end next when %r(^/drop(?:\s+(\d*))?$) @messages.drop($1) @messages.list_conversation(2) next when %r(^/model$) @model = choose_model('', @model) next when %r(^/system$) change_system_prompt(@system) info next when %r(^/regenerate$) if content = @messages.second_last&.content content.gsub!(/\nConsider these chunks for your answer.*\z/, '') @messages.drop(2) else STDOUT.puts "Not enough messages in this conversation." redo end parse_content = false content when %r(^/collection(?:\s+(clear|change))?$) case $1 || 'change' when 'clear' loop do = @documents..add('[EXIT]').add('[ALL]') tag = OllamaChat::Utils::Chooser.choose(, prompt: 'Clear? %s') case tag when nil, '[EXIT]' STDOUT.puts "Exiting chooser." break when '[ALL]' if ask?(prompt: 'Are you sure? (y/n) ') =~ /\Ay/i @documents.clear STDOUT.puts "Cleared collection #{bold{@documents.collection}}." break else STDOUT.puts 'Cancelled.' sleep 3 end when /./ @documents.clear(tags: [ tag ]) STDOUT.puts "Cleared tag #{tag} from collection #{bold{@documents.collection}}." sleep 3 end end when 'change' choose_collection(@documents.collection) end next when %r(^/info$) info next when %r(^/document_policy$) choose_document_policy next when %r(^/import\s+(.+)) parse_content = false content = import($1) or next when %r(^/summarize\s+(?:(\d+)\s+)?(.+)) parse_content = false content = summarize($2, words: $1) or next when %r(^/embedding$) .toggle(show: false) .show next when %r(^/embed\s+(.+)) parse_content = false content = ($1) or next when %r(^/web\s+(?:(\d+)\s+)?(.+)) parse_content = false urls = search_web($2, $1.to_i) urls.each do |url| fetch_source(url) { |url_io| (url_io, url) } end urls_summarized = urls.map { summarize(_1) } query = $2.inspect results = urls.zip(urls_summarized). map { |u, s| "%s as \n:%s" % [ u, s ] } * "\n\n" content = config.prompts.web % { query:, results: } when %r(^/save\s+(.+)$) @messages.save_conversation($1) STDOUT.puts "Saved conversation to #$1." next when %r(^/links(?:\s+(clear))?$) case $1 when 'clear' loop do = links.dup.add('[EXIT]').add('[ALL]') link = OllamaChat::Utils::Chooser.choose(, prompt: 'Clear? %s') case link when nil, '[EXIT]' STDOUT.puts "Exiting chooser." break when '[ALL]' if ask?(prompt: 'Are you sure? (y/n) ') =~ /\Ay/i links.clear STDOUT.puts "Cleared all links in list." break else STDOUT.puts 'Cancelled.' sleep 3 end when /./ links.delete(link) STDOUT.puts "Cleared link from links in list." sleep 3 end end when nil if links.empty? STDOUT.puts "List is empty." else Math.log10(links.size).ceil format = "% #{}s. %s" connect = -> link { hyperlink(link) { link } } STDOUT.puts links.each_with_index.map { |x, i| format % [ i + 1, connect.(x) ] } end end next when %r(^/load\s+(.+)$) @messages.load_conversation($1) STDOUT.puts "Loaded conversation from #$1." next when %r(^/config$) default_pager = ENV['PAGER'].full? if fallback_pager = `which less`.chomp.full? || `which more`.chomp.full? fallback_pager << ' -r' end my_pager = default_pager || fallback_pager rendered = config.to_s Kramdown::ANSI::Pager.pager( lines: rendered.count(?\n), command: my_pager ) do |output| output.puts rendered end next when %r(^/quit$) STDOUT.puts "Goodbye." return when %r(^/) display_chat_help next when '' STDOUT.puts "Type /quit to quit." next when nil STDOUT.puts "Goodbye." return end content, = if parse_content parse_content(content, @images) else [ content, Documentrix::Utils::Tags.new ] end if .on? && content records = @documents.find_where( content.downcase, tags:, prompt: config..model.prompt?, text_size: config..found_texts_size?, text_count: config..found_texts_count?, ) unless records.empty? content += "\nConsider these chunks for your answer:\n\n"\ "#{records.map { [ _1.text, _1. ] * ?\n }.join("\n\n---\n\n")}" end end @messages << Ollama::Message.new(role: 'user', content:, images: @images.dup) @images.clear handler = OllamaChat::FollowChat.new( chat: self, messages: @messages, voice: (@current_voice if voice.on?) ) ollama.chat( model: @model, messages: @messages, options: @model_options, stream: stream.on?, &handler ) if .on? && !records.empty? STDOUT.puts "", records.map { |record| link = if record.source =~ %r(\Ahttps?://) record.source else 'file://%s' % File.(record.source) end [ link, record..first ] }.uniq.map { |l, t| hyperlink(l, t) }.join(' ') config.debug and jj @messages.to_ary end rescue Interrupt STDOUT.puts "Type /quit to quit." end 0 end |