Class: Parse::Vector

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/parse/model/vector.rb

Overview

Wraps a dense numeric embedding stored on a Parse object. Backs the ‘:vector` property data type and the `embed` DSL. The value is just an array of Floats — `Parse::Vector` adds dimension awareness, finite-value validation, and JSON serialization helpers so the provider/index plumbing can rely on a single concrete shape.

Examples:

class Document < Parse::Object
  property :embedding, :vector, dimensions: 1536,
                                provider: :openai,
                                model: "text-embedding-3-small",
                                similarity: :cosine
end

doc = Document.new(embedding: Array.new(1536) { rand })
doc.embedding         # => #<Parse::Vector dims=1536>
doc.embedding.to_a    # => [0.123, 0.456, ...]

Constant Summary collapse

MAX_DIMENSIONS =

Maximum dimensions a Parse::Vector will accept. Atlas Vector Search caps individual vector indexes at 8192 dims as of MongoDB 7.0; we keep some headroom but still refuse pathological inputs that would blow up memory.

16384

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values) ⇒ Vector

Returns a new instance of Vector.

Parameters:

Raises:

  • (ArgumentError)

    if any element is not finite numeric



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/parse/model/vector.rb', line 38

def initialize(values)
  values = values.values if values.is_a?(Parse::Vector)
  unless values.is_a?(Array)
    raise ArgumentError, "[Parse::Vector] expected Array, got #{values.class}."
  end
  if values.length > MAX_DIMENSIONS
    raise ArgumentError,
          "[Parse::Vector] refusing #{values.length}-dim vector; max #{MAX_DIMENSIONS}."
  end
  @values = values.map do |x|
    unless x.is_a?(Numeric) && x.respond_to?(:finite?) && x.finite?
      raise ArgumentError,
            "[Parse::Vector] all elements must be finite Numeric (got #{x.inspect})."
    end
    x.to_f
  end.freeze
end

Instance Attribute Details

#valuesArray<Float> (readonly)

Returns the underlying float array.

Returns:

  • (Array<Float>)

    the underlying float array



34
35
36
# File 'lib/parse/model/vector.rb', line 34

def values
  @values
end

Instance Method Details

#==(other) ⇒ Boolean Also known as: eql?

Returns equality by element-wise comparison.

Returns:

  • (Boolean)

    equality by element-wise comparison



84
85
86
87
88
89
90
# File 'lib/parse/model/vector.rb', line 84

def ==(other)
  case other
  when Parse::Vector then @values == other.values
  when Array         then @values == other
  else false
  end
end

#as_jsonArray<Float>

MongoDB / Parse server store this as a plain BSON array.

Returns:

  • (Array<Float>)

    passes the float array through as JSON



70
71
72
# File 'lib/parse/model/vector.rb', line 70

def as_json(*)
  @values
end

#dimensionsInteger Also known as: length, size

Returns number of dimensions.

Returns:

  • (Integer)

    number of dimensions



57
58
59
# File 'lib/parse/model/vector.rb', line 57

def dimensions
  @values.length
end

#each(&block) ⇒ Object



79
80
81
# File 'lib/parse/model/vector.rb', line 79

def each(&block)
  @values.each(&block)
end

#hashObject



93
94
95
# File 'lib/parse/model/vector.rb', line 93

def hash
  @values.hash
end

#to_aArray<Float>

Returns the underlying float array.

Returns:

  • (Array<Float>)

    the underlying float array



64
65
66
# File 'lib/parse/model/vector.rb', line 64

def to_a
  @values.dup
end

#to_json(*opts) ⇒ String

Returns JSON representation.

Returns:

  • (String)

    JSON representation



75
76
77
# File 'lib/parse/model/vector.rb', line 75

def to_json(*opts)
  @values.to_json(*opts)
end