Closed gjtorikian closed 3 months ago
Ah. Sometimes column_attribute
is
#<Neighbor::Type::Vector:0x000000014de95d38 @precision=nil, @scale=nil, @limit=1536>
but other times, it's:
#<ActiveModel::Type::Value:0x000000014f97d588 @limit=nil, @precision=nil, @scale=nil>
Manually setting the cast_type
works: attribute :embedding_1536, Neighbor::Type::Vector.new
.
Hi @gjtorikian, I'm not sure how to reproduce the issue (one guess is another gem could be interfering with it). If you can create a minimal reproducible script, happy to look into it more.
require "bundler/inline"
gemfile do
source "https://rubygems.org"
gem "activerecord", require: "active_record"
gem "neighbor", github: "ankane/neighbor"
gem "pg"
end
ActiveRecord::Base.establish_connection adapter: "postgresql", database: "neighbor_repro"
ActiveRecord::Base.logger = ActiveSupport::Logger.new(STDOUT)
ActiveRecord::Schema.define do
enable_extension "vector"
create_table :items, force: :cascade do |t|
t.column :embedding, :vector, limit: 3
end
end
class Item < ActiveRecord::Base
has_neighbors :embedding
end
Item.create!(embedding: [1,2,3])
p Item.nearest_neighbors(:embedding, [0.9, 1.3, 1.1], distance: "euclidean").first(5)
Maybe similar issue here. After upgrading to rails 7.2, the tests works on my Mac, but fail in GitHub Actions with:
TypeError: can't cast Array
Setting the type manually fixes it:
attribute :embedding, Neighbor::Type::Vector.new # <-- Fix
has_neighbors :embedding, dimensions: 3072
Seems like it may be something with Rails 7.2, possibly related to https://github.com/rails/rails/issues/52607. Does changing config.eager_load
or enabling/disabling parallel tests fix it?
Disabling parallelization in tests also seems to work.
Was able to reproduce with parallel tests and config.eager_load = true
, but still trying to figure out the cause.
A temporary fix is to call reset_column_information
in parallelize_setup
.
class ActiveSupport::TestCase
parallelize_setup do |worker|
Item.reset_column_information
end
end
This is fixed by https://github.com/rails/rails/pull/52703.
I'm not quite sure what's causing it, but sometimes,
neighbor
throws the following error:This is despite the fact that none of the arguments in the
nearest_neighbors
scope change.