-
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.
this will add some tagging to the document process not as a function …
…though
- Loading branch information
Showing
9 changed files
with
341 additions
and
103 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,35 @@ | ||
<?php | ||
|
||
namespace App\Events; | ||
|
||
use App\Models\Document; | ||
use Illuminate\Broadcasting\InteractsWithSockets; | ||
use Illuminate\Broadcasting\PrivateChannel; | ||
use Illuminate\Foundation\Events\Dispatchable; | ||
use Illuminate\Queue\SerializesModels; | ||
|
||
class DocumentParsedEvent | ||
{ | ||
use Dispatchable, InteractsWithSockets, SerializesModels; | ||
|
||
/** | ||
* Create a new event instance. | ||
*/ | ||
public function __construct( | ||
public Document $document) | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Get the channels the eve nt should broadcast on. | ||
* | ||
* @return array<int, \Illuminate\Broadcasting\Channel> | ||
*/ | ||
public function broadcastOn(): array | ||
{ | ||
return [ | ||
new PrivateChannel('channel-name'), | ||
]; | ||
} | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class TagResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return parent::toArray($request); | ||
} | ||
} |
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,92 @@ | ||
<?php | ||
|
||
namespace App\Listeners; | ||
|
||
use App\Events\DocumentParsedEvent; | ||
use Illuminate\Contracts\Queue\ShouldQueue; | ||
use Illuminate\Support\Collection; | ||
use Illuminate\Support\Facades\Log; | ||
use LlmLaraHub\LlmDriver\LlmDriverFacade; | ||
use LlmLaraHub\LlmDriver\Responses\CompletionResponse; | ||
|
||
class TagDocumentListener implements ShouldQueue | ||
{ | ||
protected Collection $tags; | ||
|
||
/** | ||
* Create the event listener. | ||
*/ | ||
public function __construct() | ||
{ | ||
// | ||
} | ||
|
||
/** | ||
* Handle the event. | ||
*/ | ||
public function handle(DocumentParsedEvent $event): void | ||
{ | ||
Log::info('[LaraChain] Tagging document'); | ||
|
||
$document = $event->document; | ||
$summary = $document->summary; | ||
$prompt = <<<EOT | ||
This is the summary of the document, Can you make some tags I can use. | ||
Please return them as a string of text with each tag separated by a comma for example: | ||
Tag 1, Tag Two Test, Tag Three Test | ||
And nothing else. Here is the summary: | ||
### START SUMMARY | ||
{$summary} | ||
### END SUMMARY | ||
EOT; | ||
/** @var CompletionResponse $response */ | ||
$response = LlmDriverFacade::driver($document->getDriver()) | ||
->completion( | ||
prompt: $prompt | ||
); | ||
|
||
$this->tags = collect(explode(',', $response->content)); | ||
|
||
Log::info('[LaraChain] Tags Found: '.$response->content); | ||
|
||
$this->tags->map(function ($tag) use ($document) { | ||
$document->addTag($tag); | ||
}); | ||
|
||
foreach ($document->document_chunks as $chunk) { | ||
$tagsFlat = $this->tags->implode(','); | ||
$summary = $chunk->summary; | ||
$prompt = <<<EOT | ||
This is one chunk or page number {$chunk->sort_order} in the document , Can you make some tags I can use. | ||
Please return them as a string of text with each tag separated by a comma for example: | ||
Tag 1, Tag Two Test, Tag Three Test | ||
And nothing else. Here is the summary: | ||
### START SUMMARY | ||
{$summary} | ||
### END SUMMARY | ||
### AND HERE ARE EXISTING TAGS | ||
{$tagsFlat} | ||
### END EXISTING TAGS | ||
EOT; | ||
|
||
/** @var CompletionResponse $response */ | ||
$response = LlmDriverFacade::driver($document->getDriver()) | ||
->completion( | ||
prompt: $prompt | ||
); | ||
|
||
$tagsChild = explode(',', $response->content); | ||
|
||
foreach ($tagsChild as $tag) { | ||
$chunk->addTag($tag); | ||
$this->tags->push($tag); | ||
} | ||
} | ||
|
||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<template> | ||
<div class="flex flex-wrap justify-center items-center mx-auto gap-2 text-gray-500"> | ||
<div v-for="tag in document.tags" :key="tag.id"> | ||
<span class="badge badge-outline text-xs">{{ tag.name }}</span> | ||
</div> | ||
</div> | ||
</template> | ||
|
||
<script setup> | ||
const props = defineProps({ | ||
document: Object | ||
}); | ||
</script> |
Oops, something went wrong.