neighbor 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5818fdfa27cc4b0678fb125e82d2fa0a3066ea173674ca541987b9084a94ac14
4
- data.tar.gz: e874541532172dce9932a98506bd5ce7866a0f5c3e600ffd9d6473f226829368
3
+ metadata.gz: 0c8b5d19222742f33f51f2c30f9d03108ebd3ed99908a7e9dd5f4e49caa2e225
4
+ data.tar.gz: c9cfa942f2cdd8b9757c9ecfe5e89d0aced11263f8a559004ee15fa0c8adb3f4
5
5
  SHA512:
6
- metadata.gz: 96211da20caf70383018ca626cbd83bbda5e4ffaf95e5a9b5405686b192643916325f89a652b4ba9e34e17daa1b84e8045cc644b606bab8aa6ea339ed4d7ea0d
7
- data.tar.gz: bf855dc34617d489618417b9c807969f828358de19462458b7c09e1743102a8b9967b32b09660fab35f35543b77d588297c69971edb7442af0995cdca3b6ff8e
6
+ metadata.gz: e9e0050031ce7691baa9242b3b6b5aa76afb1fe7c63575129e68b2f5c027143b3c08f68a7babfcf2a9b02f1d9327679f75e9c40b95ac2245ea7c8dd3025d3cdb
7
+ data.tar.gz: a9c505740cba454437617733d4025360848a16ef9a4c9c83fc16d5bc82a3e5521c77e3cba874ef3cf318cf3a1e319567958a6156481f7fd82ef72ebaa87d97eb
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 0.3.2 (2023-12-12)
2
+
3
+ - Added deprecation warning for `has_neighbors` without an attribute name
4
+ - Added deprecation warning for `nearest_neighbors` without an attribute name
5
+
1
6
  ## 0.3.1 (2023-09-25)
2
7
 
3
8
  - Added support for passing multiple attributes to `has_neighbors`
data/README.md CHANGED
@@ -14,7 +14,7 @@ gem "neighbor"
14
14
 
15
15
  ## Choose An Extension
16
16
 
17
- Neighbor supports two extensions: [cube](https://www.postgresql.org/docs/current/cube.html) and [vector](https://github.com/pgvector/pgvector). cube ships with Postgres, while vector supports approximate nearest neighbor search.
17
+ Neighbor supports two extensions: [cube](https://www.postgresql.org/docs/current/cube.html) and [vector](https://github.com/pgvector/pgvector). cube ships with Postgres, while vector supports more dimensions and approximate nearest neighbor search.
18
18
 
19
19
  For cube, run:
20
20
 
@@ -35,7 +35,7 @@ rails db:migrate
35
35
  Create a migration
36
36
 
37
37
  ```ruby
38
- class AddNeighborVectorToItems < ActiveRecord::Migration[7.0]
38
+ class AddEmbeddingToItems < ActiveRecord::Migration[7.1]
39
39
  def change
40
40
  add_column :items, :embedding, :cube
41
41
  # or
@@ -114,7 +114,7 @@ end
114
114
  For vector, add an approximate index to speed up queries. Create a migration with:
115
115
 
116
116
  ```ruby
117
- class AddIndexToItemsNeighborVector < ActiveRecord::Migration[7.0]
117
+ class AddIndexToItemsEmbedding < ActiveRecord::Migration[7.1]
118
118
  def change
119
119
  add_index :items, :embedding, using: :ivfflat, opclass: :vector_l2_ops
120
120
  # or with pgvector 0.5.0+
@@ -2,6 +2,7 @@ module Neighbor
2
2
  module Model
3
3
  def has_neighbors(*attribute_names, dimensions: nil, normalize: nil)
4
4
  if attribute_names.empty?
5
+ warn "[neighbor] has_neighbors without an attribute name is deprecated"
5
6
  attribute_names << :neighbor_vector
6
7
  else
7
8
  attribute_names.map!(&:to_sym)
@@ -44,6 +45,7 @@ module Neighbor
44
45
  raise ArgumentError, "unknown keywords: #{options.keys.map(&:inspect).join(", ")}" if options.any?
45
46
 
46
47
  if vector.nil? && !attribute_name.nil? && attribute_name.respond_to?(:to_a)
48
+ warn "[neighbor] nearest_neighbors without an attribute name is deprecated"
47
49
  vector = attribute_name
48
50
  attribute_name = :neighbor_vector
49
51
  end
@@ -123,7 +125,11 @@ module Neighbor
123
125
  .order(Arel.sql(order))
124
126
  }
125
127
 
126
- def nearest_neighbors(attribute_name = :neighbor_vector, **options)
128
+ def nearest_neighbors(attribute_name = nil, **options)
129
+ if attribute_name.nil?
130
+ warn "[neighbor] nearest_neighbors without an attribute name is deprecated"
131
+ attribute_name = :neighbor_vector
132
+ end
127
133
  attribute_name = attribute_name.to_sym
128
134
  # important! check if neighbor attribute before calling send
129
135
  raise ArgumentError, "Invalid attribute" unless self.class.neighbor_attributes[attribute_name]
@@ -0,0 +1,38 @@
1
+ module Neighbor
2
+ module Type
3
+ class Cube < ActiveRecord::Type::String
4
+ def type
5
+ :cube
6
+ end
7
+
8
+ def cast(value)
9
+ if value.is_a?(Array)
10
+ if value.first.is_a?(Array)
11
+ value.map { |v| cast_point(v) }.join(", ")
12
+ else
13
+ cast_point(value)
14
+ end
15
+ else
16
+ super
17
+ end
18
+ end
19
+
20
+ # TODO uncomment in 0.4.0
21
+ # def deserialize(value)
22
+ # if value.nil?
23
+ # super
24
+ # elsif value.include?("),(")
25
+ # value[1..-1].split("),(").map { |v| v.split(",").map(&:to_f) }
26
+ # else
27
+ # value[1..-1].split(",").map(&:to_f)
28
+ # end
29
+ # end
30
+
31
+ private
32
+
33
+ def cast_point(value)
34
+ "(#{value.map(&:to_f).join(", ")})"
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,14 @@
1
+ module Neighbor
2
+ module Type
3
+ class Vector < ActiveRecord::Type::String
4
+ def type
5
+ :vector
6
+ end
7
+
8
+ # TODO uncomment in 0.4.0
9
+ # def deserialize(value)
10
+ # value[1..-1].split(",").map(&:to_f) unless value.nil?
11
+ # end
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module Neighbor
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
data/lib/neighbor.rb CHANGED
@@ -10,10 +10,10 @@ module Neighbor
10
10
  module RegisterTypes
11
11
  def initialize_type_map(m = type_map)
12
12
  super
13
- m.register_type "cube", ActiveRecord::ConnectionAdapters::PostgreSQL::OID::SpecializedString.new(:cube)
13
+ m.register_type "cube", Type::Cube.new
14
14
  m.register_type "vector" do |_, _, sql_type|
15
15
  limit = extract_limit(sql_type)
16
- ActiveRecord::ConnectionAdapters::PostgreSQL::OID::SpecializedString.new(:vector, limit: limit)
16
+ Type::Vector.new(limit: limit)
17
17
  end
18
18
  end
19
19
  end
@@ -22,6 +22,8 @@ end
22
22
  ActiveSupport.on_load(:active_record) do
23
23
  require_relative "neighbor/model"
24
24
  require_relative "neighbor/vector"
25
+ require_relative "neighbor/type/cube"
26
+ require_relative "neighbor/type/vector"
25
27
 
26
28
  extend Neighbor::Model
27
29
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: neighbor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-09-26 00:00:00.000000000 Z
11
+ date: 2023-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -40,6 +40,8 @@ files:
40
40
  - lib/neighbor.rb
41
41
  - lib/neighbor/model.rb
42
42
  - lib/neighbor/railtie.rb
43
+ - lib/neighbor/type/cube.rb
44
+ - lib/neighbor/type/vector.rb
43
45
  - lib/neighbor/vector.rb
44
46
  - lib/neighbor/version.rb
45
47
  homepage: https://github.com/ankane/neighbor