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: default_collection, cache: MemoryCache, redis_url: nil) ⇒ 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: default_collection, cache: MemoryCache, redis_url: nil) ⇒ Documents
Returns a new instance of Documents.
38
39
40
41
|
# File 'lib/ollama/documents.rb', line 38
def initialize(ollama:, model:, model_options: nil, collection: default_collection, cache: MemoryCache, redis_url: nil)
@ollama, @model, @model_options, @collection = ollama, model, model_options, collection.to_sym
@cache, @redis_url = connect_cache(cache), redis_url
end
|
Instance Attribute Details
#collection ⇒ Object
Returns the value of attribute collection.
47
48
49
|
# File 'lib/ollama/documents.rb', line 47
def collection
@collection
end
|
#model ⇒ Object
Returns the value of attribute model.
47
48
49
|
# File 'lib/ollama/documents.rb', line 47
def model
@model
end
|
#ollama ⇒ Object
Returns the value of attribute ollama.
47
48
49
|
# File 'lib/ollama/documents.rb', line 47
def ollama
@ollama
end
|
Instance Method Details
#[](text) ⇒ Object
82
83
84
|
# File 'lib/ollama/documents.rb', line 82
def [](text)
@cache[key(text)]
end
|
#[]=(text, record) ⇒ Object
86
87
88
|
# File 'lib/ollama/documents.rb', line 86
def []=(text, record)
@cache[key(text)] = record
end
|
#add(inputs, batch_size: 10, source: nil, tags: []) ⇒ Object
Also known as:
<<
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/ollama/documents.rb', line 54
def add(inputs, batch_size: 10, source: nil, tags: [])
inputs = Array(inputs)
tags = Ollama::Utils::Tags.new(tags)
source and tags.add File.basename(source).gsub(/\?.*/, '')
inputs.map! { |i|
text = i.respond_to?(:read) ? i.read : i.to_s
text
}
inputs.reject! { |i| exist?(i) }
inputs.empty? and return self
batches = inputs.each_slice(batch_size).
with_infobar(
label: "Add #{truncate(tags.to_s, 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
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/ollama/documents.rb', line 102
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
150
151
152
|
# File 'lib/ollama/documents.rb', line 150
def collections
([ default_collection ] + @cache.collections('%s-' % self.class)).uniq
end
|
#default_collection ⇒ Object
43
44
45
|
# File 'lib/ollama/documents.rb', line 43
def default_collection
:default
end
|
#delete(text) ⇒ Object
94
95
96
|
# File 'lib/ollama/documents.rb', line 94
def delete(text)
@cache.delete(key(text))
end
|
#exist?(text) ⇒ Boolean
90
91
92
|
# File 'lib/ollama/documents.rb', line 90
def exist?(text)
@cache.key?(key(text))
end
|
#find(string, tags: nil, prompt: nil) ⇒ Object
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/ollama/documents.rb', line 116
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
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/ollama/documents.rb', line 136
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
98
99
100
|
# File 'lib/ollama/documents.rb', line 98
def size
@cache.size
end
|
154
155
156
|
# File 'lib/ollama/documents.rb', line 154
def tags
@cache.inject(Ollama::Utils::Tags.new) { |t, (_, record)| t.merge(record.tags) }
end
|