Skip to content

Commit

Permalink
Reasoning example
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Sep 7, 2024
1 parent 2edd84f commit f7f8d26
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
69 changes: 69 additions & 0 deletions docs/cookbook/examples/techniques/reflection_prompting.mdx
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);
?>
```
1 change: 1 addition & 0 deletions docs/mint.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
"cookbook/examples/techniques/image_to_data_anthropic",
"cookbook/examples/techniques/image_to_data_gemini",
"cookbook/examples/techniques/limiting_lists",
"cookbook/examples/techniques/reflection_prompting",
"cookbook/examples/techniques/restate_instructions",
"cookbook/examples/techniques/rewrite_instructions",
"cookbook/examples/techniques/search_query_expansion",
Expand Down
3 changes: 1 addition & 2 deletions examples/03_Techniques/ReflectionPrompting/run.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,9 @@ public function validate(): array {

$text = 'If a+|a|=0, try to prove that a<0';

$solution = (new Instructor)->withClient('cohere')->respond(
$solution = (new Instructor)->withClient('anthropic')->respond(
prompt: $text,
responseModel: ReflectiveResponse::class,
mode: Mode::MdJson,
options: ['max_tokens' => 2048]
);

Expand Down

0 comments on commit f7f8d26

Please sign in to comment.