Skip to content

Commit

Permalink
This adds a verifcation check to the get information area
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Aug 9, 2024
1 parent 4b3af54 commit 509bf80
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 22 deletions.
36 changes: 36 additions & 0 deletions app/Domains/Prompts/VerifyPrompt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Domains\Prompts;

use Illuminate\Support\Facades\Log;

class VerifyPrompt
{
public static function prompt(string $originalResults, string $context): string
{
Log::info('[LaraChain] - Verify Prompt');

return <<<PROMPT
<ROLE>
You are here to verify the results of a previous LLM. Below is the results,
the context for that prompt
<TASK>
Compare the results of the prompt to the data / context it was given and fix any issues.
Return then the fixed results.
<format>
Sae format seen in the original results. Just make sure to not include anything like
"I have checkec the results and they are correct" or anything like that.
<original results>
$originalResults
<context>
$context
PROMPT;
}
}
40 changes: 21 additions & 19 deletions app/Jobs/GatherInfoFinalPromptJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
namespace App\Jobs;

use App\Domains\Messages\RoleEnum;
use App\Domains\Prompts\VerifyPrompt;
use App\Domains\Reporting\StatusEnum;
use Facades\App\Domains\Tokenizer\Templatizer;
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 Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\LlmDriverFacade;
use LlmLaraHub\LlmDriver\Requests\MessageInDto;
use LlmLaraHub\LlmDriver\ToolsHelper;

class GatherInfoFinalPromptJob implements ShouldQueue
Expand All @@ -35,29 +37,30 @@ public function handle(): void
{
$messages = [];

$history = [];
$context = [];

foreach ($this->report->sections as $section) {
$messages[] = MessageInDto::from([
'content' => $section->content,
'role' => 'user',
]);
$context[] = $section->content;
}

$history[] = $section->content;
$context = implode("\n", $context);

$messages[] = MessageInDto::from([
'content' => 'Using the surrounding context to continue this response thread',
'role' => 'assistant',
]);
}
$prompt = Templatizer::appendContext(true)
->handle($this->report->message->getContent(), $context);

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

Log::info('GatherInfoReportSectionsJob doing one more check', [
'response' => $response->content,
]);

$prompt = VerifyPrompt::prompt(
originalResults: $response->content,
context: $context);

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

$assistantMessage = $this->report->getChat()->addInput(
message: $response->content,
Expand All @@ -73,8 +76,7 @@ public function handle(): void
$this->report->status_entries_generation = StatusEnum::Complete;
$this->report->save();

$this->savePromptHistory($assistantMessage,
implode("\n", $history));
$this->savePromptHistory($assistantMessage, $prompt);

notify_ui($this->report->getChat(), 'Building Solutions list');
notify_ui_report($this->report, 'Building Solutions list');
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Jobs/GatherInfoFinalPromptJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public function test_final_prompt(): void
]
);

LlmDriverFacade::shouldReceive('driver->chat')
->once()
LlmDriverFacade::shouldReceive('driver->completion')
->twice()
->andReturn(
CompletionResponse::from([
'content' => 'Foo bar',
Expand Down
34 changes: 33 additions & 1 deletion tests/fixtures/claude_payload_chat.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,39 @@
"max_tokens": 4096,
"messages": [
{
"content": "Using the context of this chat can you can you list off all the September events",
"content": "Based on the Dallas Cowboys schedule provided, the main events happening in August are:\n\n1. Preseason Week 1: Sunday, August 11 at 3:30 PM CDT - Cowboys at Los Angeles Rams\n\n2. Preseason Week 2: Saturday, August 17 at 9:00 PM CDT - Cowboys at Las Vegas Raiders\n\n3. Preseason Week 3: Saturday, August 24 at 3:00 PM CDT - Los Angeles Chargers at Cowboys (home game at AT&T Stadium)\n\nThese are the three preseason games scheduled for the Dallas Cowboys in August, which serve as warmup matches before the regular season begins in September.",
"role": "user"
},
{
"content": "Using the surrounding context to continue this response thread",
"role": "assistant"
},
{
"content": "Based on the Dallas Cowboys schedule provided, the following events happen in August:\n\n1. Week 1 Preseason Game:\n- Sunday, August 11 at 3:30 PM CDT\n- Cowboys at Los Angeles Rams\n- SoFi Stadium\n\n2. Week 2 Preseason Game:\n- Saturday, August 17 at 9:00 PM CDT \n- Cowboys at Las Vegas Raiders\n- Allegiant Stadium\n\n3. Week 3 Preseason Game:\n- Saturday, August 24 at 3:00 PM CDT\n- Los Angeles Chargers at Cowboys \n- AT&T Stadium\n\nThese three preseason games make up the Cowboys' August schedule. The regular season begins in September.",
"role": "user"
},
{
"content": "Using the surrounding context to continue this response thread",
"role": "assistant"
},
{
"content": "Based on the Dallas Cowboys schedule provided, the main events happening in August are:\n\n1. Week 1 Preseason Game: \nSunday, August 11 at 3:30 PM CDT\nCowboys at Los Angeles Rams\nSoFi Stadium\n\n2. Week 2 Preseason Game:\nSaturday, August 17 at 9:00 PM CDT \nCowboys at Las Vegas Raiders\nAllegiant Stadium\n\n3. Week 3 Preseason Game:\nSaturday, August 24 at 3:00 PM CDT\nLos Angeles Chargers at Cowboys \nAT&T Stadium\n\nThese three preseason games make up the Cowboys' August schedule as they prepare for the regular season, which begins in September. The preseason allows teams to evaluate players and get ready for the upcoming year.",
"role": "user"
},
{
"content": "Using the surrounding context to continue this response thread",
"role": "assistant"
},
{
"content": "Using the context of this chat can you what events happen in august",
"role": "user"
},
{
"content": "Certainly! Based on the context of our conversation, the main events happening for the Dallas Cowboys in August are:\n\n1. Week 1 Preseason Game:\n - Date: Sunday, August 11 at 3:30 PM CDT\n - Matchup: Cowboys at Los Angeles Rams\n - Venue: SoFi Stadium\n\n2. Week 2 Preseason Game:\n - Date: Saturday, August 17 at 9:00 PM CDT\n - Matchup: Cowboys at Las Vegas Raiders\n - Venue: Allegiant Stadium\n\n3. Week 3 Preseason Game:\n - Date: Saturday, August 24 at 3:00 PM CDT\n - Matchup: Los Angeles Chargers at Cowboys\n - Venue: AT&T Stadium (home game)\n\nThese three preseason games constitute the Dallas Cowboys' schedule for August. They serve as preparation for the regular season, which begins in September. Preseason games are important for teams to evaluate players, test strategies, and get ready for the upcoming regular season.",
"role": "assistant"
},
{
"content": "can you review these results with the history of this chat and fix any issues Certainly! Based on the context of our conversation, the main events happening for the Dallas Cowboys in August are:\n\n1. Week 1 Preseason Game:\n - Date: Sunday, August 11 at 3:30 PM CDT\n - Matchup: Cowboys at Los Angeles Rams\n - Venue: SoFi Stadium\n\n2. Week 2 Preseason Game:\n - Date: Saturday, August 17 at 9:00 PM CDT\n - Matchup: Cowboys at Las Vegas Raiders\n - Venue: Allegiant Stadium\n\n3. Week 3 Preseason Game:\n - Date: Saturday, August 24 at 3:00 PM CDT\n - Matchup: Los Angeles Chargers at Cowboys\n - Venue: AT&T Stadium (home game)\n\nThese three preseason games constitute the Dallas Cowboys' schedule for August. They serve as preparation for the regular season, which begins in September. Preseason games are important for teams to evaluate players, test strategies, and get ready for the upcoming regular season.",
"role": "user"
}
],
Expand Down

0 comments on commit 509bf80

Please sign in to comment.