Skip to content

Commit

Permalink
Minor touch up work to the timing in the ui
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jul 13, 2024
2 parents a974df6 + ca13c84 commit 170e72f
Show file tree
Hide file tree
Showing 25 changed files with 795 additions and 297 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci-cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,4 @@ jobs:
curl https://envoyer.io/deploy/${{ secrets.ENVOYER_FRESHJONES }}
curl https://envoyer.io/deploy/${{ secrets.ENVOYER_Q }}
curl https://envoyer.io/deploy/${{ secrets.ENVOYER_T }}
curl https://envoyer.io/deploy/${{ secrets.ENVOYER_FH }}
262 changes: 58 additions & 204 deletions Modules/LlmDriver/app/Functions/ReportingTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,21 @@

namespace LlmLaraHub\LlmDriver\Functions;

use App\Domains\Chat\MetaDataDto;
use App\Domains\Messages\RoleEnum;
use App\Domains\Prompts\FindSolutionsPrompt;
use App\Domains\Prompts\ReportBuildingFindRequirementsPrompt;
use App\Domains\Prompts\ReportingSummaryPrompt;
use App\Domains\Reporting\EntryTypeEnum;
use App\Domains\Reporting\ReportTypeEnum;
use App\Domains\Reporting\StatusEnum;
use App\Models\Document;
use App\Models\DocumentChunk;
use App\Models\Entry;
use App\Jobs\MakeReportSectionsJob;
use App\Jobs\ReportMakeEntriesJob;
use App\Models\Message;
use App\Models\Report;
use App\Models\Section;
use Illuminate\Bus\Batch;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use Laravel\Pennant\Feature;
use LlmLaraHub\LlmDriver\DistanceQuery\DistanceQueryFacade;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use LlmLaraHub\LlmDriver\Responses\CompletionResponse;
use LlmLaraHub\LlmDriver\Responses\EmbeddingsResponseDto;
use LlmLaraHub\LlmDriver\Responses\FunctionResponse;
use LlmLaraHub\LlmDriver\ToolsHelper;

Expand All @@ -39,6 +34,8 @@ class ReportingTool extends FunctionContract

protected array $promptHistory = [];

protected array $sectionJobs = [];

public function handle(
Message $message): FunctionResponse
{
Expand All @@ -65,48 +62,33 @@ public function handle(
'documents_count' => count($documents),
]);

foreach ($documents as $index => $document) {
try {

$groupedChunks = $document->document_chunks
->sortBy('sort_order')
->groupBy('sort_order');

$pagesGrouped = $groupedChunks->map(function ($chunks, $pageNumber) {
return [
"page_$pageNumber" => $chunks->toArray(),
];
})->collapse();

foreach (collect($pagesGrouped)
->chunk(3) as $pageIndex => $pagesChunk) {
$prompts = [];
foreach ($pagesChunk as $index => $page) {
$pageContent = collect($page)->pluck('content')->toArray();
$pageContent = implode("\n", $pageContent);
$prompt = ReportBuildingFindRequirementsPrompt::prompt(
$pageContent, $message->getContent(), $message->getChatable()->description
);
$prompts[] = $prompt;
}
$this->poolPrompt($prompts, $report, $document);
}

} catch (\Exception $e) {
Log::error('Error running Reporting Tool Checker', [
'error' => $e->getMessage(),
'index' => $index,
'line' => $e->getLine(),
]);
}
}
$this->buildUpSections($documents, $report, $message);

Bus::batch($this->sectionJobs)
->name(sprintf('Reporting Tool Sections Report id: %d Chat id %d', $report->id, $message->getChat()->id))
->allowFailures()
->finally(function (Batch $batch) use ($report) {
$report->update(['status_sections_generation' => StatusEnum::Complete]);
Bus::batch([
new ReportMakeEntriesJob($report),
])->name(sprintf('Reporting Entities Report Id %s', $report->id))
->allowFailures()
->finally(function (Batch $batch) use ($report) {
$report->update([
'status_entries_generation' => StatusEnum::Complete,
]);
})
->dispatch();

})
->dispatch();

notify_ui($message->getChat(), 'Building Summary');

$response = $this->summarizeReport($report);

$report->update([
'status_sections_generation' => StatusEnum::Complete,
'status_sections_generation' => StatusEnum::Running,
]);

$assistantMessage = $message->getChat()->addInput(
Expand All @@ -124,22 +106,9 @@ public function handle(
$report->message_id = $assistantMessage->id;
$report->save();

notify_ui_complete($message->getChat());

notify_ui($message->getChat(), 'Building Solutions list');
notify_ui_report($report, 'Building Solutions list');

$this->makeEntriesFromSections($report);

$report->update([
'status_entries_generation' => StatusEnum::Complete,
]);

notify_ui_complete($message->getChat());
notify_ui_report_complete($report);
//as a final output
//get deadlines
//get contacts
notify_ui_complete($report->getChat());

return FunctionResponse::from([
'content' => $response->content,
Expand All @@ -150,104 +119,44 @@ public function handle(
]);
}

protected function makeEntriesFromSections(Report $report): void
protected function buildUpSections(Collection $documents, Report $report, Message $message): void
{
$referenceCollection = $report->reference_collection;

if (! $referenceCollection) {
return;
}

foreach ($report->refresh()->sections->chunk(3) as $sectionChunk) {
$prompts = []; //reset every 3 sections
$sections = []; //reset every 3 sections

/** @var Section $section */
foreach ($sectionChunk as $section) {

try {
$input = $section->content;

/** @var EmbeddingsResponseDto $embedding */
$embedding = LlmDriverFacade::driver(
$report->getEmbeddingDriver()
)->embedData($input);

$embeddingSize = get_embedding_size($report->getEmbeddingDriver());

/** @phpstan-ignore-next-line */
$documentChunkResults = DistanceQueryFacade::cosineDistance(
$embeddingSize,
/** @phpstan-ignore-next-line */
$referenceCollection->id, //holy luck batman this is nice!
$embedding->embedding,
MetaDataDto::from([])//@NOTE could use this later if needed
);

$content = [];
/** @var DocumentChunk $result */
foreach ($documentChunkResults as $result) {
$contentString = remove_ascii($result->content);
$content[] = $contentString; //reduce_text_size seem to mess up Claude?
}
foreach ($documents as $index => $document) {
try {

$context = implode(' ', $content);

$prompt = FindSolutionsPrompt::prompt(
$section->content,
$context,
$report->getChatable()->description
);

$prompts[] = $prompt;
$sections[] = $section;
} catch (\Exception $e) {
Log::error('Error running Reporting Tool Checker for Section', [
'error' => $e->getMessage(),
'section' => $section->id,
'line' => $e->getLine(),
]);
}
}
$groupedChunks = $document->document_chunks
->sortBy('sort_order')
->groupBy('sort_order');

$results = LlmDriverFacade::driver($report->getDriver())
->completionPool($prompts);
$pagesGrouped = $groupedChunks->map(function ($chunks, $pageNumber) {
return [
"page_$pageNumber" => $chunks->toArray(),
];
})->collapse();

foreach ($results as $resultIndex => $result) {
$section = data_get($sections, $resultIndex, null);
foreach (collect($pagesGrouped)
->chunk(3) as $pageIndex => $pagesChunk) {
$prompts = [];
foreach ($pagesChunk as $index => $page) {
$pageContent = collect($page)->pluck('content')->toArray();
$pageContent = implode("\n", $pageContent);
$prompt = ReportBuildingFindRequirementsPrompt::prompt(
$pageContent, $message->getContent(), $message->getChatable()->description
);
$prompts[] = $prompt;
}

if (! $section) {
continue;
$this->sectionJobs[] = new MakeReportSectionsJob($prompts, $report, $document);
}

$content = $result->content;
$title = str($content)->limit(125)->toString();

Entry::updateOrCreate([
'section_id' => $section->id,
'document_id' => $section->document_id,
],
[
'title' => $title,
'content' => $content,
'type' => EntryTypeEnum::Solution,
]);

notify_ui_report($report, 'Added Solution');
} catch (\Exception $e) {
Log::error('Error running Reporting Tool Checker', [
'error' => $e->getMessage(),
'index' => $index,
'line' => $e->getLine(),
]);
}
}

}

protected function poolPrompt(array $prompts, Report $report, Document $document): void
{
$results = LlmDriverFacade::driver($report->getDriver())
->completionPool($prompts);
foreach ($results as $resultIndex => $result) {
//make the sections per the results coming back.
$content = $result->content;
$this->makeSectionFromContent($content, $document, $report);
}
}

protected function summarizeReport(Report $report): CompletionResponse
Expand All @@ -267,61 +176,6 @@ protected function summarizeReport(Report $report): CompletionResponse
return $response;
}

protected function makeSectionFromContent(
string $content,
Document $document,
Report $report): void
{
try {

notify_ui($report->getChat(), 'Building Requirements list');
notify_ui_report($report, 'Building Requirements list');

/**
* @TODO
* use the force tool feature to then
* make a tool that it has to return the values
* as
*/
$content = str($content)
->remove('```json')
->remove('```')
->toString();
$contentDecoded = json_decode($content, true);
foreach ($contentDecoded as $sectionIndex => $sectionText) {
$title = data_get($sectionText, 'title', 'NOT TITLE GIVEN');
$contentBody = data_get($sectionText, 'content', 'NOT CONTENT GIVEN');
Section::updateOrCreate([
'document_id' => $document->id,
'report_id' => $report->id,
'sort_order' => $report->refresh()->sections->count() + 1,
], [
'subject' => $title,
'content' => $contentBody,
]);
notify_ui_report($report, 'Added Requirement');
}
} catch (\Exception $e) {
Section::updateOrCreate([
'document_id' => $document->id,
'report_id' => $report->id,
'sort_order' => $report->refresh()->sections->count() + 1,
], [
'subject' => '[ERROR FORMATTING PLEASE FIX]',
'content' => $content,
]);
notify_ui_report($report, 'Added Requirement');
Log::error('Error parsing JSON', [
'error' => $e->getMessage(),
'content' => $content,
'line' => $e->getLine(),
]);
}

notify_ui($report->getChat(), 'Done Building Requirements list');
notify_ui_report($report, 'Done Building Requirements list');
}

/**
* @return PropertyDto[]
*/
Expand Down
Loading

0 comments on commit 170e72f

Please sign in to comment.