Class: Ollama::Documents
- Inherits:
-
Object
- Object
- Ollama::Documents
- Includes:
- Utils::Math, Utils::Width
- Defined in:
- lib/ollama/documents.rb,
lib/ollama/documents.rb,
lib/ollama/documents/cache/redis_backed_memory_cache.rb
Defined Under Namespace
Modules: Splitters Classes: Cache, MemoryCache, Record, RedisBackedMemoryCache, RedisCache
Instance Attribute Summary collapse
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#collection ⇒ Object
Returns the value of attribute collection.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#ollama ⇒ Object
readonly
Returns the value of attribute ollama.
Instance Method Summary collapse
- #[](text) ⇒ Object
- #[]=(text, record) ⇒ Object
- #add(inputs, batch_size: nil, source: nil, tags: []) ⇒ Object (also: #<<)
- #clear(tags: nil) ⇒ Object
- #collections ⇒ Object
- #default_collection ⇒ Object
- #delete(text) ⇒ Object
- #exist?(text) ⇒ Boolean
- #find(string, tags: nil, prompt: nil) ⇒ Object
- #find_where(string, text_size: nil, text_count: nil, **opts) ⇒ Object
-
#initialize(ollama:, model:, model_options: nil, collection: nil, cache: MemoryCache, redis_url: nil, debug: false) ⇒ Documents
constructor
A new instance of Documents.
- #size ⇒ Object
- #tags ⇒ Object
Methods included from Utils::Width
Methods included from Utils::Math
Constructor Details
#initialize(ollama:, model:, model_options: nil, collection: nil, cache: MemoryCache, redis_url: nil, debug: false) ⇒ Documents
Returns a new instance of Documents.
38 39 40 41 42 43 44 45 |
# File 'lib/ollama/documents.rb', line 38 def initialize(ollama:, model:, model_options: nil, collection: nil, cache: MemoryCache, redis_url: nil, debug: false) collection ||= default_collection @ollama, @model, , @collection = ollama, model, , collection.to_sym @redis_url = redis_url @cache = connect_cache(cache) @debug = debug end |
Instance Attribute Details
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
51 52 53 |
# File 'lib/ollama/documents.rb', line 51 def cache @cache end |
#collection ⇒ Object
Returns the value of attribute collection.
51 52 53 |
# File 'lib/ollama/documents.rb', line 51 def collection @collection end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
51 52 53 |
# File 'lib/ollama/documents.rb', line 51 def model @model end |
#ollama ⇒ Object (readonly)
Returns the value of attribute ollama.
51 52 53 |
# File 'lib/ollama/documents.rb', line 51 def ollama @ollama end |
Instance Method Details
#[](text) ⇒ Object
92 93 94 |
# File 'lib/ollama/documents.rb', line 92 def [](text) @cache[key(text)] end |
#[]=(text, record) ⇒ Object
96 97 98 |
# File 'lib/ollama/documents.rb', line 96 def []=(text, record) @cache[key(text)] = record end |
#add(inputs, batch_size: nil, source: nil, tags: []) ⇒ Object Also known as: <<
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/ollama/documents.rb', line 58 def add(inputs, batch_size: nil, source: nil, tags: []) inputs = Array(inputs) batch_size ||= 10 = Ollama::Utils::Tags.new(, source:) if source .add(File.basename(source).gsub(/\?.*/, ''), source:) end inputs.map! { |i| text = i.respond_to?(:read) ? i.read : i.to_s text } inputs.reject! { |i| exist?(i) } inputs.empty? and return self if @debug puts Ollama::Utils::ColorizeTexts.new(inputs) end batches = inputs.each_slice(batch_size). ( label: "Add #{truncate(tags.to_s(link: false), percentage: 25)}", total: inputs.size ) batches.each do |batch| = (model:, options: , input: batch) batch.zip() do |text, | norm = norm() self[text] = Record[text:, embedding:, norm:, source:, tags: .to_a] end .progress by: batch.size end .newline self end |
#clear(tags: nil) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/ollama/documents.rb', line 112 def clear(tags: nil) if = Ollama::Utils::Tags.new(Array()).to_a @cache.each do |key, record| if ( & record.).size >= 1 @cache.delete(@cache.unpre(key)) end end else @cache.clear end self end |
#collections ⇒ Object
160 161 162 |
# File 'lib/ollama/documents.rb', line 160 def collections ([ default_collection ] + @cache.collections('%s-' % self.class)).uniq end |
#default_collection ⇒ Object
47 48 49 |
# File 'lib/ollama/documents.rb', line 47 def default_collection :default end |
#delete(text) ⇒ Object
104 105 106 |
# File 'lib/ollama/documents.rb', line 104 def delete(text) @cache.delete(key(text)) end |
#exist?(text) ⇒ Boolean
100 101 102 |
# File 'lib/ollama/documents.rb', line 100 def exist?(text) @cache.key?(key(text)) end |
#find(string, tags: nil, prompt: nil) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/ollama/documents.rb', line 126 def find(string, tags: nil, prompt: nil) needle = convert_to_vector(string, prompt:) needle_norm = norm(needle) records = @cache if = Ollama::Utils::Tags.new().to_a records = records.select { |_key, record| ( & record.).size >= 1 } end records = records.sort_by { |key, record| record.key = key record.similarity = cosine_similarity( a: needle, b: record., a_norm: needle_norm, b_norm: record.norm, ) } records.transpose.last&.reverse.to_a end |
#find_where(string, text_size: nil, text_count: nil, **opts) ⇒ Object
146 147 148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/ollama/documents.rb', line 146 def find_where(string, text_size: nil, text_count: nil, **opts) records = find(string, **opts) size, count = 0, 0 records.take_while do |record| if text_size and (size += record.text.size) > text_size next false end if text_count and (count += 1) > text_count next false end true end end |
#size ⇒ Object
108 109 110 |
# File 'lib/ollama/documents.rb', line 108 def size @cache.size end |
#tags ⇒ Object
164 165 166 167 168 169 170 |
# File 'lib/ollama/documents.rb', line 164 def @cache.each_with_object(Ollama::Utils::Tags.new) do |(_, record), t| record..each do |tag| t.add(tag, source: record.source) end end end |