Skip to content

Commit

Permalink
this will add verification to the summarize function
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Apr 29, 2024
1 parent 54e086c commit 756e56f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 5 deletions.
37 changes: 35 additions & 2 deletions Modules/LlmDriver/app/Functions/SummarizeCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace LlmLaraHub\LlmDriver\Functions;

use App\Domains\Agents\VerifyPromptInputDto;
use App\Domains\Agents\VerifyPromptOutputDto;
use Facades\App\Domains\Agents\VerifyResponseAgent;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\HasDrivers;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
Expand Down Expand Up @@ -33,9 +36,19 @@ public function handle(
}
}

notify_ui($model->getChat(), 'Getting Summary');

$summary = $summary->implode('\n');

$prompt = 'Can you summarize all of this content for me from a collection of documents I uploaded what follows is the content: '.$summary;
$prompt = <<<PROMPT
Can you summarize all of this content for me from a collection of documents I uploaded what
follows is the content:
### START ALL SUMMARY DATA
$summary
### END ALL SUMMARY DATA
PROMPT;

$messagesArray = [];

Expand All @@ -46,8 +59,28 @@ public function handle(

$results = LlmDriverFacade::driver($model->getDriver())->chat($messagesArray);

notify_ui($model->getChat(), 'Summary complete going to do one verfication check on the summarhy');

$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' => $model->getChat(),
'originalPrompt' => 'Can you summarize this collection of data for me.',
'context' => $summary,
'llmResponse' => $results->content,
'verifyPrompt' => $verifyPrompt,
]
);

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

return FunctionResponse::from([
'content' => $results->content,
'content' => $response->response,
'requires_followup' => true,
]);
}
Expand Down
5 changes: 2 additions & 3 deletions app/Http/Controllers/ChatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Domains\Agents\VerifyPromptInputDto;
use App\Domains\Agents\VerifyPromptOutputDto;
use App\Domains\Messages\RoleEnum;
use App\Events\ChatUiUpdateEvent;
use App\Events\ChatUpdatedEvent;
use App\Http\Resources\ChatResource;
use App\Http\Resources\CollectionResource;
Expand Down Expand Up @@ -73,7 +72,7 @@ public function chat(Chat $chat)
Log::info('[LaraChain] Running Simple Completion');
$prompt = $validated['input'];

notify_ui($chat, "We are running a completion back shortly");
notify_ui($chat, 'We are running a completion back shortly');

$response = LlmDriverFacade::driver($chat->getDriver())->completion($prompt);
$response = $response->content;
Expand All @@ -88,7 +87,7 @@ public function chat(Chat $chat)
]
);

notify_ui($chat, "We are verifying the completion back shortly");
notify_ui($chat, 'We are verifying the completion back shortly');

/** @var VerifyPromptOutputDto $response */
$response = VerifyResponseAgent::verify($dto);
Expand Down
14 changes: 14 additions & 0 deletions tests/Feature/SummarizeCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Tests\Feature;

use App\Domains\Agents\VerifyPromptOutputDto;
use App\Models\Collection;
use App\Models\DocumentChunk;
use Facades\App\Domains\Agents\VerifyResponseAgent;
use LlmLaraHub\LlmDriver\Functions\ParametersDto;
use LlmLaraHub\LlmDriver\Functions\PropertyDto;
use LlmLaraHub\LlmDriver\Functions\SummarizeCollection;
Expand Down Expand Up @@ -54,6 +56,18 @@ public function test_gathers_all_content()
'chatable_id' => $collection->id,
]);

VerifyResponseAgent::shouldReceive('verify')->once()->andReturn(
VerifyPromptOutputDto::from(
[
'chattable' => $chat,
'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!',
]
));

$document = \App\Models\Document::factory()->create([
'collection_id' => $collection->id,
]);
Expand Down

0 comments on commit 756e56f

Please sign in to comment.