Class: Zvec::RubyLLM::Store
- Inherits:
-
Object
- Object
- Zvec::RubyLLM::Store
- Defined in:
- lib/zvec/ruby_llm.rb
Overview
A vector store backend for the ruby_llm gem.
Provides a simple add/search/delete interface on top of a Collection. Compatible with the ruby_llm vector store protocol.
Constant Summary collapse
- DEFAULT_VECTOR_FIELD =
Returns default vector field name.
"embedding"- DEFAULT_CONTENT_FIELD =
Returns default content field name.
"content"
Instance Attribute Summary collapse
-
#collection ⇒ Zvec::Collection
readonly
The underlying collection.
-
#dimension ⇒ Integer
readonly
The vector dimension.
Instance Method Summary collapse
-
#add(id, embedding:, content: nil, metadata: {}) ⇒ Array
Add a document with its embedding and optional metadata.
-
#add_many(docs) ⇒ Array
Batch-add multiple documents at once.
-
#count ⇒ Integer
Return the number of documents in the store.
-
#delete(*ids) ⇒ Array
Delete documents by primary key(s).
-
#fetch(*ids) ⇒ Hash{String => Zvec::Doc}
Fetch documents by primary key(s).
-
#flush ⇒ self
Flush pending writes to disk.
-
#initialize(path, dimension:, metric: :cosine, vector_field: DEFAULT_VECTOR_FIELD, content_field: DEFAULT_CONTENT_FIELD) ⇒ Store
constructor
Create a new store, opening an existing collection or creating one.
-
#search(query_vector, top_k: 10, filter: nil) ⇒ Array<Hash>
Search for similar vectors.
Constructor Details
#initialize(path, dimension:, metric: :cosine, vector_field: DEFAULT_VECTOR_FIELD, content_field: DEFAULT_CONTENT_FIELD) ⇒ Store
Create a new store, opening an existing collection or creating one.
41 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 68 69 70 |
# File 'lib/zvec/ruby_llm.rb', line 41 def initialize(path, dimension:, metric: :cosine, vector_field: DEFAULT_VECTOR_FIELD, content_field: DEFAULT_CONTENT_FIELD) @vector_field = vector_field.to_s @content_field = content_field.to_s @dimension = dimension metric_type = case metric.to_sym when :cosine then Zvec::DataTypes::COSINE when :l2 then Zvec::DataTypes::L2 when :ip then Zvec::DataTypes::IP else raise ArgumentError, "Unknown metric: #{metric}" end cf = @content_field vf = @vector_field dim = dimension schema = Zvec::Schema.new("ruby_llm_store") do string cf, nullable: true vector vf, dimension: dim, index: Zvec::Ext::HnswIndexParams.new(metric_type) end @schema = schema if Dir.exist?(path) @collection = Zvec::Collection.open(path) else @collection = Zvec::Collection.create_and_open(path, schema) end end |
Instance Attribute Details
#collection ⇒ Zvec::Collection (readonly)
Returns the underlying collection.
26 27 28 |
# File 'lib/zvec/ruby_llm.rb', line 26 def collection @collection end |
#dimension ⇒ Integer (readonly)
Returns the vector dimension.
28 29 30 |
# File 'lib/zvec/ruby_llm.rb', line 28 def dimension @dimension end |
Instance Method Details
#add(id, embedding:, content: nil, metadata: {}) ⇒ Array
Add a document with its embedding and optional metadata.
82 83 84 85 86 87 88 |
# File 'lib/zvec/ruby_llm.rb', line 82 def add(id, embedding:, content: nil, metadata: {}) doc = Zvec::Doc.new(pk: id, schema: @schema) doc[@vector_field] = doc[@content_field] = content if content .each { |k, v| doc[k] = v } @collection.insert(doc) end |
#add_many(docs) ⇒ Array
Batch-add multiple documents at once.
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/zvec/ruby_llm.rb', line 104 def add_many(docs) zvec_docs = docs.map do |d| doc = Zvec::Doc.new(pk: d[:id], schema: @schema) doc[@vector_field] = d[:embedding] doc[@content_field] = d[:content] if d[:content] (d[:metadata] || {}).each { |k, v| doc[k] = v } doc end @collection.insert(zvec_docs) end |
#count ⇒ Integer
Return the number of documents in the store.
175 176 177 |
# File 'lib/zvec/ruby_llm.rb', line 175 def count @collection.doc_count end |
#delete(*ids) ⇒ Array
Delete documents by primary key(s).
153 154 155 |
# File 'lib/zvec/ruby_llm.rb', line 153 def delete(*ids) @collection.delete(*ids.flatten) end |
#fetch(*ids) ⇒ Hash{String => Zvec::Doc}
Fetch documents by primary key(s).
161 162 163 |
# File 'lib/zvec/ruby_llm.rb', line 161 def fetch(*ids) @collection.fetch(*ids.flatten) end |
#flush ⇒ self
Flush pending writes to disk.
168 169 170 |
# File 'lib/zvec/ruby_llm.rb', line 168 def flush @collection.flush end |
#search(query_vector, top_k: 10, filter: nil) ⇒ Array<Hash>
Search for similar vectors.
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/zvec/ruby_llm.rb', line 132 def search(query_vector, top_k: 10, filter: nil) results = @collection.query( field_name: @vector_field, vector: query_vector, topk: top_k, filter: filter ) results.map do |doc| { id: doc.pk, score: doc.score, content: doc[@content_field], metadata: doc.to_h.reject { |k, _| ["pk", "score", @vector_field, @content_field].include?(k) } } end end |