-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
117 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
require_relative 'lantern/version' | ||
require_relative 'lantern/model' | ||
require 'active_support' | ||
|
||
module Lantern | ||
class Error < StandardError; end | ||
end | ||
|
||
ActiveSupport.on_load(:active_record) do | ||
extend Lantern::Model | ||
end | ||
|
||
require_relative 'lantern/railtie' if defined?(Rails::Railtie) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Lantern | ||
module Model | ||
def has_neighbors(*attribute_names) | ||
attribute_names.map!(&:to_sym) | ||
|
||
scope :nearest_neighbors, ->(attribute_name, vector) { | ||
attribute_name = attribute_name.to_sym | ||
|
||
unless vector.all? { |v| v.is_a?(Integer) } | ||
vector = vector.map(&:to_f) | ||
end | ||
|
||
vector_literal = vector.join(',') | ||
order_expression = "#{quoted_table_name}.#{connection.quote_column_name(attribute_name)} <-> ARRAY[#{vector_literal}]" | ||
|
||
select("#{quoted_table_name}.*, #{order_expression} AS distance") | ||
.where.not(attribute_name => nil) | ||
.order(Arel.sql(order_expression)) | ||
} | ||
|
||
define_method :nearest_neighbors do |attribute_name| | ||
attribute_name = attribute_name.to_sym | ||
self.class | ||
.where.not(id: id) | ||
.nearest_neighbors(attribute_name, self[attribute_name]) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require "test_helper" | ||
|
||
class Item < ActiveRecord::Base | ||
has_neighbors :embedding | ||
end | ||
|
||
class LanternTest < Minitest::Test | ||
def setup | ||
@conn = ActiveRecord::Base.connection | ||
|
||
@conn.execute "DROP TABLE IF EXISTS items" | ||
@conn.execute <<~SQL | ||
CREATE TABLE items ( | ||
id serial PRIMARY KEY, | ||
embedding REAL[] | ||
) | ||
SQL | ||
|
||
@vectors = [ | ||
[1.0, 0.0, 0.0], | ||
[0.0, 1.0, 0.0], | ||
[0.0, 0.0, 1.0] | ||
] | ||
|
||
@vectors.each do |vector| | ||
@conn.execute "INSERT INTO items (embedding) VALUES (ARRAY#{vector})" | ||
end | ||
end | ||
|
||
def test_instance_nearest_neighbors | ||
item = Item.first | ||
neighbors = item.nearest_neighbors(:embedding).limit(2) | ||
|
||
assert_equal 2, neighbors.length | ||
assert_equal [2, 3], neighbors.pluck(:id) | ||
|
||
distances = neighbors.map { |neighbor| neighbor.distance } | ||
expected_distances = [2.0, 2.0] | ||
assert_equal expected_distances, distances | ||
end | ||
|
||
def test_class_nearest_neighbors | ||
neighbors = Item.nearest_neighbors(:embedding, [0.5, 0.0, 0.5]).limit(2) | ||
|
||
assert 2, neighbors.length | ||
assert_equal [1, 3], neighbors.pluck(:id) | ||
|
||
distances = neighbors.map { |neighbor| neighbor.distance } | ||
expected_distances = [0.5, 0.5] | ||
assert_equal expected_distances, distances | ||
end | ||
|
||
def test_invalid_dimensions | ||
assert_raises(ActiveRecord::StatementInvalid) do | ||
Item.nearest_neighbors(:embedding, [1.0, 0.0]).limit(2).to_a | ||
end | ||
end | ||
|
||
def teardown | ||
@conn.execute "DROP TABLE IF EXISTS items" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters