From 2d373461418ac805052fe3f960f9fabf3b2a04bb Mon Sep 17 00:00:00 2001 From: Alfred Nutile Date: Mon, 25 Mar 2024 21:29:19 -0400 Subject: [PATCH] mock driver working with summary and vector --- .../Documents/Transformers/PdfTransformer.php | 23 +++++++++---------- app/Jobs/ProcessFileJob.php | 2 +- app/Jobs/SummarizeDataJob.php | 3 ++- app/Jobs/VectorlizeDataJob.php | 6 ++--- app/LlmDriver/BaseClient.php | 17 +++++++------- app/LlmDriver/LlmDriverClient.php | 12 ++++------ app/LlmDriver/LlmDriverFacade.php | 8 +++---- app/LlmDriver/MockClient.php | 23 ++++++++----------- app/Models/Document.php | 1 - app/Providers/AppServiceProvider.php | 5 +--- config/llmdriver.php | 23 +++++++++---------- tests/Feature/Jobs/VectorlizeDataJobTest.php | 6 ++--- tests/Feature/LlmDriverFacadeTest.php | 4 +--- tests/Feature/MockClientTest.php | 9 +++----- tests/Feature/PdfTransformerTest.php | 2 +- tests/Feature/SummarizeDataJobTest.php | 12 ++++------ 16 files changed, 66 insertions(+), 90 deletions(-) diff --git a/app/Domains/Documents/Transformers/PdfTransformer.php b/app/Domains/Documents/Transformers/PdfTransformer.php index 9975fd65..69ba7731 100644 --- a/app/Domains/Documents/Transformers/PdfTransformer.php +++ b/app/Domains/Documents/Transformers/PdfTransformer.php @@ -6,7 +6,6 @@ use App\Events\CollectionStatusEvent; use App\Jobs\SummarizeDataJob; use App\Jobs\VectorlizeDataJob; -use App\Models\Collection; use App\Models\Document; use App\Models\DocumentChunk; use Illuminate\Bus\Batch; @@ -42,25 +41,25 @@ public function handle(Document $document): Document ] ); /** - * Soon taggings + * Soon taggings * And Summary */ $chunks[] = [ new VectorlizeDataJob($DocumentChunk), - new SummarizeDataJob($DocumentChunk) + new SummarizeDataJob($DocumentChunk), ]; } $batch = Bus::batch($chunks) - ->name("Chunking Document - {$this->document->id}") - ->finally(function (Batch $batch) use ($document) { - CollectionStatusEvent::dispatch( - $document->collection, - CollectionStatusEnum::PROCESSED - ); - }) - ->allowFailures() - ->dispatch(); + ->name("Chunking Document - {$this->document->id}") + ->finally(function (Batch $batch) use ($document) { + CollectionStatusEvent::dispatch( + $document->collection, + CollectionStatusEnum::PROCESSED + ); + }) + ->allowFailures() + ->dispatch(); return $this->document; } diff --git a/app/Jobs/ProcessFileJob.php b/app/Jobs/ProcessFileJob.php index 63eb708c..01ddcda1 100644 --- a/app/Jobs/ProcessFileJob.php +++ b/app/Jobs/ProcessFileJob.php @@ -48,7 +48,7 @@ public function handle(): void //new TagDataJob($this->document), //then mark it all as done and notify the ui ]) - ->name('Process PDF Document - ' . $document->id) + ->name('Process PDF Document - '.$document->id) ->finally(function (Batch $batch) use ($document) { /** * @TODO diff --git a/app/Jobs/SummarizeDataJob.php b/app/Jobs/SummarizeDataJob.php index c4301741..35523df0 100644 --- a/app/Jobs/SummarizeDataJob.php +++ b/app/Jobs/SummarizeDataJob.php @@ -4,9 +4,9 @@ use App\Domains\Documents\StatusEnum; use App\LlmDriver\LlmDriverFacade; +use App\LlmDriver\Responses\CompletionResponse; use App\Models\DocumentChunk; use Illuminate\Bus\Batchable; -use App\LlmDriver\Responses\CompletionResponse; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; @@ -35,6 +35,7 @@ public function handle(): void $this->documentChunk->update([ 'status_summary' => StatusEnum::Cancelled, ]); + return; } $content = $this->documentChunk->content; diff --git a/app/Jobs/VectorlizeDataJob.php b/app/Jobs/VectorlizeDataJob.php index fde29168..1399866a 100644 --- a/app/Jobs/VectorlizeDataJob.php +++ b/app/Jobs/VectorlizeDataJob.php @@ -3,16 +3,15 @@ namespace App\Jobs; use App\Domains\Documents\StatusEnum; -use App\LlmDriver\LlmDriverClient; use App\LlmDriver\LlmDriverFacade; +use App\LlmDriver\Responses\EmbeddingsResponseDto; use App\Models\DocumentChunk; +use Illuminate\Bus\Batchable; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; -use App\LlmDriver\Responses\EmbeddingsResponseDto; -use Illuminate\Bus\Batchable; class VectorlizeDataJob implements ShouldQueue { @@ -37,6 +36,7 @@ public function handle(): void $this->documentChunk->update([ 'status_embeddings' => StatusEnum::Cancelled, ]); + return; } diff --git a/app/LlmDriver/BaseClient.php b/app/LlmDriver/BaseClient.php index fa814f40..525aa2f3 100644 --- a/app/LlmDriver/BaseClient.php +++ b/app/LlmDriver/BaseClient.php @@ -1,17 +1,17 @@ -$driver(); } @@ -30,5 +29,4 @@ public function mock(): BaseClient { return new MockClient(); } - -} \ No newline at end of file +} diff --git a/app/LlmDriver/LlmDriverFacade.php b/app/LlmDriver/LlmDriverFacade.php index 8dd15a23..60965b68 100644 --- a/app/LlmDriver/LlmDriverFacade.php +++ b/app/LlmDriver/LlmDriverFacade.php @@ -2,17 +2,15 @@ namespace App\LlmDriver; -use \Illuminate\Support\Facades\Facade; +use Illuminate\Support\Facades\Facade; class LlmDriverFacade extends Facade { /** * Get the registered name of the component. - * - * @return string */ protected static function getFacadeAccessor(): string { - return "llm_driver"; + return 'llm_driver'; } -} \ No newline at end of file +} diff --git a/app/LlmDriver/MockClient.php b/app/LlmDriver/MockClient.php index 154170c4..b54b2c4d 100644 --- a/app/LlmDriver/MockClient.php +++ b/app/LlmDriver/MockClient.php @@ -1,25 +1,20 @@ -isAdmin(); }); - - $this->app->bind('llm_driver', function() { + $this->app->bind('llm_driver', function () { return LlmDriverClient::make(); }); - - } } diff --git a/config/llmdriver.php b/config/llmdriver.php index 9e90bebc..a6a71671 100644 --- a/config/llmdriver.php +++ b/config/llmdriver.php @@ -1,22 +1,21 @@ - env("LLM_DRIVER", "mock"), + 'driver' => env('LLM_DRIVER', 'mock'), - "drivers" => - [ - "mock" => [ + 'drivers' => [ + 'mock' => [ ], - "openai" => [ - "api_key" => env("OPENAI_API_KEY"), - "api_url" => env("OPENAI_API_URL", "https://api.openai.com/v1/engines/davinci-codex/completions") + 'openai' => [ + 'api_key' => env('OPENAI_API_KEY'), + 'api_url' => env('OPENAI_API_URL', 'https://api.openai.com/v1/engines/davinci-codex/completions'), ], - "azure" => [ + 'azure' => [ ], - "ollama" => [ + 'ollama' => [ ], - ] -]; \ No newline at end of file + ], +]; diff --git a/tests/Feature/Jobs/VectorlizeDataJobTest.php b/tests/Feature/Jobs/VectorlizeDataJobTest.php index c17e504b..b1472c71 100644 --- a/tests/Feature/Jobs/VectorlizeDataJobTest.php +++ b/tests/Feature/Jobs/VectorlizeDataJobTest.php @@ -5,8 +5,6 @@ use App\Jobs\VectorlizeDataJob; use App\LlmDriver\LlmDriverFacade; use App\Models\DocumentChunk; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class VectorlizeDataJobTest extends TestCase @@ -17,7 +15,7 @@ class VectorlizeDataJobTest extends TestCase public function test_gets_data(): void { $embedding = get_fixture('embedding_response.json'); - + $dto = new \App\LlmDriver\Responses\EmbeddingsResponseDto( data_get($embedding, 'data.0.embedding'), 1000 @@ -28,7 +26,7 @@ public function test_gets_data(): void ->andReturn($dto); $documentChunk = DocumentChunk::factory()->create([ - 'embedding' => null + 'embedding' => null, ]); $job = new VectorlizeDataJob($documentChunk); diff --git a/tests/Feature/LlmDriverFacadeTest.php b/tests/Feature/LlmDriverFacadeTest.php index 2d2eda67..37ea07be 100644 --- a/tests/Feature/LlmDriverFacadeTest.php +++ b/tests/Feature/LlmDriverFacadeTest.php @@ -4,8 +4,6 @@ use App\LlmDriver\LlmDriverFacade; use App\LlmDriver\Responses\EmbeddingsResponseDto; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; use Tests\TestCase; class LlmDriverFacadeTest extends TestCase @@ -15,7 +13,7 @@ class LlmDriverFacadeTest extends TestCase */ public function test_facade(): void { - $results = LlmDriverFacade::embedData("test"); + $results = LlmDriverFacade::embedData('test'); $this->assertInstanceOf( EmbeddingsResponseDto::class, diff --git a/tests/Feature/MockClientTest.php b/tests/Feature/MockClientTest.php index 3106b578..6d22cece 100644 --- a/tests/Feature/MockClientTest.php +++ b/tests/Feature/MockClientTest.php @@ -2,12 +2,9 @@ namespace Tests\Feature; -use App\LlmDriver\Responses\EmbeddingsResponseDto; use App\LlmDriver\MockClient; use App\LlmDriver\Responses\CompletionResponse; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; -use OpenAI\Resources\Embeddings; +use App\LlmDriver\Responses\EmbeddingsResponseDto; use Tests\TestCase; class MockClientTest extends TestCase @@ -23,7 +20,7 @@ public function test_embeddings(): void $results = $client->embedData('test'); $this->assertInstanceOf(EmbeddingsResponseDto::class, $results); - + } public function test_completion(): void @@ -34,6 +31,6 @@ public function test_completion(): void $results = $client->completion('test'); $this->assertInstanceOf(CompletionResponse::class, $results); - + } } diff --git a/tests/Feature/PdfTransformerTest.php b/tests/Feature/PdfTransformerTest.php index 3ba6c5dd..5dfc5d15 100644 --- a/tests/Feature/PdfTransformerTest.php +++ b/tests/Feature/PdfTransformerTest.php @@ -29,7 +29,7 @@ public function test_gets_data_from_pdf() $transformer = new PdfTransformer(); $transformer->handle($this->document); $this->assertDatabaseCount('document_chunks', 10); - + Bus::assertBatchCount(1); } diff --git a/tests/Feature/SummarizeDataJobTest.php b/tests/Feature/SummarizeDataJobTest.php index 14f1161e..69176a1d 100644 --- a/tests/Feature/SummarizeDataJobTest.php +++ b/tests/Feature/SummarizeDataJobTest.php @@ -3,11 +3,9 @@ namespace Tests\Feature; use App\Jobs\SummarizeDataJob; -use Illuminate\Foundation\Testing\RefreshDatabase; -use Illuminate\Foundation\Testing\WithFaker; -use Tests\TestCase; use App\LlmDriver\LlmDriverFacade; use App\Models\DocumentChunk; +use Tests\TestCase; class SummarizeDataJobTest extends TestCase { @@ -16,8 +14,8 @@ class SummarizeDataJobTest extends TestCase */ public function test_gets_data(): void { - - $data = "Foo bar"; + + $data = 'Foo bar'; $dto = new \App\LlmDriver\Responses\CompletionResponse($data); LlmDriverFacade::shouldReceive('completion') @@ -25,12 +23,12 @@ public function test_gets_data(): void ->andReturn($dto); $documentChunk = DocumentChunk::factory()->create([ - 'summary' => null + 'summary' => null, ]); $job = new SummarizeDataJob($documentChunk); $job->handle(); - $this->assertEquals("Foo bar", $documentChunk->refresh()->summary); + $this->assertEquals('Foo bar', $documentChunk->refresh()->summary); } }