-
Notifications
You must be signed in to change notification settings - Fork 11.8k
Vector things #58337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+282
−3
Merged
Vector things #58337
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
9fd7cb5
initial work on vector distances
taylorotwell 6fdd12b
formatting
taylorotwell 91d5318
add where clause helpers
taylorotwell 99c3860
where similar helper
taylorotwell b99cf4c
adjust similarity
taylorotwell 3cbb61e
add vector index methods
taylorotwell 44037de
support fluent vector indexes
taylorotwell b75ba12
reorder methods
taylorotwell 6ff1c02
add tests
taylorotwell 85797f0
Apply fixes from StyleCI
StyleCIBot 4244a98
move method
taylorotwell 40eff06
formatting
taylorotwell 2045626
ensure postgres
taylorotwell dacf55a
Apply fixes from StyleCI
StyleCIBot 7f99b9d
rename method
taylorotwell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| use Illuminate\Database\ConnectionInterface; | ||
| use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | ||
| use Illuminate\Database\Eloquent\Relations\Relation; | ||
| use Illuminate\Database\PostgresConnection; | ||
| use Illuminate\Database\Query\Grammars\Grammar; | ||
| use Illuminate\Database\Query\Processors\Processor; | ||
| use Illuminate\Pagination\Paginator; | ||
|
|
@@ -458,6 +459,39 @@ public function addSelect($column) | |
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Add a vector-similarity selection to the query. | ||
| * | ||
| * @param \Illuminate\Contracts\Database\Query\Expression|string $column | ||
| * @param \Illuminate\Support\Collection<int, float>|\Illuminate\Contracts\Support\Arrayable|array<int, float>|string $vector | ||
| * @param string|null $as | ||
| * @return $this | ||
| */ | ||
| public function selectVectorDistance($column, $vector, $as = null) | ||
| { | ||
| $this->ensureConnectionSupportsVectors(); | ||
|
|
||
| if (is_string($vector)) { | ||
| Str::of($vector)->toEmbeddings(); | ||
| } | ||
|
|
||
| $this->addBinding( | ||
| json_encode( | ||
| $vector instanceof Arrayable | ||
| ? $vector->toArray() | ||
| : $vector, | ||
| flags: JSON_THROW_ON_ERROR | ||
| ), | ||
| 'select', | ||
| ); | ||
|
|
||
| $as = $this->getGrammar()->wrap($as ?? $column.'_distance'); | ||
|
|
||
| return $this->addSelect( | ||
| new Expression("({$this->getGrammar()->wrap($column)} <=> ?) as {$as}") | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Force the query to only return distinct results. | ||
| * | ||
|
|
@@ -1098,6 +1132,75 @@ public function orWhereColumn($first, $operator = null, $second = null) | |
| return $this->whereColumn($first, $operator, $second, 'or'); | ||
| } | ||
|
|
||
| /** | ||
| * Add a vector similarity clause to the query, filtering by minimum similarity and ordering by similarity. | ||
| * | ||
| * @param \Illuminate\Contracts\Database\Query\Expression|string $column | ||
| * @param \Illuminate\Support\Collection<int, float>|\Illuminate\Contracts\Support\Arrayable|array<int, float>|string $vector | ||
| * @param float $minSimilarity A value between 0.0 and 1.0, where 1.0 is identical. | ||
| * @param bool $order | ||
| * @return $this | ||
| */ | ||
| public function whereVectorSimilarTo($column, $vector, $minSimilarity = 0.6, $order = true) | ||
| { | ||
| if (is_string($vector)) { | ||
| $vector = Str::of($vector)->toEmbeddings(); | ||
| } | ||
|
|
||
| $this->whereVectorDistanceLessThan($column, $vector, 1 - $minSimilarity); | ||
|
|
||
| if ($order) { | ||
| $this->orderByVectorDistance($column, $vector); | ||
| } | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Add a vector distance "where" clause to the query. | ||
| * | ||
| * @param \Illuminate\Contracts\Database\Query\Expression|string $column | ||
| * @param \Illuminate\Support\Collection<int, float>|\Illuminate\Contracts\Support\Arrayable|array<int, float>|string $vector | ||
| * @param float $maxDistance | ||
| * @param string $boolean | ||
| * @return $this | ||
| */ | ||
| public function whereVectorDistanceLessThan($column, $vector, $maxDistance, $boolean = 'and') | ||
| { | ||
| $this->ensureConnectionSupportsVectors(); | ||
|
|
||
| if (is_string($vector)) { | ||
| Str::of($vector)->toEmbeddings(); | ||
| } | ||
|
|
||
| return $this->whereRaw( | ||
| "({$this->getGrammar()->wrap($column)} <=> ?) <= ?", | ||
taylorotwell marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| [ | ||
| json_encode( | ||
| $vector instanceof Arrayable | ||
| ? $vector->toArray() | ||
| : $vector, | ||
| flags: JSON_THROW_ON_ERROR | ||
| ), | ||
| $maxDistance, | ||
| ], | ||
| $boolean | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Add a vector distance "or where" clause to the query. | ||
| * | ||
| * @param \Illuminate\Contracts\Database\Query\Expression|string $column | ||
| * @param \Illuminate\Support\Collection<int, float>|\Illuminate\Contracts\Support\Arrayable|array<int, float>|string $vector | ||
| * @param float $maxDistance | ||
| * @return $this | ||
| */ | ||
| public function orWhereVectorDistanceLessThan($column, $vector, $maxDistance) | ||
| { | ||
| return $this->whereVectorDistanceLessThan($column, $vector, $maxDistance, 'or'); | ||
| } | ||
|
|
||
| /** | ||
| * Add a raw "where" clause to the query. | ||
| * | ||
|
|
@@ -2781,6 +2884,39 @@ public function oldest($column = 'created_at') | |
| return $this->orderBy($column, 'asc'); | ||
| } | ||
|
|
||
| /** | ||
| * Add a vector-distance "order by" clause to the query. | ||
| * | ||
| * @param \Illuminate\Contracts\Database\Query\Expression|string $column | ||
| * @param \Illuminate\Support\Collection<int, float>|\Illuminate\Contracts\Support\Arrayable|array<int, float> $vector | ||
| * @return $this | ||
| */ | ||
| public function orderByVectorDistance($column, $vector) | ||
| { | ||
| $this->ensureConnectionSupportsVectors(); | ||
|
|
||
| if (is_string($vector)) { | ||
| Str::of($vector)->toEmbeddings(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did you forget to reassign the variable? |
||
| } | ||
|
|
||
| $this->addBinding( | ||
| json_encode( | ||
| $vector instanceof Arrayable | ||
| ? $vector->toArray() | ||
| : $vector, | ||
| flags: JSON_THROW_ON_ERROR | ||
| ), | ||
| $this->unions ? 'unionOrder' : 'order' | ||
| ); | ||
|
|
||
| $this->{$this->unions ? 'unionOrders' : 'orders'}[] = [ | ||
| 'column' => new Expression("({$this->getGrammar()->wrap($column)} <=> ?)"), | ||
| 'direction' => 'asc', | ||
| ]; | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Put the query's results in random order. | ||
| * | ||
|
|
@@ -4403,6 +4539,18 @@ public function getConnection() | |
| return $this->connection; | ||
| } | ||
|
|
||
| /** | ||
| * Ensure the database connection supports vector queries. | ||
| * | ||
| * @return void | ||
| */ | ||
| protected function ensureConnectionSupportsVectors() | ||
| { | ||
| if (! $this->connection instanceof PostgresConnection) { | ||
| throw new RuntimeException('Vector distance queries are only supported by Postgres.'); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get the database query processor instance. | ||
| * | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.