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 682f553 commit 2edd84f
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .env-dist
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ AZURE_OPENAI_REQUEST_TIMEOUT=30

COHERE_API_KEY=
COHERE_BASE_URI='https://api.cohere.com/v1/'
COHERE_DEFAULT_MODEL='cohere-r-plus'
COHERE_DEFAULT_MODEL='command-r-plus-08-2024'
COHERE_DEFAULT_MAX_TOKENS=256
COHERE_CONNECT_TIMEOUT=3
COHERE_REQUEST_TIMEOUT=30
Expand Down
2 changes: 1 addition & 1 deletion config/instructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
'clientType' => ClientType::Cohere->value,
'apiUrl' => Env::get('COHERE_API_URL', 'https://api.cohere.ai/v1'),
'apiKey' => Env::get('COHERE_API_KEY', ''),
'defaultModel' => Env::get('COHERE_DEFAULT_MODEL', 'cohere-r-plus'),
'defaultModel' => Env::get('COHERE_DEFAULT_MODEL', 'command-r-plus-08-2024'),
'defaultMaxTokens' => Env::get('COHERE_DEFAULT_MAX_TOKENS', 1024),
'connectTimeout' => Env::get('COHERE_CONNECT_TIMEOUT', 3),
'requestTimeout' => Env::get('COHERE_REQUEST_TIMEOUT', 30),
Expand Down
70 changes: 70 additions & 0 deletions examples/03_Techniques/ReflectionPrompting/run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
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('cohere')->respond(
prompt: $text,
responseModel: ReflectiveResponse::class,
mode: Mode::MdJson,
options: ['max_tokens' => 2048]
);

print("Problem:\n$text\n\n");
dump($solution);

?>
```

0 comments on commit 2edd84f

Please sign in to comment.