-
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
1c2837a
commit adede3c
Showing
36 changed files
with
824 additions
and
189 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
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,73 @@ | ||
--- | ||
title: 'Validation with LLM' | ||
docname: 'validation_with_llm' | ||
--- | ||
|
||
## Overview | ||
|
||
You can use LLM capability to semantically process the context to validate | ||
the response following natural language instructions. This way you can | ||
implement more complex validation logic that would be difficult (or impossible) | ||
to achieve using traditional, code-based validation. | ||
|
||
## Example | ||
|
||
<?php | ||
$loader = require 'vendor/autoload.php'; | ||
$loader->add('Cognesy\\Instructor\\', __DIR__.'../../src/'); | ||
|
||
use Cognesy\Instructor\Events\Event; | ||
use Cognesy\Instructor\Extras\Scalar\Scalar; | ||
use Cognesy\Instructor\Instructor; | ||
use Cognesy\Instructor\Schema\Attributes\Description; | ||
use Cognesy\Instructor\Utils\Str; | ||
use Cognesy\Instructor\Validation\Traits\ValidationMixin; | ||
use Cognesy\Instructor\Validation\ValidationResult; | ||
|
||
class UserDetails | ||
{ | ||
use ValidationMixin; | ||
|
||
public string $name; | ||
#[Description('User details in format: key=value')] | ||
/** @var string[] */ | ||
public array $details; | ||
|
||
public function validate() : ValidationResult { | ||
return match($this->hasPII()) { | ||
true => ValidationResult::fieldError( | ||
field: 'details', | ||
value: implode('\n', $this->details), | ||
message: "Details contain PII, remove it from the response." | ||
), | ||
false => ValidationResult::valid(), | ||
}; | ||
} | ||
|
||
private function hasPII() : bool { | ||
$data = implode('\n', $this->details); | ||
return (new Instructor)->respond( | ||
messages: "Context:\n$data\n", | ||
responseModel: Scalar::boolean('hasPII', 'Does the context contain any PII?'), | ||
); | ||
} | ||
} | ||
|
||
$text = <<<TEXT | ||
My name is Jason. I am is 25 years old. I am developer. | ||
My phone number is +1 123 34 45 and social security number is 123-45-6789 | ||
TEXT; | ||
|
||
$user = (new Instructor) | ||
->wiretap(fn(Event $e) => $e->print()) // let's check the internals of Instructor processing | ||
->respond( | ||
messages: $text, | ||
responseModel: UserDetails::class, | ||
maxRetries: 2 | ||
); | ||
|
||
dump($user); | ||
|
||
assert(!Str::contains(implode('\n', $user->details), '123-45-6789')); | ||
?> | ||
``` |
This file was deleted.
Oops, something went wrong.
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,73 @@ | ||
--- | ||
title: 'Validation with LLM' | ||
docname: 'validation_with_llm' | ||
--- | ||
|
||
## Overview | ||
|
||
You can use LLM capability to semantically process the context to validate | ||
the response following natural language instructions. This way you can | ||
implement more complex validation logic that would be difficult (or impossible) | ||
to achieve using traditional, code-based validation. | ||
|
||
## Example | ||
|
||
<?php | ||
$loader = require 'vendor/autoload.php'; | ||
$loader->add('Cognesy\\Instructor\\', __DIR__.'../../src/'); | ||
|
||
use Cognesy\Instructor\Events\Event; | ||
use Cognesy\Instructor\Extras\Scalar\Scalar; | ||
use Cognesy\Instructor\Instructor; | ||
use Cognesy\Instructor\Schema\Attributes\Description; | ||
use Cognesy\Instructor\Utils\Str; | ||
use Cognesy\Instructor\Validation\Traits\ValidationMixin; | ||
use Cognesy\Instructor\Validation\ValidationResult; | ||
|
||
class UserDetails | ||
{ | ||
use ValidationMixin; | ||
|
||
public string $name; | ||
#[Description('User details in format: key=value')] | ||
/** @var string[] */ | ||
public array $details; | ||
|
||
public function validate() : ValidationResult { | ||
return match($this->hasPII()) { | ||
true => ValidationResult::fieldError( | ||
field: 'details', | ||
value: implode('\n', $this->details), | ||
message: "Details contain PII, remove it from the response." | ||
), | ||
false => ValidationResult::valid(), | ||
}; | ||
} | ||
|
||
private function hasPII() : bool { | ||
$data = implode('\n', $this->details); | ||
return (new Instructor)->respond( | ||
messages: "Context:\n$data\n", | ||
responseModel: Scalar::boolean('hasPII', 'Does the context contain any PII?'), | ||
); | ||
} | ||
} | ||
|
||
$text = <<<TEXT | ||
My name is Jason. I am is 25 years old. I am developer. | ||
My phone number is +1 123 34 45 and social security number is 123-45-6789 | ||
TEXT; | ||
|
||
$user = (new Instructor) | ||
->wiretap(fn(Event $e) => $e->print()) // let's check the internals of Instructor processing | ||
->respond( | ||
messages: $text, | ||
responseModel: UserDetails::class, | ||
maxRetries: 2 | ||
); | ||
|
||
dump($user); | ||
|
||
assert(!Str::contains(implode('\n', $user->details), '123-45-6789')); | ||
?> | ||
``` |
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 was deleted.
Oops, something went wrong.
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,28 @@ | ||
<?php | ||
|
||
namespace Cognesy\Instructor\Extras\Codebase\Actions; | ||
|
||
use Cognesy\Instructor\Extras\Codebase\Codebase; | ||
use PhpParser\Node; | ||
use PhpParser\NodeFinder; | ||
|
||
class ExtractClasses | ||
{ | ||
private Codebase $codebase; | ||
|
||
public function __construct(Codebase $codebase) { | ||
$this->codebase = $codebase; | ||
} | ||
|
||
public function __invoke(array $ast): array { | ||
$nodeFinder = new NodeFinder(); | ||
$classes = $nodeFinder->findInstanceOf($ast, Node\Stmt\Class_::class); | ||
|
||
$classList = []; | ||
foreach ($classes as $class) { | ||
$classObject = (new MakeClass($this->codebase))($class); | ||
$classList[$classObject->name] = $classObject; | ||
} | ||
return $classList; | ||
} | ||
} |
Oops, something went wrong.