-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2edd84f
commit f7f8d26
Showing
3 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
69 changes: 69 additions & 0 deletions
69
docs/cookbook/examples/techniques/reflection_prompting.mdx
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,69 @@ | ||
--- | ||
title: 'Reflection Prompting' | ||
docname: 'reflection_prompting' | ||
--- | ||
|
||
## Overview | ||
|
||
This implementation of Reflection Prompting with Instructor provides a structured way | ||
to encourage LLM to engage in more thorough and self-critical thinking processes, | ||
potentially leading to higher quality and more reliable outputs. | ||
|
||
|
||
## Example | ||
|
||
```php | ||
<?php | ||
$loader = require 'vendor/autoload.php'; | ||
$loader->add('Cognesy\\Instructor\\', __DIR__.'../../src/'); | ||
|
||
use Cognesy\Instructor\Enums\Mode; | ||
use Cognesy\Instructor\Instructor; | ||
use Cognesy\Instructor\Schema\Attributes\Instructions; | ||
|
||
class ReflectiveResponse { | ||
#[Instructions('Is problem solvable and what domain expertise it requires')] | ||
public string $assessment; | ||
|
||
#[Instructions('Describe a persona who would be able to solve this problem, their skills and experience')] | ||
public string $persona; | ||
|
||
#[Instructions('Initial analysis and approach to the problem of the expert persona')] | ||
public string $initialThinking; | ||
|
||
/** @var string[] */ | ||
#[Instructions('Steps of reasoning leading to the final answer - how would the expert persona think through the problem')] | ||
public array $chainOfThought; | ||
|
||
#[Instructions('Critical examination of the reasoning process - what could go wrong, what are the assumptions')] | ||
public string $reflection; | ||
|
||
#[Instructions('Final answer after reflection')] | ||
public string $finalOutput; | ||
|
||
// Validation method to ensure thorough reflection | ||
public function validate(): array { | ||
$errors = []; | ||
if (empty($this->reflection)) { | ||
$errors[] = "Reflection is required for a thorough response."; | ||
} | ||
if (count($this->chainOfThought) < 2) { | ||
$errors[] = "Please provide at least two steps in the chain of thought."; | ||
} | ||
return $errors; | ||
} | ||
} | ||
|
||
$text = 'If a+|a|=0, try to prove that a<0'; | ||
$solution = (new Instructor)->withClient('anthropic')->respond( | ||
prompt: $text, | ||
responseModel: ReflectiveResponse::class, | ||
options: ['max_tokens' => 2048] | ||
); | ||
print("Problem:\n$text\n\n"); | ||
dump($solution); | ||
?> | ||
``` |
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