From 95cc23a430fc71f66d7b805dbc347dd1d2c502bf Mon Sep 17 00:00:00 2001 From: Andrew Kane Date: Wed, 1 May 2024 16:46:56 -0700 Subject: [PATCH] Added L1 distance --- .github/workflows/build.yml | 2 +- CHANGELOG.md | 1 + src/laravel/Distance.php | 1 + src/laravel/HasNeighbors.php | 3 +++ tests/LaravelTest.php | 9 +++++++++ 5 files changed, 15 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6985963..30c57a9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -22,7 +22,7 @@ jobs: dev-files: true - run: | cd /tmp - git clone --branch v0.6.0 https://github.com/pgvector/pgvector.git + git clone --branch v0.7.0 https://github.com/pgvector/pgvector.git cd pgvector make sudo make install diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b2deff..f0765ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ## 0.2.0 (unreleased) +- Added `L1` distance - Changed `Distance` to enum - Dropped support for PHP < 8.1 - Dropped support for Laravel < 10 diff --git a/src/laravel/Distance.php b/src/laravel/Distance.php index d698d81..44c0ab1 100644 --- a/src/laravel/Distance.php +++ b/src/laravel/Distance.php @@ -7,4 +7,5 @@ enum Distance case L2; case InnerProduct; case Cosine; + case L1; } diff --git a/src/laravel/HasNeighbors.php b/src/laravel/HasNeighbors.php index fd8da2a..48147a3 100644 --- a/src/laravel/HasNeighbors.php +++ b/src/laravel/HasNeighbors.php @@ -19,6 +19,9 @@ public function scopeNearestNeighbors(Builder $query, string $column, mixed $val case Distance::Cosine: $op = '<=>'; break; + case Distance::L1: + $op = '<+>'; + break; default: throw new \InvalidArgumentException("Invalid distance"); } diff --git a/tests/LaravelTest.php b/tests/LaravelTest.php index b692142..fb88655 100644 --- a/tests/LaravelTest.php +++ b/tests/LaravelTest.php @@ -97,6 +97,15 @@ public function testInstance() $this->assertEqualsWithDelta([1, sqrt(3)], $neighbors->pluck('neighbor_distance')->toArray(), 0.00001); } + public function testInstanceL1() + { + $this->createItems(); + $item = Item::find(1); + $neighbors = $item->nearestNeighbors('embedding', Distance::L1)->take(5)->get(); + $this->assertEquals([3, 2], $neighbors->pluck('id')->toArray()); + $this->assertEqualsWithDelta([1, 3], $neighbors->pluck('neighbor_distance')->toArray(), 0.00001); + } + public function testMissingAttribute() { $this->expectException(MissingAttributeException::class);