Class: Ollama::Documents

Inherits:
Object
  • Object
show all
Includes:
Utils::Math
Defined in:
lib/ollama/documents.rb,
lib/ollama/documents.rb

Defined Under Namespace

Modules: Splitters Classes: MemoryCache, Record, RedisCache

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::Math

#cosine_similarity, #norm

Constructor Details

#initialize(ollama:, model:, model_options: nil, collection: :default, cache: MemoryCache, redis_url: nil) ⇒ Documents

Returns a new instance of Documents.



30
31
32
33
# File 'lib/ollama/documents.rb', line 30

def initialize(ollama:, model:, model_options: nil, collection: :default, cache: MemoryCache, redis_url: nil)
  @ollama, @model, @model_options, @collection = ollama, model, model_options, collection
  @cache, @redis_url = connect_cache(cache), redis_url
end

Instance Attribute Details

#collectionObject

Returns the value of attribute collection.



35
36
37
# File 'lib/ollama/documents.rb', line 35

def collection
  @collection
end

#modelObject (readonly)

Returns the value of attribute model.



35
36
37
# File 'lib/ollama/documents.rb', line 35

def model
  @model
end

#ollamaObject (readonly)

Returns the value of attribute ollama.



35
36
37
# File 'lib/ollama/documents.rb', line 35

def ollama
  @ollama
end

Instance Method Details

#[](text) ⇒ Object



70
71
72
# File 'lib/ollama/documents.rb', line 70

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

#[]=(text, record) ⇒ Object



74
75
76
# File 'lib/ollama/documents.rb', line 74

def []=(text, record)
  @cache[key(text)] = record
end

#add(inputs, batch_size: 10, source: nil, tags: []) ⇒ Object Also known as: <<



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ollama/documents.rb', line 42

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)
  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 #{tags}",
      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

#clearObject



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

def clear
  @cache.clear
end

#collectionsObject



114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ollama/documents.rb', line 114

def collections
  case @cache
  when MemoryCache
    [ @collection ]
  when RedisCache
    prefix = '%s-' % self.class
    Documents::RedisCache.new(prefix:, url: @redis_url).map { _1[/#{prefix}(.*)-/, 1] }.uniq
  else
    []
  end
end

#delete(text) ⇒ Object



82
83
84
# File 'lib/ollama/documents.rb', line 82

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

#exist?(text) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/ollama/documents.rb', line 78

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

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



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/ollama/documents.rb', line 94

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)
    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

#sizeObject



86
87
88
# File 'lib/ollama/documents.rb', line 86

def size
  @cache.size
end

#tagsObject



126
127
128
# File 'lib/ollama/documents.rb', line 126

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