Skip to content

Commit

Permalink
Added L1 distance
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed May 1, 2024
1 parent 07cfef0 commit 95cc23a
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions src/laravel/Distance.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ enum Distance
case L2;
case InnerProduct;
case Cosine;
case L1;
}
3 changes: 3 additions & 0 deletions src/laravel/HasNeighbors.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand Down
9 changes: 9 additions & 0 deletions tests/LaravelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 95cc23a

Please sign in to comment.