-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add the verification process to the prompt
- Loading branch information
Showing
12 changed files
with
290 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?php | ||
|
||
namespace App\Domains\Agents; | ||
|
||
abstract class BaseAgent | ||
{ | ||
abstract public function verify(VerifyPromptInputDto $input) : VerifyPromptOutputDto; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Agents | ||
|
||
This will be for specific case classes. | ||
I am calling it agents but in the end this will be a fine line between agents and tools/functions. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Domains\Agents; | ||
|
||
use LlmLaraHub\LlmDriver\HasDrivers; | ||
use Spatie\LaravelData\Data; | ||
|
||
class VerifyPromptInputDto extends Data { | ||
|
||
public function __construct( | ||
public HasDrivers $chattable, | ||
public string $originalPrompt, | ||
public string $context, | ||
public string $llmResponse, | ||
public string $verifyPrompt | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace App\Domains\Agents; | ||
|
||
use LlmLaraHub\LlmDriver\HasDrivers; | ||
use Spatie\LaravelData\Data; | ||
|
||
class VerifyPromptOutputDto extends Data { | ||
|
||
public function __construct( | ||
public HasDrivers $chattable, | ||
public string $originalPrompt, | ||
public string $context, | ||
public string $llmResponse, | ||
public string $verifyPrompt, | ||
public string $response | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
|
||
namespace App\Domains\Agents; | ||
|
||
use Illuminate\Support\Facades\Log; | ||
use LlmLaraHub\LlmDriver\LlmDriverFacade; | ||
use LlmLaraHub\LlmDriver\Responses\CompletionResponse; | ||
|
||
class VerifyResponseAgent extends BaseAgent | ||
{ | ||
public function verify(VerifyPromptInputDto $input) : VerifyPromptOutputDto | ||
{ | ||
|
||
Log::info("[LaraChain] VerifyResponseAgent::verify"); | ||
$originalPrompt = $input->originalPrompt; | ||
$context = $input->context; | ||
$llmResponse = $input->llmResponse; | ||
$verifyPrompt = $input->verifyPrompt; | ||
|
||
|
||
$prompt = <<<EOT | ||
As a date verification assistant please review the following and return | ||
a response that cleans up the original "LLM RESPONSE" included below. | ||
What is key for you to do is that this is a RAG systems so if the original "LLM RESPONSE" response does not | ||
line up with the data in the "CONTEXT" then remove any questionable text and | ||
numbers. See VERIFY PROMPT for any additional information. The output here | ||
will go directly to the user in a chat window so please reply accordingly. | ||
Your Response will not include anything about the verification process you are just a proxy to the origin LLM RESPONSE. | ||
Your Response will be that just cleaned up for chat. | ||
DO NOT include text like "Here is the cleaned-up response" the user should not even know your step happened :) | ||
Your repsonse will NOT be a list like below but just follow the formatting of the "LLM RESPONSE". | ||
### Included are the following sections | ||
- ORIGINAL PROMPT: The question from the user | ||
- CONTEXT: | ||
- LLM RESPONSE: The response from the LLM system using the original prompt and context | ||
- VERIFY PROMPT: The prompt added to help clear up the required output. | ||
### START ORIGINAL PROMPT | ||
{$originalPrompt} | ||
### END ORIGINAL PROMPT | ||
### START CONTEXT | ||
{$context} | ||
### END CONTEXT | ||
### START LLM RESPONSE | ||
{$llmResponse} | ||
### END LLM RESPONSE | ||
### START VERIFY PROMPT | ||
{$verifyPrompt} | ||
### END VERIFY PROMPT | ||
EOT; | ||
|
||
|
||
Log::info("[LaraChain] VerifyResponseAgent::verify", [ | ||
'prompt' => $prompt | ||
]); | ||
/** @var CompletionResponse $response */ | ||
$response = LlmDriverFacade::driver( | ||
$input->chattable->getDriver() | ||
)->completion($prompt, $input->llmResponse); | ||
|
||
return VerifyPromptOutputDto::from( | ||
[ | ||
'chattable' => $input->chattable, | ||
'originalPrompt' => $input->originalPrompt, | ||
'context' => $input->context, | ||
'llmResponse' => $input->llmResponse, | ||
'verifyPrompt' => $input->verifyPrompt, | ||
'response' => $response->content | ||
] | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace Tests\Feature; | ||
|
||
use App\Domains\Agents\VerifyPromptInputDto; | ||
use App\Domains\Agents\VerifyPromptOutputDto; | ||
use App\Domains\Agents\VerifyResponseAgent; | ||
use App\Models\Chat; | ||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use Illuminate\Foundation\Testing\WithFaker; | ||
use LlmLaraHub\LlmDriver\LlmDriverFacade; | ||
use LlmLaraHub\LlmDriver\Responses\CompletionResponse; | ||
use Tests\TestCase; | ||
|
||
class VerifyResponseAgentTest extends TestCase | ||
{ | ||
/** | ||
* A basic feature test example. | ||
*/ | ||
public function test_agent(): void | ||
{ | ||
$chat = Chat::factory()->create(); | ||
|
||
$response = CompletionResponse::from([ | ||
'content' => 'test' | ||
]); | ||
|
||
LlmDriverFacade::shouldReceive('driver->completion')->once()->andReturn($response); | ||
|
||
$verifyPromptInput = VerifyPromptInputDto::from([ | ||
'chattable' => $chat, | ||
'originalPrompt' => 'test', | ||
'context' => 'test', | ||
'llmResponse' => 'test', | ||
'verifyPrompt' => 'test' | ||
]); | ||
|
||
|
||
$response = (new VerifyResponseAgent())->verify($verifyPromptInput); | ||
|
||
$this->assertInstanceOf(VerifyPromptOutputDto::class, $response); | ||
|
||
} | ||
} |
Oops, something went wrong.