Skip to content

Commit

Permalink
this will add some tagging to the document process not as a function …
Browse files Browse the repository at this point in the history
…though
  • Loading branch information
alnutile committed Apr 20, 2024
1 parent 442b12e commit 07ce85a
Show file tree
Hide file tree
Showing 9 changed files with 341 additions and 103 deletions.
35 changes: 35 additions & 0 deletions app/Events/DocumentParsedEvent.php
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'),
];
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/DocumentResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ public function toArray(Request $request): array
'type' => str($this->type->name)->title()->toString(),
'status' => str($this->status->name)->headline()->toString(),
'document_chunks_count' => $this->document_chunks()->count(),
'tags' => TagResource::collection($this->tags),
];
}
}
19 changes: 19 additions & 0 deletions app/Http/Resources/TagResource.php
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);
}
}
10 changes: 8 additions & 2 deletions app/Jobs/ProcessFileJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Jobs;

use App\Domains\Documents\TypesEnum;
use App\Events\DocumentParsedEvent;
use App\Models\Document;
use Illuminate\Bus\Batch;
use Illuminate\Bus\Queueable;
Expand Down Expand Up @@ -37,7 +38,11 @@ public function handle(): void
if ($document->type === TypesEnum::Pptx) {
if (Feature::active('process-ppxt')) {
/**
* Seems to be some work some do not
* @NOTE
* Seems to work with my example
* power point
* But the ones I needed it to work on broke
* so will tray again soon.
*/
$batch = Bus::batch([
new ParsePowerPointJob($this->document),
Expand Down Expand Up @@ -74,7 +79,8 @@ public function handle(): void
new ParsePdfFileJob($this->document),
])
->name('Process PDF Document - '.$document->id)
->finally(function (Batch $batch) {
->finally(function (Batch $batch) use ($document) {
DocumentParsedEvent::dispatch($document);
})
->allowFailures()
->onQueue(LlmDriverFacade::driver($document->getDriver())->onQueue())
Expand Down
92 changes: 92 additions & 0 deletions app/Listeners/TagDocumentListener.php
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);
}
}

}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions resources/js/Components/Tags.vue
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>
Loading

0 comments on commit 07ce85a

Please sign in to comment.