-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from LlmLaraHub/distance
Distance Query Updates
- Loading branch information
Showing
27 changed files
with
1,547 additions
and
52 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace LlmLaraHub\LlmDriver; | ||
|
||
use App\Models\Document; | ||
use App\Models\DocumentChunk; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Log; | ||
use Pgvector\Laravel\Distance; | ||
use Pgvector\Laravel\Vector; | ||
|
||
class DistanceQuery | ||
{ | ||
protected int $distanceThreshold = 0; | ||
|
||
/** | ||
* @TODO | ||
* Track the document page for referehce | ||
* | ||
* @see https://github.com/orgs/LlmLaraHub/projects/1?pane=issue&itemId=60394288 | ||
*/ | ||
public function distance( | ||
string $embeddingSize, | ||
int $collectionId, | ||
Vector $embedding | ||
): Collection { | ||
$documentIds = Document::query() | ||
->select('id') | ||
->where('documents.collection_id', $collectionId) | ||
->pluck('id'); | ||
|
||
$commonQuery = DocumentChunk::query() | ||
->whereIn('document_id', $documentIds); | ||
|
||
// Find nearest neighbors using L2 distance | ||
$documentChunkResults = $commonQuery | ||
->nearestNeighbors($embeddingSize, $embedding, Distance::L2) | ||
->take(5) | ||
->get(); | ||
|
||
// Get IDs of the nearest neighbors found 5 | ||
$nearestNeighborIds = $documentChunkResults->pluck('id')->toArray(); | ||
Log::info('[LaraChain] Nearest Neighbor IDs', [ | ||
'count' => count($nearestNeighborIds), | ||
'ids' => $nearestNeighborIds, | ||
]); | ||
// Find nearest neighbors using InnerProduct distance | ||
$neighborsInnerProduct = $commonQuery | ||
->whereNotIn('document_chunks.id', $nearestNeighborIds) | ||
->nearestNeighbors($embeddingSize, $embedding, Distance::InnerProduct) | ||
->get(); | ||
|
||
// Find nearest neighbors using Cosine distance found 0 | ||
$neighborsInnerProductIds = $neighborsInnerProduct->pluck('id')->toArray(); | ||
|
||
Log::info('[LaraChain] Nearest Neighbor Inner Product IDs', [ | ||
'count' => count($neighborsInnerProductIds), | ||
'ids' => $neighborsInnerProductIds, | ||
]); | ||
|
||
$neighborsCosine = $commonQuery | ||
->whereNotIn('id', $nearestNeighborIds) | ||
->when(! empty($neighborsInnerProductIds), function ($query) use ($neighborsInnerProductIds) { | ||
return $query->whereNotIn('id', $neighborsInnerProductIds); | ||
}) | ||
->nearestNeighbors($embeddingSize, $embedding, Distance::Cosine) | ||
->get(); | ||
|
||
Log::info('[LaraChain] Nearest Neighbor Cosine IDs', [ | ||
'count' => $neighborsCosine->count(), | ||
'ids' => $neighborsCosine->pluck('id')->toArray(), | ||
]); | ||
|
||
$results = collect($documentChunkResults)->merge($neighborsCosine)->unique('id')->take(5); | ||
|
||
return $results; | ||
} | ||
} |
This file contains 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 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 was deleted.
Oops, something went wrong.
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Models\Document; | ||
use App\Models\DocumentChunk; | ||
use Illuminate\Support\Facades\File; | ||
use LlmLaraHub\LlmDriver\DistanceQuery; | ||
use Pgvector\Laravel\Vector; | ||
use Tests\TestCase; | ||
|
||
class DistanceQueryTest extends TestCase | ||
{ | ||
public function test_results() | ||
{ | ||
$files = File::files(base_path('tests/fixtures/document_chunks')); | ||
$document = Document::factory()->create([ | ||
'id' => 31, | ||
]); | ||
|
||
foreach ($files as $file) { | ||
$data = json_decode(File::get($file), true); | ||
DocumentChunk::factory()->create($data); | ||
} | ||
|
||
$question = get_fixture('embedding_question_distance.json'); | ||
|
||
$vector = new Vector($question); | ||
|
||
$results = (new DistanceQuery())->distance( | ||
'embedding_1024', | ||
$document->collection_id, | ||
$vector); | ||
|
||
$this->assertCount(1, $results); | ||
|
||
} | ||
} |
This file contains 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 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 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 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 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 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 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
Oops, something went wrong.