Skip to content

Commit

Permalink
Trying to simplify the query
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed May 11, 2024
1 parent adc763f commit 4651e0e
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 39 deletions.
37 changes: 4 additions & 33 deletions Modules/LlmDriver/app/DistanceQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,15 @@ public function distance(
$documentIds = Document::query()
->select('id')
->where('documents.collection_id', $collectionId)
->orderBy('id')
->pluck('id');

$commonQuery = DocumentChunk::query()
->orderBy('sort_order')
->orderBy('section_number')
->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();

Expand All @@ -79,10 +52,8 @@ public function distance(
]);

$results = collect($neighborsCosine)
->merge($neighborsInnerProduct)
->merge($documentChunkResults)
->unique('id')
->take(10);
->take(5);

$siblingsIncluded = collect();

Expand Down
6 changes: 2 additions & 4 deletions resources/js/Pages/Chat/Components/ReferenceTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
<th></th>
<th>Document Name</th>
<th>Page</th>
<th class="text-center">Section <br>Record ID</th>
<th>Distance</th>
<th class="text-center">Section/<br>Record ID</th>
<th>Summary</th>
</tr>
</thead>
Expand All @@ -23,7 +22,6 @@
</td>
<td>{{ reference.page }}</td>
<td>{{ reference.section_number }}/{{ reference.document_chunk_id }}</td>
<td>{{ reference.distance }}</td>
<td>
<span v-html="reference.summary"></span>
</td>
Expand All @@ -35,4 +33,4 @@
const props = defineProps({
message: Object
})
</script>
</script>
1 change: 1 addition & 0 deletions tests/Feature/GenericSiteSpiderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class GenericSiteSpiderTest extends TestCase
*/
public function test_spider(): void
{
$this->markTestSkipped('@NOTE not sure I will keep this');
$urls = [
'https://alfrednutile.info/ssh-config',
'https://alnutile.medium.com/multiple-openai-functions-php-laravel-466cb72eefb8',
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/WebSearchSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function test_searches(): void

LlmDriverFacade::shouldReceive('driver->completion')
->once()->andReturn(CompletionResponse::from([
'content' => 'updated query',
]));
'content' => 'updated query',
]));

LlmDriverFacade::shouldReceive('driver->onQueue')
->once()->andReturn('ollama');
Expand Down

0 comments on commit 4651e0e

Please sign in to comment.