Class: Ollama::Documents
- Inherits:
-
Object
show all
- 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
Instance Method Summary
collapse
-
#[](text) ⇒ Object
-
#[]=(text, record) ⇒ Object
-
#add(inputs, batch_size: 10, 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
truncate, width, wrap
#cosine_similarity, #norm
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, @model_options, @collection =
ollama, model, model_options, collection.to_sym
@redis_url = redis_url
@cache = connect_cache(cache)
@debug = debug
end
|
Instance Attribute Details
#cache ⇒ Object
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
Returns the value of attribute model.
51
52
53
|
# File 'lib/ollama/documents.rb', line 51
def model
@model
end
|
#ollama ⇒ Object
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
91
92
93
|
# File 'lib/ollama/documents.rb', line 91
def [](text)
@cache[key(text)]
end
|
#[]=(text, record) ⇒ Object
95
96
97
|
# File 'lib/ollama/documents.rb', line 95
def []=(text, record)
@cache[key(text)] = record
end
|
#add(inputs, batch_size: 10, 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
|
# File 'lib/ollama/documents.rb', line 58
def add(inputs, batch_size: 10, source: nil, tags: [])
inputs = Array(inputs)
tags = Ollama::Utils::Tags.new(tags, source:)
if source
tags.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).
with_infobar(
label: "Add #{truncate(tags.to_s(link: false), percentage: 25)}",
total: inputs.size
)
batches.each do |batch|
embeddings = fetch_embeddings(model:, options: @model_options, input: batch)
batch.zip(embeddings) do |text, embedding|
norm = norm(embedding)
self[text] = Record[text:, embedding:, norm:, source:, tags: tags.to_a]
end
infobar.progress by: batch.size
end
infobar.newline
self
end
|
#clear(tags: nil) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
123
|
# File 'lib/ollama/documents.rb', line 111
def clear(tags: nil)
if tags
tags = Ollama::Utils::Tags.new(Array(tags)).to_a
@cache.each do |key, record|
if (tags & record.tags).size >= 1
@cache.delete(@cache.unpre(key))
end
end
else
@cache.clear
end
self
end
|
#collections ⇒ Object
159
160
161
|
# File 'lib/ollama/documents.rb', line 159
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
103
104
105
|
# File 'lib/ollama/documents.rb', line 103
def delete(text)
@cache.delete(key(text))
end
|
#exist?(text) ⇒ Boolean
99
100
101
|
# File 'lib/ollama/documents.rb', line 99
def exist?(text)
@cache.key?(key(text))
end
|
#find(string, tags: nil, prompt: nil) ⇒ Object
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
# File 'lib/ollama/documents.rb', line 125
def find(string, tags: nil, prompt: nil)
needle = convert_to_vector(string, prompt:)
needle_norm = norm(needle)
records = @cache
if tags
tags = Ollama::Utils::Tags.new(tags).to_a
records = records.select { |_key, record| (tags & record.tags).size >= 1 }
end
records = records.sort_by { |key, record|
record.key = key
record.similarity = cosine_similarity(
a: needle,
b: record.embedding,
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
145
146
147
148
149
150
151
152
153
154
155
156
157
|
# File 'lib/ollama/documents.rb', line 145
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
107
108
109
|
# File 'lib/ollama/documents.rb', line 107
def size
@cache.size
end
|
163
164
165
166
167
168
169
|
# File 'lib/ollama/documents.rb', line 163
def tags
@cache.each_with_object(Ollama::Utils::Tags.new) do |(_, record), t|
record.tags.each do |tag|
t.add(tag, source: record.source)
end
end
end
|