Skip to content

Commit

Permalink
add to the Summarize Document job
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Apr 29, 2024
1 parent 756e56f commit 8e0631d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 6 deletions.
34 changes: 29 additions & 5 deletions app/Jobs/SummarizeDocumentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

namespace App\Jobs;

use App\Domains\Agents\VerifyPromptInputDto;
use App\Domains\Collections\CollectionStatusEnum;
use App\Domains\Documents\StatusEnum;
use App\Events\CollectionStatusEvent;
use App\Models\Document;
use Facades\App\Domains\Agents\VerifyResponseAgent;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -15,6 +17,7 @@
use LlmLaraHub\LlmDriver\Helpers\TrimText;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
use App\Domains\Agents\VerifyPromptOutputDto;

class SummarizeDocumentJob implements ShouldQueue
{
Expand All @@ -41,12 +44,13 @@ public function handle(): void
}

$content = implode(' ', $content);
$intro = "The following content is part of a larger document. I would like you to summarize it so
I can show a summary view of all the other pages and this ones related to the same document.
Just return the summary, 1-2 lines if possible and no extra surrounding text.
The content to summarize follows:";

$prompt = <<<EOD
The following content is part of a larger document. I would like you to summarize it so
I can show a summary view of all the other pages and this ones related to the same document.
Just return the summary, 1-2 lines if possible and no extra surrounding text.
The content to summarize follows:
$intro
{$content}
EOD;
Expand All @@ -56,11 +60,31 @@ public function handle(): void
$this->document->getDriver()
)->completion($prompt);


$verifyPrompt = <<<'PROMPT'
This the content from all the documents in this collection.
Then that was passed into the LLM to summarize the results.
PROMPT;

$dto = VerifyPromptInputDto::from(
[
'chattable' => $this->document->collection->getChat(),
'originalPrompt' => $intro,
'context' => $content,
'llmResponse' => $results->content,
'verifyPrompt' => $verifyPrompt,
]
);

/** @var VerifyPromptOutputDto $response */
$response = VerifyResponseAgent::verify($dto);

$this->document->update([
'summary' => $results->content,
'summary' => $response->response,
'status_summary' => StatusEnum::Complete,
]);


CollectionStatusEvent::dispatch(
$this->document->collection,
CollectionStatusEnum::PROCESSED
Expand Down
14 changes: 13 additions & 1 deletion tests/Feature/SummarizeDocumentJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Tests\Feature;

use App\Jobs\SummarizeDocumentJob;
use App\Models\Chat;
use App\Models\Collection;
use App\Models\Document;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use Tests\TestCase;
Expand All @@ -22,13 +24,23 @@ public function test_summary_job(): void
->once()
->andReturn($dto);

$collection = Collection::factory()->create();

$chat = Chat::factory()->create([
'chatable_type' => Collection::class,
'chatable_id' => $collection->id,
]);

$document = Document::factory()->create([
'summary' => null,
'collection_id' => $collection->id,
]);

$this->fakeVerify($chat);

$job = new SummarizeDocumentJob($document);
$job->handle();

$this->assertEquals('Foo bar', $document->refresh()->summary);
$this->assertEquals('verified yay!', $document->refresh()->summary);
}
}
17 changes: 17 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

namespace Tests;

use App\Domains\Agents\VerifyPromptOutputDto;
use App\Models\Team;
use App\Models\User;
use Facades\App\Domains\Agents\VerifyResponseAgent;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
use LlmLaraHub\LlmDriver\HasDrivers;

abstract class TestCase extends BaseTestCase
{
Expand All @@ -19,4 +22,18 @@ public function createUserWithCurrentTeam()

return $user->refresh();
}

public function fakeVerify(HasDrivers $model) {
VerifyResponseAgent::shouldReceive('verify')->once()->andReturn(
VerifyPromptOutputDto::from(
[
'chattable' => $model->getChat(),
'originalPrompt' => 'test',
'context' => 'test',
'llmResponse' => 'test',
'verifyPrompt' => 'This is a completion so the users prompt was past directly to the llm with all the context.',
'response' => 'verified yay!',
]
));
}
}

0 comments on commit 8e0631d

Please sign in to comment.