Skip to content

Commit

Permalink
mock driver working with summary and vector
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Mar 26, 2024
1 parent c0a0cad commit 2d37346
Show file tree
Hide file tree
Showing 16 changed files with 66 additions and 90 deletions.
23 changes: 11 additions & 12 deletions app/Domains/Documents/Transformers/PdfTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Jobs/ProcessFileJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/Jobs/SummarizeDataJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -35,6 +35,7 @@ public function handle(): void
$this->documentChunk->update([
'status_summary' => StatusEnum::Cancelled,
]);

return;
}
$content = $this->documentChunk->content;
Expand Down
6 changes: 3 additions & 3 deletions app/Jobs/VectorlizeDataJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -37,6 +36,7 @@ public function handle(): void
$this->documentChunk->update([
'status_embeddings' => StatusEnum::Cancelled,
]);

return;
}

Expand Down
17 changes: 8 additions & 9 deletions app/LlmDriver/BaseClient.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
<?php
<?php

namespace App\LlmDriver;

use App\LlmDriver\Responses\EmbeddingsResponseDto;
use Illuminate\Support\Facades\Log;
use OpenAI\Resources\Embeddings;

abstract class BaseClient {

public function embedData(string $data) : EmbeddingsResponseDto {
abstract class BaseClient
{
public function embedData(string $data): EmbeddingsResponseDto
{

Log::info('LlmDriver::MockClient::embedData');

Log::info("LlmDriver::MockClient::embedData");

$data = get_fixture('embedding_response.json');

return new EmbeddingsResponseDto(
data_get($data, 'data.0.embedding'),
1000,
);
}

}
}
12 changes: 5 additions & 7 deletions app/LlmDriver/LlmDriverClient.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
<?php
<?php

namespace App\LlmDriver;

use Mockery\Mock;

class LlmDriverClient
class LlmDriverClient
{
protected $config = [];

Expand All @@ -19,16 +17,16 @@ public static function make(): BaseClient

$config = config("llmdriver.drivers.{$driver}");

if(!method_exists(static::class, $driver)) {
if (! method_exists(static::class, $driver)) {
throw new \Exception("Driver {$driver} not found");
}

/** @phpstan-ignore-next-line */
return (new static($config))->$driver();
}

public function mock(): BaseClient
{
return new MockClient();
}

}
}
8 changes: 3 additions & 5 deletions app/LlmDriver/LlmDriverFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
}
23 changes: 9 additions & 14 deletions app/LlmDriver/MockClient.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
<?php
<?php

namespace App\LlmDriver;

use App\LlmDriver\Responses\CompletionResponse;
use App\LlmDriver\Responses\EmbeddingsResponseDto;
use Illuminate\Support\Facades\Log;
use OpenAI\Resources\Embeddings;

class MockClient extends BaseClient {

public function completion(string $prompt) : CompletionResponse {
Log::info("LlmDriver::MockClient::completion");
$data = <<<EOD
class MockClient extends BaseClient
{
public function completion(string $prompt): CompletionResponse
{
Log::info('LlmDriver::MockClient::completion');

$data = <<<'EOD'
Voluptate irure cillum dolor anim officia reprehenderit dolor. Eiusmod veniam nostrud consectetur incididunt proident id. Anim adipisicing pariatur amet duis Lorem sunt veniam veniam est. Deserunt ea aliquip cillum pariatur consectetur. Dolor in reprehenderit adipisicing consectetur cupidatat ad cupidatat reprehenderit. Nostrud mollit voluptate aliqua anim pariatur excepteur eiusmod velit quis exercitation tempor quis excepteur.
EOD;

return new CompletionResponse($data);
}



}
}
1 change: 0 additions & 1 deletion app/Models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
* @property int $collection_id
* @property string|null $summary
* @property string|null $file_path
*/
class Document extends Model
{
Expand Down
5 changes: 1 addition & 4 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@ public function boot(): void
return $user->isAdmin();
});


$this->app->bind('llm_driver', function() {
$this->app->bind('llm_driver', function () {
return LlmDriverClient::make();
});



}
}
23 changes: 11 additions & 12 deletions config/llmdriver.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php
<?php

return [
'driver' => 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' => [

],
]
];
],
];
6 changes: 2 additions & 4 deletions tests/Feature/Jobs/VectorlizeDataJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -28,7 +26,7 @@ public function test_gets_data(): void
->andReturn($dto);

$documentChunk = DocumentChunk::factory()->create([
'embedding' => null
'embedding' => null,
]);

$job = new VectorlizeDataJob($documentChunk);
Expand Down
4 changes: 1 addition & 3 deletions tests/Feature/LlmDriverFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
9 changes: 3 additions & 6 deletions tests/Feature/MockClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,7 +20,7 @@ public function test_embeddings(): void
$results = $client->embedData('test');

$this->assertInstanceOf(EmbeddingsResponseDto::class, $results);

}

public function test_completion(): void
Expand All @@ -34,6 +31,6 @@ public function test_completion(): void
$results = $client->completion('test');

$this->assertInstanceOf(CompletionResponse::class, $results);

}
}
2 changes: 1 addition & 1 deletion tests/Feature/PdfTransformerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Expand Down
Loading

0 comments on commit 2d37346

Please sign in to comment.