ollama_chat 0.0.20 → 0.0.21
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/VERSION +1 -1
- data/lib/ollama_chat/chat.rb +172 -5
- data/lib/ollama_chat/dialog.rb +8 -5
- data/lib/ollama_chat/follow_chat.rb +36 -0
- data/lib/ollama_chat/information.rb +1 -0
- data/lib/ollama_chat/ollama_chat_config/default_config.yml +1 -0
- data/lib/ollama_chat/source_fetching.rb +91 -1
- data/lib/ollama_chat/switches.rb +79 -16
- data/lib/ollama_chat/utils/cache_fetcher.rb +30 -0
- data/lib/ollama_chat/utils/fetcher.rb +117 -0
- data/lib/ollama_chat/version.rb +1 -1
- data/lib/ollama_chat/vim.rb +53 -0
- data/lib/ollama_chat/web_searching.rb +35 -1
- data/lib/ollama_chat.rb +1 -0
- data/ollama_chat.gemspec +4 -4
- data/spec/assets/api_tags.json +17 -0
- data/spec/ollama_chat/switches_spec.rb +9 -14
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 91a8452d2970be06a595b83e8dfdc4ee8be0388b37171967ef884139ad97b492
|
4
|
+
data.tar.gz: 116ca9055525145d36a65abc5d38341a603fbc62ccf4d52257876ddaa3a3f0d9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 710139d1db7d25ed49218eaf6c22f61cea64352bbd2d8d1c418ccaba67f190c6157b14d8c5ec41da18e83669177310143b0e3683d5df7b2d7e9c68e3edf72df7
|
7
|
+
data.tar.gz: ef6f2705cfacb4c4523ead9e3361ce980aef227e06b6bc3a7a639d6e492d82cbd03f6c1ca4b82383b29cb95d740d9b815fff3d5c57336baf5f885e1e43e81277
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.21
|
data/lib/ollama_chat/chat.rb
CHANGED
@@ -34,6 +34,33 @@ class OllamaChat::Chat
|
|
34
34
|
include OllamaChat::History
|
35
35
|
include OllamaChat::ServerSocket
|
36
36
|
|
37
|
+
# Initializes a new OllamaChat::Chat instance with the given command-line
|
38
|
+
# arguments.
|
39
|
+
#
|
40
|
+
# Sets up the chat environment including configuration parsing, Ollama client
|
41
|
+
# initialization, model selection, system prompt handling, document
|
42
|
+
# processing setup, and history management. This method handles all the
|
43
|
+
# bootstrapping necessary to create a functional chat session that can
|
44
|
+
# communicate with an Ollama server and process various input types including
|
45
|
+
# text, documents, web content, and images.
|
46
|
+
#
|
47
|
+
# The initialization process includes parsing command-line options using
|
48
|
+
# Tins::GO for robust argument handling, setting up the Ollama client with
|
49
|
+
# configurable timeouts (connect, read, write), validating Ollama API version
|
50
|
+
# compatibility (requires >= 0.9.0 for features used), configuring model
|
51
|
+
# selection based on command-line or configuration defaults, initializing
|
52
|
+
# system prompts from files or inline content, setting up document processing
|
53
|
+
# pipeline with embedding capabilities through Documentrix::Documents,
|
54
|
+
# creating message history management through OllamaChat::MessageList,
|
55
|
+
# initializing cache systems for document embeddings, setting up
|
56
|
+
# voice support and image handling for multimodal interactions, enabling
|
57
|
+
# optional server socket functionality for remote input, and handling
|
58
|
+
# configuration errors with interactive recovery mechanisms.
|
59
|
+
#
|
60
|
+
# @param argv [Array<String>] Command-line arguments to parse (defaults to ARGV.dup)
|
61
|
+
#
|
62
|
+
# @raise [ArgumentError] If the Ollama API version is less than 0.9.0, indicating
|
63
|
+
# incompatibility with required API features
|
37
64
|
def initialize(argv: ARGV.dup)
|
38
65
|
@opts = go 'f:u:m:s:c:C:D:MESVh', argv
|
39
66
|
@opts[?h] and exit usage
|
@@ -80,28 +107,66 @@ class OllamaChat::Chat
|
|
80
107
|
fix_config(e)
|
81
108
|
end
|
82
109
|
|
110
|
+
# The ollama reader returns the Ollama API client instance.
|
111
|
+
#
|
112
|
+
# @return [Ollama::Client] the configured Ollama API client
|
83
113
|
attr_reader :ollama
|
84
114
|
|
115
|
+
|
116
|
+
# Returns the documents set for this object, initializing it lazily if needed.
|
117
|
+
#
|
118
|
+
# The documents set is memoized, meaning it will only be created once per
|
119
|
+
# object instance and subsequent calls will return the same
|
120
|
+
# Documentrix::Documents instance.
|
121
|
+
#
|
122
|
+
# @return [Documentrix::Documents] A Documentrix::Documents object containing
|
123
|
+
# all documents associated with this instance
|
85
124
|
attr_reader :documents
|
86
125
|
|
126
|
+
# Returns the messages set for this object, initializing it lazily if needed.
|
127
|
+
#
|
128
|
+
# The messages set is memoized, meaning it will only be created once per
|
129
|
+
# object instance and subsequent calls will return the same
|
130
|
+
# OllamaChat::MessageList instance.
|
131
|
+
#
|
132
|
+
# @return [OllamaChat::MessageList] A MessageList object containing all
|
133
|
+
# messages associated with this instance
|
87
134
|
attr_reader :messages
|
88
135
|
|
136
|
+
# Returns the links set for this object, initializing it lazily if needed.
|
137
|
+
#
|
138
|
+
# The links set is memoized, meaning it will only be created once per object
|
139
|
+
# instance and subsequent calls will return the same Set instance.
|
140
|
+
#
|
141
|
+
# @return [Set] A Set object containing all links associated with this instance
|
89
142
|
def links
|
90
143
|
@links ||= Set.new
|
91
144
|
end
|
92
145
|
|
93
146
|
class << self
|
147
|
+
# The config attribute accessor provides read and write access to the
|
148
|
+
# configuration object associated with this instance.
|
94
149
|
attr_accessor :config
|
95
150
|
end
|
96
151
|
|
152
|
+
# The config= method assigns a new configuration object to the class.
|
153
|
+
#
|
154
|
+
# @param config [ ComplexConfig::Settings ] the configuration object to be set
|
97
155
|
def config=(config)
|
98
156
|
self.class.config = config
|
99
157
|
end
|
100
158
|
|
159
|
+
# The config method returns the configuration object associated with the
|
160
|
+
# class.
|
161
|
+
#
|
162
|
+
# @return [ ComplexConfig::Settings ] the configuration instance
|
101
163
|
def config
|
102
164
|
self.class.config
|
103
165
|
end
|
104
166
|
|
167
|
+
# The start method initializes the chat session by displaying information and
|
168
|
+
# conversation history, then prompts the user for input to begin interacting
|
169
|
+
# with the chat.
|
105
170
|
def start
|
106
171
|
info
|
107
172
|
if messages.size > 1
|
@@ -235,7 +300,7 @@ class OllamaChat::Chat
|
|
235
300
|
if messages.save_conversation(filename)
|
236
301
|
STDOUT.puts "Saved conversation to #{filename.inspect}."
|
237
302
|
else
|
238
|
-
|
303
|
+
STDERR.puts "Saving conversation to #{filename.inspect} failed."
|
239
304
|
end
|
240
305
|
:next
|
241
306
|
when %r(^/links(?:\s+(clear))?$)
|
@@ -250,7 +315,7 @@ class OllamaChat::Chat
|
|
250
315
|
if success
|
251
316
|
STDOUT.puts "Loaded conversation from #{filename.inspect}."
|
252
317
|
else
|
253
|
-
|
318
|
+
STDERR.puts "Loading conversation from #{filename.inspect} failed."
|
254
319
|
end
|
255
320
|
:next
|
256
321
|
when %r(^/pipe\s+(.+)$)
|
@@ -259,6 +324,13 @@ class OllamaChat::Chat
|
|
259
324
|
when %r(^/output\s+(.+)$)
|
260
325
|
output($1)
|
261
326
|
:next
|
327
|
+
when %r(^/vim(?:\s+(.+))?$)
|
328
|
+
if message = messages.last
|
329
|
+
OllamaChat::Vim.new($1).insert message.content
|
330
|
+
else
|
331
|
+
STDERR.puts "Warning: No message found to insert into Vim"
|
332
|
+
end
|
333
|
+
:next
|
262
334
|
when %r(^/config$)
|
263
335
|
display_config
|
264
336
|
:next
|
@@ -274,6 +346,14 @@ class OllamaChat::Chat
|
|
274
346
|
end
|
275
347
|
end
|
276
348
|
|
349
|
+
# The web method searches for URLs based on a query and processes them by
|
350
|
+
# fetching, embedding, and summarizing their content, then formats the
|
351
|
+
# results into a prompt string.
|
352
|
+
#
|
353
|
+
# @param count [ String ] the number of URLs to search for
|
354
|
+
# @param query [ String ] the search query string
|
355
|
+
#
|
356
|
+
# @return [ String ] the formatted prompt string containing the query and summarized results
|
277
357
|
def web(count, query)
|
278
358
|
urls = search_web(query, count.to_i) or return :next
|
279
359
|
urls.each do |url|
|
@@ -285,6 +365,16 @@ class OllamaChat::Chat
|
|
285
365
|
config.prompts.web % { query:, results: }
|
286
366
|
end
|
287
367
|
|
368
|
+
# The manage_links method handles operations on a collection of links, such
|
369
|
+
# as displaying them or clearing specific entries.
|
370
|
+
#
|
371
|
+
# It supports two main commands: 'clear' and nil (default).
|
372
|
+
# When the command is 'clear', it presents an interactive menu to either
|
373
|
+
# clear all links or individual links.
|
374
|
+
# When the command is nil, it displays the current list of links with
|
375
|
+
# hyperlinks.
|
376
|
+
#
|
377
|
+
# @param command [ String, nil ] the operation to perform on the links
|
288
378
|
def manage_links(command)
|
289
379
|
case command
|
290
380
|
when 'clear'
|
@@ -322,10 +412,14 @@ class OllamaChat::Chat
|
|
322
412
|
end
|
323
413
|
end
|
324
414
|
|
415
|
+
# The clean method clears various parts of the chat session based on the
|
416
|
+
# specified parameter.
|
417
|
+
#
|
418
|
+
# @param what [ String, nil ] the type of data to clear, defaults to
|
419
|
+
# 'messages' if nil
|
325
420
|
def clean(what)
|
326
|
-
what = 'messages' if what.nil?
|
327
421
|
case what
|
328
|
-
when 'messages'
|
422
|
+
when 'messages', nil
|
329
423
|
messages.clear
|
330
424
|
STDOUT.puts "Cleared messages."
|
331
425
|
when 'links'
|
@@ -350,6 +444,11 @@ class OllamaChat::Chat
|
|
350
444
|
end
|
351
445
|
end
|
352
446
|
|
447
|
+
# The display_config method renders the configuration and displays it using a
|
448
|
+
# pager.
|
449
|
+
# It determines an appropriate pager command based on environment variables
|
450
|
+
# and available system commands, then uses Kramdown::ANSI::Pager to show the
|
451
|
+
# formatted configuration output.
|
353
452
|
def display_config
|
354
453
|
default_pager = ENV['PAGER'].full?
|
355
454
|
if fallback_pager = `which less`.chomp.full? || `which more`.chomp.full?
|
@@ -365,6 +464,18 @@ class OllamaChat::Chat
|
|
365
464
|
end
|
366
465
|
end
|
367
466
|
|
467
|
+
# The interact_with_user method manages the interactive loop for user input
|
468
|
+
# and chat processing.
|
469
|
+
# It handles reading user input, processing commands, managing messages, and
|
470
|
+
# communicating with the Ollama server.
|
471
|
+
# The method supports command completion, prefilling prompts, socket input
|
472
|
+
# handling, and various chat features including embedding context and voice
|
473
|
+
# support.
|
474
|
+
# It processes user input through command handling, content parsing, and
|
475
|
+
# message formatting before sending requests to the Ollama server.
|
476
|
+
# The method also handles server socket messages, manages chat history, and
|
477
|
+
# ensures proper cleanup and configuration handling throughout the
|
478
|
+
# interaction.
|
368
479
|
def interact_with_user
|
369
480
|
loop do
|
370
481
|
@parse_content = true
|
@@ -479,6 +590,13 @@ class OllamaChat::Chat
|
|
479
590
|
|
480
591
|
private
|
481
592
|
|
593
|
+
# The setup_documents method initializes the document processing pipeline by
|
594
|
+
# configuring the embedding model and database connection.
|
595
|
+
# It then loads specified documents into the system and returns the
|
596
|
+
# configured document collection.
|
597
|
+
#
|
598
|
+
# @return [ Documentrix::Documents, NULL ] the initialized document
|
599
|
+
# collection if embedding is enabled, otherwise NULL
|
482
600
|
def setup_documents
|
483
601
|
if embedding.on?
|
484
602
|
@embedding_model = config.embedding.model.name
|
@@ -501,10 +619,35 @@ class OllamaChat::Chat
|
|
501
619
|
add_documents_from_argv(document_list)
|
502
620
|
@documents
|
503
621
|
else
|
504
|
-
|
622
|
+
NULL
|
505
623
|
end
|
506
624
|
end
|
507
625
|
|
626
|
+
# Adds documents from command line arguments to the document collection
|
627
|
+
#
|
628
|
+
# Processes a list of document paths or URLs, handling both local files and
|
629
|
+
# remote resources.
|
630
|
+
#
|
631
|
+
# @param document_list [Array<String>] List of document paths or URLs to process
|
632
|
+
#
|
633
|
+
# @return [void]
|
634
|
+
#
|
635
|
+
# @example Adding local files
|
636
|
+
# add_documents_from_argv(['/path/to/file1.txt', '/path/to/file2.pdf'])
|
637
|
+
#
|
638
|
+
# @example Adding remote URLs
|
639
|
+
# add_documents_from_argv(['https://example.com/page1', 'http://example.com/page2'])
|
640
|
+
#
|
641
|
+
# @example Mixed local and remote
|
642
|
+
# add_documents_from_argv(['/local/file.txt', 'https://remote.com/document'])
|
643
|
+
#
|
644
|
+
# @note Empty entries in the document list will trigger a collection clear operation
|
645
|
+
# @note Documents are processed in batches of 25 to manage memory usage
|
646
|
+
# @note Progress is reported to STDOUT during processing
|
647
|
+
#
|
648
|
+
# @see fetch_source
|
649
|
+
# @see embed_source
|
650
|
+
# @see documents.clear
|
508
651
|
def add_documents_from_argv(document_list)
|
509
652
|
if document_list.any?(&:empty?)
|
510
653
|
STDOUT.puts "Clearing collection #{bold{documents.collection}}."
|
@@ -532,6 +675,11 @@ class OllamaChat::Chat
|
|
532
675
|
end
|
533
676
|
end
|
534
677
|
|
678
|
+
# The setup_cache method initializes and returns a Redis cache instance with
|
679
|
+
# expiring keys if a Redis URL is configured.
|
680
|
+
#
|
681
|
+
# @return [ Documentrix::Documents::RedisCache, nil ] the configured Redis
|
682
|
+
# cache instance or nil if no URL is set.
|
535
683
|
def setup_cache
|
536
684
|
if url = config.redis.expiring.url?
|
537
685
|
ex = config.redis.expiring.ex?.to_i
|
@@ -543,6 +691,14 @@ class OllamaChat::Chat
|
|
543
691
|
end
|
544
692
|
end
|
545
693
|
|
694
|
+
# The fix_config method handles configuration file errors by informing the
|
695
|
+
# user about the exception and prompting them to fix it.
|
696
|
+
# It then executes a diff tool to compare the current config file with the
|
697
|
+
# default one.
|
698
|
+
# This method exits the program after handling the configuration error
|
699
|
+
#
|
700
|
+
# @param exception [ Exception ] the exception that occurred while reading
|
701
|
+
# the config file
|
546
702
|
def fix_config(exception)
|
547
703
|
STDOUT.puts "When reading the config file, a #{exception.class} "\
|
548
704
|
"exception was caught: #{exception.message.inspect}"
|
@@ -558,6 +714,17 @@ class OllamaChat::Chat
|
|
558
714
|
end
|
559
715
|
end
|
560
716
|
|
717
|
+
# Enables tab completion for chat commands within the interactive session
|
718
|
+
#
|
719
|
+
# Temporarily replaces the current Reline completion procedure with a custom
|
720
|
+
# one that provides command completion based on the chat help message.
|
721
|
+
#
|
722
|
+
# @param block [Proc] The block to execute with enhanced tab completion enabled
|
723
|
+
#
|
724
|
+
# @return [Object] The return value of the executed block
|
725
|
+
#
|
726
|
+
# @see display_chat_help_message
|
727
|
+
# @see Reline.completion_proc
|
561
728
|
def enable_command_completion(&block)
|
562
729
|
old = Reline.completion_proc
|
563
730
|
commands = display_chat_help_message.scan(/^\s*(\S+)/).inject(&:concat)
|
data/lib/ollama_chat/dialog.rb
CHANGED
@@ -32,11 +32,14 @@ module OllamaChat::Dialog
|
|
32
32
|
end
|
33
33
|
models = ollama.tags.models.sort_by(&:name).map { |m| model_with_size(m) }
|
34
34
|
selector and models = models.grep(selector)
|
35
|
-
model =
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
35
|
+
model =
|
36
|
+
if models.size == 1
|
37
|
+
models.first
|
38
|
+
elsif cli_model == ''
|
39
|
+
OllamaChat::Utils::Chooser.choose(models) || current_model
|
40
|
+
else
|
41
|
+
cli_model || current_model
|
42
|
+
end
|
40
43
|
ensure
|
41
44
|
STDOUT.puts green { "Connecting to #{model}@#{ollama.base_url} now…" }
|
42
45
|
end
|
@@ -60,6 +60,13 @@ class OllamaChat::FollowChat
|
|
60
60
|
|
61
61
|
private
|
62
62
|
|
63
|
+
# The ensure_assistant_response_exists method ensures that the last message
|
64
|
+
# in the conversation is from the assistant role.
|
65
|
+
#
|
66
|
+
# If the last message is not from an assistant, it adds a new assistant
|
67
|
+
# message with empty content and optionally includes thinking content if the
|
68
|
+
# chat's think mode is enabled. It also updates the user display variable to
|
69
|
+
# reflect the assistant's message type and styling.
|
63
70
|
def ensure_assistant_response_exists
|
64
71
|
if @messages&.last&.role != 'assistant'
|
65
72
|
@messages << Message.new(
|
@@ -72,6 +79,12 @@ class OllamaChat::FollowChat
|
|
72
79
|
end
|
73
80
|
end
|
74
81
|
|
82
|
+
# The update_last_message method appends the content of a response to the
|
83
|
+
# last message in the conversation. It also appends thinking content to the
|
84
|
+
# last message if thinking is enabled and thinking content is present.
|
85
|
+
#
|
86
|
+
# @param response [ Object ] the response object containing message content
|
87
|
+
# and thinking
|
75
88
|
def update_last_message(response)
|
76
89
|
@messages.last.content << response.message&.content
|
77
90
|
if @chat.think.on? and response_thinking = response.message&.thinking.full?
|
@@ -79,6 +92,13 @@ class OllamaChat::FollowChat
|
|
79
92
|
end
|
80
93
|
end
|
81
94
|
|
95
|
+
# The display_formatted_terminal_output method formats and outputs the
|
96
|
+
# terminal content by processing the last message's content and thinking,
|
97
|
+
# then prints it to the output. It handles markdown parsing and annotation
|
98
|
+
# based on chat settings, and ensures proper formatting with clear screen and
|
99
|
+
# move home commands. The method takes into account whether markdown and
|
100
|
+
# thinking modes are enabled to determine how to process and display the
|
101
|
+
# content.
|
82
102
|
def display_formatted_terminal_output
|
83
103
|
content, thinking = @messages.last.content, @messages.last.thinking
|
84
104
|
if @chat.markdown.on?
|
@@ -95,6 +115,14 @@ class OllamaChat::FollowChat
|
|
95
115
|
].compact))
|
96
116
|
end
|
97
117
|
|
118
|
+
# The eval_stats method processes response statistics and formats them into a
|
119
|
+
# colored, readable string output.
|
120
|
+
#
|
121
|
+
# @param response [ Object ] the response object containing evaluation metrics
|
122
|
+
#
|
123
|
+
# @return [ String ] a formatted string with statistical information about
|
124
|
+
# the evaluation process including durations, counts, and rates, styled with
|
125
|
+
# colors and formatting
|
98
126
|
def eval_stats(response)
|
99
127
|
eval_duration = response.eval_duration / 1e9
|
100
128
|
prompt_eval_duration = response.prompt_eval_duration / 1e9
|
@@ -113,11 +141,19 @@ class OllamaChat::FollowChat
|
|
113
141
|
}
|
114
142
|
end
|
115
143
|
|
144
|
+
# The output_eval_stats method outputs evaluation statistics to the specified
|
145
|
+
# output stream.
|
146
|
+
#
|
147
|
+
# @param response [ Object ] the response object containing evaluation data
|
116
148
|
def output_eval_stats(response)
|
117
149
|
response.done or return
|
118
150
|
@output.puts "", eval_stats(response)
|
119
151
|
end
|
120
152
|
|
153
|
+
# The debug_output method conditionally outputs the response object using jj
|
154
|
+
# when debugging is enabled.
|
155
|
+
#
|
156
|
+
# @param response [ Object ] the response object to be outputted
|
121
157
|
def debug_output(response)
|
122
158
|
OllamaChat::Chat.config.debug and jj response
|
123
159
|
end
|
@@ -116,6 +116,7 @@ module OllamaChat::Information
|
|
116
116
|
/load filename load conversation messages
|
117
117
|
/output filename save last response to filename
|
118
118
|
/pipe command write last response to command's stdin
|
119
|
+
/vim insert the last message into a vim server
|
119
120
|
/quit to quit
|
120
121
|
/help to view this help
|
121
122
|
EOT
|
@@ -1,4 +1,13 @@
|
|
1
1
|
module OllamaChat::SourceFetching
|
2
|
+
# The http_options method prepares HTTP options for requests based on
|
3
|
+
# configuration settings.
|
4
|
+
# It determines whether SSL peer verification should be disabled for a given
|
5
|
+
# URL and whether a proxy should be used, then returns a hash of options.
|
6
|
+
#
|
7
|
+
# @param url [ String ] the URL for which HTTP options are being prepared
|
8
|
+
#
|
9
|
+
# @return [ Hash ] a hash containing HTTP options such as ssl_verify_peer and
|
10
|
+
# proxy settings
|
2
11
|
def http_options(url)
|
3
12
|
options = {}
|
4
13
|
if ssl_no_verify = config.ssl_no_verify?
|
@@ -11,6 +20,13 @@ module OllamaChat::SourceFetching
|
|
11
20
|
options
|
12
21
|
end
|
13
22
|
|
23
|
+
# The fetch_source method retrieves content from various source types
|
24
|
+
# including commands, URLs, and file paths. It processes the source based on
|
25
|
+
# its type and yields a temporary file handle for further processing.
|
26
|
+
#
|
27
|
+
# @param source [ String ] the source identifier which can be a command, URL, or file path
|
28
|
+
#
|
29
|
+
# @yield [ tmp ]
|
14
30
|
def fetch_source(source, &block)
|
15
31
|
case source
|
16
32
|
when %r(\A!(.*))
|
@@ -36,18 +52,38 @@ module OllamaChat::SourceFetching
|
|
36
52
|
block.(tmp)
|
37
53
|
end
|
38
54
|
else
|
39
|
-
raise "invalid source"
|
55
|
+
raise "invalid source #{source.inspect}"
|
40
56
|
end
|
41
57
|
rescue => e
|
42
58
|
STDERR.puts "Cannot fetch source #{source.to_s.inspect}: #{e.class} #{e}\n#{e.backtrace * ?\n}"
|
43
59
|
end
|
44
60
|
|
61
|
+
|
62
|
+
# Adds an image to the images collection from the given source IO and source
|
63
|
+
# identifier.
|
64
|
+
#
|
65
|
+
# This method takes an IO object containing image data and associates it with
|
66
|
+
# a source, creating an Ollama::Image instance and adding it to the images
|
67
|
+
# array.
|
68
|
+
#
|
69
|
+
# @param images [Array] The collection of images to which the new image will be added
|
70
|
+
# @param source_io [IO] The input stream containing the image data
|
71
|
+
# @param source [String, #to_s] The identifier or path for the source of the image
|
45
72
|
def add_image(images, source_io, source)
|
46
73
|
STDERR.puts "Adding #{source_io&.content_type} image #{source.to_s.inspect}."
|
47
74
|
image = Ollama::Image.for_io(source_io, path: source.to_s)
|
48
75
|
(images << image).uniq!
|
49
76
|
end
|
50
77
|
|
78
|
+
# The import_source method processes and imports content from a given source,
|
79
|
+
# displaying information about the document type and returning a formatted
|
80
|
+
# string that indicates the import result along with the parsed content.
|
81
|
+
#
|
82
|
+
# @param source_io [ IO ] the input stream containing the document content
|
83
|
+
# @param source [ String ] the source identifier or path
|
84
|
+
#
|
85
|
+
# @return [ String ] a formatted message indicating the import result and the
|
86
|
+
# parsed content
|
51
87
|
def import_source(source_io, source)
|
52
88
|
source = source.to_s
|
53
89
|
document_type = source_io&.content_type.full? { |ct| italic { ct } + ' ' }
|
@@ -56,6 +92,16 @@ module OllamaChat::SourceFetching
|
|
56
92
|
"Imported #{source.inspect}:\n\n#{source_content}\n\n"
|
57
93
|
end
|
58
94
|
|
95
|
+
# Imports content from the specified source and processes it.
|
96
|
+
#
|
97
|
+
# This method fetches content from a given source (command, URL, or file) and
|
98
|
+
# passes the resulting IO object to the import_source method for processing.
|
99
|
+
#
|
100
|
+
# @param source [String] The source identifier which can be a command, URL,
|
101
|
+
# or file path
|
102
|
+
#
|
103
|
+
# @return [String, nil] A formatted message indicating the import result and
|
104
|
+
# parsed content, # or nil if the operation fails
|
59
105
|
def import(source)
|
60
106
|
fetch_source(source) do |source_io|
|
61
107
|
content = import_source(source_io, source) or return
|
@@ -64,6 +110,16 @@ module OllamaChat::SourceFetching
|
|
64
110
|
end
|
65
111
|
end
|
66
112
|
|
113
|
+
|
114
|
+
# Summarizes content from the given source IO and source identifier.
|
115
|
+
#
|
116
|
+
# This method takes an IO object containing document content and generates a
|
117
|
+
# summary based on the configured prompt template and word count.
|
118
|
+
#
|
119
|
+
# @param source_io [IO] The input stream containing the document content to summarize
|
120
|
+
# @param source [String, #to_s] The identifier or path for the source of the content
|
121
|
+
# @param words [Integer, nil] The target number of words for the summary (defaults to 100)
|
122
|
+
# @return [String, nil] The formatted summary message or nil if content is empty or cannot be processed
|
67
123
|
def summarize_source(source_io, source, words: nil)
|
68
124
|
STDOUT.puts "Summarizing #{italic { source_io&.content_type }} document #{source.to_s.inspect} now."
|
69
125
|
words = words.to_i
|
@@ -73,6 +129,15 @@ module OllamaChat::SourceFetching
|
|
73
129
|
config.prompts.summarize % { source_content:, words: }
|
74
130
|
end
|
75
131
|
|
132
|
+
|
133
|
+
# Summarizes content from the specified source.
|
134
|
+
#
|
135
|
+
# This method fetches content from a given source (command, URL, or file) and
|
136
|
+
# generates a summary using the summarize_source method.
|
137
|
+
#
|
138
|
+
# @param source [String] The source identifier which can be a command, URL, or file path
|
139
|
+
# @param words [Integer, nil] The target number of words for the summary (defaults to 100)
|
140
|
+
# @return [String, nil] The formatted summary message or nil if the operation fails
|
76
141
|
def summarize(source, words: nil)
|
77
142
|
fetch_source(source) do |source_io|
|
78
143
|
content = summarize_source(source_io, source, words:) or return
|
@@ -81,6 +146,19 @@ module OllamaChat::SourceFetching
|
|
81
146
|
end
|
82
147
|
end
|
83
148
|
|
149
|
+
|
150
|
+
# Embeds content from the given source IO and source identifier.
|
151
|
+
#
|
152
|
+
# This method processes document content by splitting it into chunks using
|
153
|
+
# various splitting strategies (Character, RecursiveCharacter, Semantic) and
|
154
|
+
# adds the chunks to a document store for embedding.
|
155
|
+
#
|
156
|
+
# @param source_io [IO] The input stream containing the document content to embed
|
157
|
+
# @param source [String, #to_s] The identifier or path for the source of the content
|
158
|
+
# @param count [Integer, nil] An optional counter for tracking processing order
|
159
|
+
#
|
160
|
+
# @return [Array, String, nil] The embedded chunks or processed content, or
|
161
|
+
# nil if embedding is disabled or fails
|
84
162
|
def embed_source(source_io, source, count: nil)
|
85
163
|
@embedding.on? or return parse_source(source_io)
|
86
164
|
m = "Embedding #{italic { source_io&.content_type }} document #{source.to_s.inspect}."
|
@@ -127,6 +205,18 @@ module OllamaChat::SourceFetching
|
|
127
205
|
@documents.add(inputs, source:, batch_size: config.embedding.batch_size?)
|
128
206
|
end
|
129
207
|
|
208
|
+
|
209
|
+
# Embeds content from the specified source.
|
210
|
+
#
|
211
|
+
# This method fetches content from a given source (command, URL, or file) and
|
212
|
+
# processes it for embedding using the embed_source method. If embedding is
|
213
|
+
# disabled, it falls back to generating a summary instead.
|
214
|
+
#
|
215
|
+
# @param source [String] The source identifier which can be a command, URL,
|
216
|
+
# or file path
|
217
|
+
#
|
218
|
+
# @return [String, nil] The formatted embedding result or summary message, or
|
219
|
+
# nil if the operation fails
|
130
220
|
def embed(source)
|
131
221
|
if @embedding.on?
|
132
222
|
STDOUT.puts "Now embedding #{source.to_s.inspect}."
|