-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
223 additions
and
86 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
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,22 @@ | ||
<?php | ||
|
||
namespace LlmLaraHub\LlmDriver\Helpers; | ||
|
||
use App\Models\Message; | ||
use Illuminate\Database\Eloquent\Collection; | ||
|
||
trait CreateReferencesTrait | ||
{ | ||
protected function saveDocumentReference( | ||
Message $model, | ||
Collection $documentChunks | ||
): void { | ||
//add each one to a batch job or do the work here. | ||
foreach ($documentChunks as $documentChunk) { | ||
$model->message_document_references()->create([ | ||
'document_chunk_id' => $documentChunk->id, | ||
'distance' => $documentChunk->distance, | ||
]); | ||
} | ||
} | ||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace LlmLaraHub\LlmDriver\Helpers; | ||
|
||
use App\Models\DocumentChunk; | ||
use Illuminate\Database\Eloquent\Collection; | ||
use Pgvector\Laravel\Vector; | ||
|
||
trait DistanceQueryTrait | ||
{ | ||
/** | ||
* @TODO | ||
* Track the document page for referehce | ||
* | ||
* @see https://github.com/orgs/LlmLaraHub/projects/1?pane=issue&itemId=60394288 | ||
*/ | ||
protected function distance( | ||
string $embeddingSize, | ||
int $collectionId, | ||
Vector $embedding | ||
): Collection { | ||
|
||
$documentChunkResults = DocumentChunk::query() | ||
->join('documents', 'documents.id', '=', 'document_chunks.document_id') | ||
->selectRaw( | ||
"document_chunks.{$embeddingSize} <-> ? as distance, document_chunks.content as content, document_chunks.{$embeddingSize} as embedding, document_chunks.id as id, document_chunks.summary as summary, document_chunks.document_id as document_id", | ||
[$embedding] | ||
) | ||
/** @phpstan-ignore-next-line */ | ||
->where('documents.collection_id', $collectionId) | ||
->limit(10) | ||
->orderByRaw('distance') | ||
->get(); | ||
|
||
return $documentChunkResults; | ||
} | ||
} |
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,26 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class MessageDocumentReferenceResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'id' => $this->id, | ||
'document_name' => $this->document_chunk->document->file_path, | ||
'page' => $this->document_chunk->sort_order, | ||
'distance' => round($this->distance, 2), | ||
'summary' => str($this->document_chunk->summary)->markdown(), | ||
'taggings' => TagResource::collection($this->document_chunk->tags), | ||
]; | ||
} | ||
} |
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.