Class: Ollama::Documents

Inherits:
Object
  • 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

Methods included from Utils::Width

truncate, width, wrap

Methods included from Utils::Math

#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

#cacheObject (readonly)

Returns the value of attribute cache.



51
52
53
# File 'lib/ollama/documents.rb', line 51

def cache
  @cache
end

#collectionObject

Returns the value of attribute collection.



51
52
53
# File 'lib/ollama/documents.rb', line 51

def collection
  @collection
end

#modelObject (readonly)

Returns the value of attribute model.



51
52
53
# File 'lib/ollama/documents.rb', line 51

def model
  @model
end

#ollamaObject (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



89
90
91
# File 'lib/ollama/documents.rb', line 89

def [](text)
  @cache[key(text)]
end

#[]=(text, record) ⇒ Object



93
94
95
# File 'lib/ollama/documents.rb', line 93

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
# 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 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
  if @debug
    puts Ollama::Utils::ColorizeTexts.new(inputs)
  end
  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



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ollama/documents.rb', line 109

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

#collectionsObject



157
158
159
# File 'lib/ollama/documents.rb', line 157

def collections
  ([ default_collection ] + @cache.collections('%s-' % self.class)).uniq
end

#default_collectionObject



47
48
49
# File 'lib/ollama/documents.rb', line 47

def default_collection
  :default
end

#delete(text) ⇒ Object



101
102
103
# File 'lib/ollama/documents.rb', line 101

def delete(text)
  @cache.delete(key(text))
end

#exist?(text) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/ollama/documents.rb', line 97

def exist?(text)
  @cache.key?(key(text))
end

#find(string, tags: nil, prompt: nil) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ollama/documents.rb', line 123

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



143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/ollama/documents.rb', line 143

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

#sizeObject



105
106
107
# File 'lib/ollama/documents.rb', line 105

def size
  @cache.size
end

#tagsObject



161
162
163
# File 'lib/ollama/documents.rb', line 161

def tags
  @cache.inject(Ollama::Utils::Tags.new) { |t, (_, record)| t.merge(record.tags) }
end