neighbor 0.5.1 → 0.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +3 -11
- data/lib/neighbor/postgresql.rb +15 -0
- data/lib/neighbor/version.rb +1 -1
- metadata +3 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fb8418a71159a849442d146643f732c28d10a5237d94a5b5b7d7466a224a40b5
|
4
|
+
data.tar.gz: 8a7ededb3071fdef4a77bbbdf4596d74ed323327d4d29ea15538e344d25af2f7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05b3b8ccd07570f531aef0132a80563aef046b88508e732b2f16fe02f6c0303bd6f6fdb00804a646845086ba7fd133358503a7c7b69b098c02f548434c859575
|
7
|
+
data.tar.gz: 9bcb61d4e1453e30daeab1fff0364c023278405d170b435cdfb262aac0aa0d91622e19471fbf3f3076e4c944d09318dc81cf334fdac7767fa6f72d95c8560907
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -306,12 +306,14 @@ class AddEmbeddingToItems < ActiveRecord::Migration[8.0]
|
|
306
306
|
def change
|
307
307
|
# Rails 8+
|
308
308
|
create_virtual_table :items, :vec0, [
|
309
|
+
"id integer PRIMARY KEY AUTOINCREMENT NOT NULL",
|
309
310
|
"embedding float[3] distance_metric=L2"
|
310
311
|
]
|
311
312
|
|
312
313
|
# Rails < 8
|
313
314
|
execute <<~SQL
|
314
315
|
CREATE VIRTUAL TABLE items USING vec0(
|
316
|
+
id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
315
317
|
embedding float[3] distance_metric=L2
|
316
318
|
)
|
317
319
|
SQL
|
@@ -329,16 +331,6 @@ ActiveRecord::SchemaDumper.ignore_tables += [
|
|
329
331
|
]
|
330
332
|
```
|
331
333
|
|
332
|
-
Create a model with `rowid` as the primary key
|
333
|
-
|
334
|
-
```ruby
|
335
|
-
class Item < ApplicationRecord
|
336
|
-
self.primary_key = "rowid"
|
337
|
-
|
338
|
-
has_neighbors :embedding, dimensions: 3
|
339
|
-
end
|
340
|
-
```
|
341
|
-
|
342
334
|
Get the `k` nearest neighbors
|
343
335
|
|
344
336
|
```ruby
|
@@ -348,7 +340,7 @@ Item.where("embedding MATCH ?", [1, 2, 3].to_s).where(k: 5).order(:distance)
|
|
348
340
|
Filter by primary key
|
349
341
|
|
350
342
|
```ruby
|
351
|
-
Item.where(
|
343
|
+
Item.where(id: [2, 3]).where("embedding MATCH ?", [1, 2, 3].to_s).where(k: 5).order(:distance)
|
352
344
|
```
|
353
345
|
|
354
346
|
### Int8 Vectors
|
data/lib/neighbor/postgresql.rb
CHANGED
@@ -19,6 +19,9 @@ module Neighbor
|
|
19
19
|
|
20
20
|
# prevent unknown OID warning
|
21
21
|
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.singleton_class.prepend(RegisterTypes)
|
22
|
+
|
23
|
+
# support vector[]/halfvec[]
|
24
|
+
ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.prepend(ArrayMethods)
|
22
25
|
end
|
23
26
|
|
24
27
|
module RegisterTypes
|
@@ -39,5 +42,17 @@ module Neighbor
|
|
39
42
|
end
|
40
43
|
end
|
41
44
|
end
|
45
|
+
|
46
|
+
ArrayWrapper = Struct.new(:to_a)
|
47
|
+
|
48
|
+
module ArrayMethods
|
49
|
+
def type_cast_array(value, method, ...)
|
50
|
+
if (subtype.is_a?(Neighbor::Type::Vector) || subtype.is_a?(Neighbor::Type::Halfvec)) && method != :deserialize && value.is_a?(::Array) && value.all? { |v| v.is_a?(::Numeric) }
|
51
|
+
super(ArrayWrapper.new(value), method, ...)
|
52
|
+
else
|
53
|
+
super
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
42
57
|
end
|
43
58
|
end
|
data/lib/neighbor/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: neighbor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Kane
|
8
|
-
autorequire:
|
9
8
|
bindir: bin
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-01-05 00:00:00.000000000 Z
|
12
11
|
dependencies:
|
13
12
|
- !ruby/object:Gem::Dependency
|
14
13
|
name: activerecord
|
@@ -24,7 +23,6 @@ dependencies:
|
|
24
23
|
- - ">="
|
25
24
|
- !ruby/object:Gem::Version
|
26
25
|
version: '7'
|
27
|
-
description:
|
28
26
|
email: andrew@ankane.org
|
29
27
|
executables: []
|
30
28
|
extensions: []
|
@@ -62,7 +60,6 @@ homepage: https://github.com/ankane/neighbor
|
|
62
60
|
licenses:
|
63
61
|
- MIT
|
64
62
|
metadata: {}
|
65
|
-
post_install_message:
|
66
63
|
rdoc_options: []
|
67
64
|
require_paths:
|
68
65
|
- lib
|
@@ -77,8 +74,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
77
74
|
- !ruby/object:Gem::Version
|
78
75
|
version: '0'
|
79
76
|
requirements: []
|
80
|
-
rubygems_version: 3.
|
81
|
-
signing_key:
|
77
|
+
rubygems_version: 3.6.2
|
82
78
|
specification_version: 4
|
83
79
|
summary: Nearest neighbor search for Rails
|
84
80
|
test_files: []
|