From 2edd84f6d2e32b9db67e07f38dacac1a7866d2e7 Mon Sep 17 00:00:00 2001 From: ddebowczyk Date: Sat, 7 Sep 2024 08:31:55 +0200 Subject: [PATCH] Reasoning example --- .env-dist | 2 +- config/instructor.php | 2 +- .../03_Techniques/ReflectionPrompting/run.php | 70 +++++++++++++++++++ 3 files changed, 72 insertions(+), 2 deletions(-) create mode 100644 examples/03_Techniques/ReflectionPrompting/run.php diff --git a/.env-dist b/.env-dist index f59e1755..52ece262 100644 --- a/.env-dist +++ b/.env-dist @@ -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 diff --git a/config/instructor.php b/config/instructor.php index 5d9141da..515f1aa2 100644 --- a/config/instructor.php +++ b/config/instructor.php @@ -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), diff --git a/examples/03_Techniques/ReflectionPrompting/run.php b/examples/03_Techniques/ReflectionPrompting/run.php new file mode 100644 index 00000000..d44dd9e0 --- /dev/null +++ b/examples/03_Techniques/ReflectionPrompting/run.php @@ -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 +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); + +?> +```