Skip to content

Commit

Permalink
qa done on moving prompting deeper into the mix
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jul 27, 2024
1 parent 9351752 commit dde89e4
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 84 deletions.
7 changes: 3 additions & 4 deletions Modules/LlmDriver/app/Functions/GatherInfoTool.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use App\Domains\Reporting\ReportTypeEnum;
use App\Domains\Reporting\StatusEnum;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Jobs\GatherInfoFinalPromptJob;
use App\Jobs\GatherInfoReportSectionsJob;
use App\Models\Collection;
use App\Models\Message;
use App\Models\Report;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -89,13 +89,13 @@ public function handle(
protected function buildUpSections(Collection $collection, Report $report, Message $message): void
{
$messagePrompt = $message->getPrompt();
$collection->documents()->chunk(3, callback: function ($documentChunks) use ($message, $report, $messagePrompt) {
$collection->documents()->chunk(3, callback: function ($documentChunks) use ($report, $messagePrompt) {
try {

$prompts = [];
foreach ($documentChunks as $document) {
$prompt = Templatizer::appendContext(true)
->handle($messagePrompt,$document->original_content);
->handle($messagePrompt, $document->original_content);
$prompts[] = $prompt;
}

Expand All @@ -107,7 +107,6 @@ protected function buildUpSections(Collection $collection, Report $report, Messa
document: $document);
}


} catch (\Exception $e) {
Log::error('Error running Reporting Tool Checker', [
'error' => $e->getMessage(),
Expand Down
2 changes: 1 addition & 1 deletion Modules/LlmDriver/app/NonFunctionSearchOrSummarize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

use App\Domains\Prompts\DefaultPrompt;
use App\Domains\Prompts\SearchOrSummarize;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Domains\Prompts\SummarizeDocumentPrompt;
use App\Domains\Prompts\SummarizePrompt;
use App\Models\Collection;
use App\Models\DocumentChunk;
use App\Models\Message;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\DistanceQuery\DistanceQueryFacade;
use LlmLaraHub\LlmDriver\Responses\NonFunctionResponseDto;
Expand Down
3 changes: 1 addition & 2 deletions app/Domains/Sources/BaseSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Domains\Sources;

use App\Domains\Prompts\PromptMerge;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Domains\UnStructured\StructuredTypeEnum;
use App\Helpers\ChatHelperTrait;
use App\Jobs\DocumentProcessingCompleteJob;
Expand All @@ -14,6 +12,7 @@
use App\Models\Output;
use App\Models\Source;
use App\Models\Transformer;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Bus\Batch;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Bus;
Expand Down
5 changes: 2 additions & 3 deletions app/Domains/Sources/EmailSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

use App\Domains\Chat\MetaDataDto;
use App\Domains\Documents\StatusEnum;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Domains\Documents\TypesEnum;
use App\Domains\EmailParser\MailDto;
use App\Domains\Messages\RoleEnum;
use App\Domains\Prompts\PromptMerge;
use App\Jobs\ChunkDocumentJob;
use App\Models\Document;
use App\Models\Source;
use Facades\App\Domains\EmailParser\Client;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
Expand Down Expand Up @@ -63,7 +62,7 @@ public function handle(Source $source): void

$this->meta_data = $this->mailDto->toArray();

$prompt =Templatizer::appendContext(true)
$prompt = Templatizer::appendContext(true)
->handle($source->getPrompt(), $this->content);

Log::info('[LaraChain] - Running Email Source', [
Expand Down
5 changes: 2 additions & 3 deletions app/Domains/Sources/WebhookSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@

use App\Domains\Documents\StatusEnum;
use App\Domains\Documents\TypesEnum;
use App\Domains\Prompts\PromptMerge;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Helpers\TextChunker;
use App\Jobs\DocumentProcessingCompleteJob;
use App\Jobs\SummarizeDocumentJob;
use App\Jobs\VectorlizeDataJob;
use App\Models\Document;
use App\Models\DocumentChunk;
use App\Models\Source;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Bus\Batch;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Bus;
Expand Down Expand Up @@ -60,7 +59,7 @@ public function handle(Source $source): void
$this->createSourceTask($this->source, $key);
$encoded = json_encode($this->payload, 128);

$prompt =Templatizer::appendContext(true)
$prompt = Templatizer::appendContext(true)
->handle($this->source->getPrompt(), $encoded);

$results = LlmDriverFacade::driver(
Expand Down
72 changes: 40 additions & 32 deletions app/Domains/Tokenizer/Templatizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace App\Domains\Tokenizer;


class Templatizer
{

protected string $content;

protected array $token;
protected string|null $replacement;

protected ?string $replacement;

protected bool $appendContext = false;

Expand All @@ -17,21 +17,22 @@ public static function getTokens(): array
$reflection = new \ReflectionClass(Templatizer::class);
$methods = $reflection->getMethods(\ReflectionMethod::IS_PROTECTED | \ReflectionMethod::IS_PUBLIC);

return array_values(array_filter(array_map(function($method) {
return array_values(array_filter(array_map(function ($method) {
$name = $method->getName();
if (!in_array($name, ['makeMethod', 'handle', 'getTokens'])) {
if (! in_array($name, ['makeMethod', 'handle', 'getTokens'])) {
return strtoupper($name);
}

return null;
}, $methods)));
}

public function handle(
string $content,
string|null $replacement = null): string
?string $replacement = null): string
{

if ($this->appendContext && !str($content)->contains('[CONTEXT]')) {
if ($this->appendContext && ! str($content)->contains('[CONTEXT]')) {
$content = str($content)->append(' [CONTEXT]')->toString();
}

Expand All @@ -49,12 +50,13 @@ public function handle(

}


protected function makeMethod(string $token): string {
protected function makeMethod(string $token): string
{
return str($token)->remove(['[', ']'])->lower()->snake()->toString();
}

protected function year() {
protected function year()
{
$replacement = now()->format('Y');

$this->content =
Expand All @@ -63,9 +65,10 @@ protected function year() {
->toString();
}

protected function end_week() {
protected function end_week()
{
$replacement = now()->endOfWeek()->format('m/d/Y');
$replacement = $replacement . " " . now()->endOfWeek()->format('M d, Y');
$replacement = $replacement.' '.now()->endOfWeek()->format('M d, Y');
$this->content =
str($this->content)
->replace('[END_WEEK]', $replacement)
Expand All @@ -75,54 +78,59 @@ protected function end_week() {
public function appendContext(bool $appendContext = false): self
{
$this->appendContext = $appendContext;

return $this;
}


protected function start_week() : void {
protected function start_week(): void
{
$replacement = now()->startOfWeek()->format('m/d/Y');
$replacement = $replacement . " " . now()->startOfWeek()->format('M d, Y');
$replacement = $replacement.' '.now()->startOfWeek()->format('M d, Y');
$this->content =
str($this->content)
->replace('[START_WEEK]', $replacement)
->toString();
->toString();
}

protected function next_month() : void {
$replacement = "Month of " . now()->addMonth()->format('m ');
$replacement = $replacement . " " . now()->addMonth()->format('M');
protected function next_month(): void
{
$replacement = 'Month of '.now()->addMonth()->format('m ');
$replacement = $replacement.' '.now()->addMonth()->format('M');
$this->content =
str($this->content)
->replace('[NEXT_MONTH]', $replacement)
->toString();
->toString();
}

protected function current_month() : void {
$replacement = "Month of " . now()->format('m');
$replacement = $replacement . " " . now()->format('M');
protected function current_month(): void
{
$replacement = 'Month of '.now()->format('m');
$replacement = $replacement.' '.now()->format('M');
$this->content =
str($this->content)
->replace('[CURRENT_MONTH]', $replacement)
->toString();
->toString();
}

protected function today() : void {
protected function today(): void
{
$replacement = now()->format('m/d/Y');
$replacement = $replacement . " " . now()->format('M d, Y');
$replacement = $replacement.' '.now()->format('M d, Y');
$this->content =
str($this->content)
->replace('[START_WEEK]', $replacement)
->toString();
}

protected function user_input() : void{
$this->content = str($this->content)->replace('[USER_INPUT]', $this->replacement)->toString();
protected function user_input(): void
{
$this->content = str($this->content)->replace('[USER_INPUT]', $this->replacement)->toString();
}

protected function context() : void{
if(!empty($this->replacement)){
$this->content = str($this->content)->replace('[CONTEXT]', $this->replacement)->toString();
protected function context(): void
{
if (! empty($this->replacement)) {
$this->content = str($this->content)->replace('[CONTEXT]', $this->replacement)->toString();
}
}

}
5 changes: 2 additions & 3 deletions app/Http/Controllers/ApiOutputController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use App\Domains\Messages\RoleEnum;
use App\Domains\Outputs\OutputTypeEnum;
use App\Domains\Prompts\ChatBotPrompt;
use App\Domains\Prompts\PromptMerge;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Domains\Prompts\SupportChatBotPrompt;
use App\Models\Chat;
use App\Models\Output;
use Facades\App\Domains\Tokenizer\Templatizer;
use Facades\LlmLaraHub\LlmDriver\NonFunctionSearchOrSummarize;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\Responses\NonFunctionResponseDto;
Expand Down Expand Up @@ -38,7 +37,7 @@ public function api(Output $output)

$prompt = $output->summary;

$prompt =Templatizer::appendContext(true)
$prompt = Templatizer::appendContext(true)
->handle($prompt, $input);

$chat = Chat::firstOrCreateUsingOutput($output);
Expand Down
1 change: 0 additions & 1 deletion app/Jobs/EmailReplyOutputJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Domains\Collections\CollectionStatusEnum;
use App\Domains\EmailParser\MailDto;
use App\Domains\Prompts\PromptMerge;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Mail\OutputMail;
use App\Models\Output;
use App\Models\Persona;
Expand Down
2 changes: 0 additions & 2 deletions app/Jobs/GatherInfoFinalPromptJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@ public function handle(): void
]);
}


$messages[] = MessageInDto::from([
'content' => sprintf('Using the context of this chat can you '.
$this->report->message->getPrompt()),
'role' => 'user',
]);


$response = LlmDriverFacade::driver($this->report->getDriver())
->chat($messages);

Expand Down
3 changes: 1 addition & 2 deletions app/Jobs/GetWebContentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@
use App\Domains\Documents\StatusEnum;
use App\Domains\Documents\TypesEnum;
use App\Domains\Messages\RoleEnum;
use App\Domains\Prompts\PromptMerge;
use App\Domains\Sources\WebSearch\Response\WebResponseDto;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Helpers\ChatHelperTrait;
use App\Helpers\TextChunker;
use App\Models\Document;
use App\Models\DocumentChunk;
use App\Models\Source;
use Facades\App\Domains\Sources\WebSearch\GetPage;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Bus\Batch;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
Expand Down
3 changes: 1 addition & 2 deletions app/Jobs/SendOutputEmailJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace App\Jobs;

use App\Domains\Collections\CollectionStatusEnum;
use App\Domains\Prompts\PromptMerge;
use App\Mail\OutputMail;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Models\Output;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand Down
3 changes: 1 addition & 2 deletions app/Jobs/SummarizeDocumentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
use App\Domains\Agents\VerifyPromptInputDto;
use App\Domains\Agents\VerifyPromptOutputDto;
use App\Domains\Documents\StatusEnum;
use App\Domains\Prompts\PromptMerge;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Domains\Prompts\SummarizeDocumentPrompt;
use App\Models\Document;
use Facades\App\Domains\Agents\VerifyResponseAgent;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Bus\Batchable;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand Down
5 changes: 2 additions & 3 deletions app/Models/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
use App\Domains\Chat\MetaDataDto;
use App\Domains\Chat\ToolsDto;
use App\Domains\Messages\RoleEnum;
use Facades\App\Domains\Tokenizer\Templatizer;
use App\Events\ChatUiUpdateEvent;
use App\Events\MessageCreatedEvent;
use App\Jobs\OrchestrateJob;
use App\Jobs\SimpleSearchAndSummarizeOrchestrateJob;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Bus\Batch;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
Expand Down Expand Up @@ -127,8 +127,7 @@ public function getFilter(): ?Filter

public function getPrompt(
bool $appendContext = false
): string
{
): string {
/**
* Fix up dates and stuff
* that are automatic tokens
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

namespace App\Models;

use Facades\App\Domains\Tokenizer\Templatizer;
use App\Domains\Outputs\OutputTypeEnum;
use App\Domains\Recurring\RecurringTypeEnum;
use Carbon\Carbon;
use Facades\App\Domains\Outputs\DefaultOutput;
use Facades\App\Domains\Tokenizer\Templatizer;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
Expand Down
Loading

0 comments on commit dde89e4

Please sign in to comment.