-
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
bed43c8
commit 97e22f0
Showing
21 changed files
with
513 additions
and
449 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
use Cognesy\Instructor\Extras\Evals\Contracts\CanEvaluateExperiment; | ||
use Cognesy\Instructor\Extras\Evals\Contracts\Metric; | ||
use Cognesy\Instructor\Extras\Evals\Data\InferenceCases; | ||
use Cognesy\Instructor\Extras\Evals\Data\InstructorData; | ||
use Cognesy\Instructor\Extras\Evals\Experiment; | ||
use Cognesy\Instructor\Extras\Evals\Inference\RunInstructor; | ||
use Cognesy\Instructor\Extras\Evals\Metrics\BooleanCorrectness; | ||
use Cognesy\Instructor\Extras\Evals\Runner; | ||
|
||
$loader = require 'vendor/autoload.php'; | ||
$loader->add('Cognesy\\Instructor\\', __DIR__ . '../../src/'); | ||
|
||
$cases = InferenceCases::get( | ||
connections: [], | ||
modes: [], | ||
stream: [] | ||
); | ||
|
||
class Company { | ||
public string $name; | ||
public int $foundingYear; | ||
} | ||
|
||
$data = new InstructorData( | ||
messages: [ | ||
['role' => 'user', 'content' => 'YOUR GOAL: Use tools to store the information from context based on user questions.'], | ||
['role' => 'user', 'content' => 'CONTEXT: Our company ACME was founded in 2020.'], | ||
//['role' => 'user', 'content' => 'EXAMPLE CONTEXT: Sony was established in 1946 by Akio Morita.'], | ||
//['role' => 'user', 'content' => 'EXAMPLE RESPONSE: ```json{"name":"Sony","year":1899}```'], | ||
['role' => 'user', 'content' => 'What is the name and founding year of our company?'], | ||
], | ||
responseModel: Company::class, | ||
); | ||
|
||
class CompanyEval implements CanEvaluateExperiment | ||
{ | ||
public function evaluate(Experiment $experiment) : Metric { | ||
/** @var Person $decoded */ | ||
$person = $experiment->response->value(); | ||
$result = $person->name === 'ACME' | ||
&& $person->foundingYear === 2020; | ||
return new BooleanCorrectness($result); | ||
} | ||
} | ||
|
||
//Debug::enable(); | ||
|
||
//$report = file_get_contents(__DIR__ . '/report.txt'); | ||
//$examples = require 'examples.php'; | ||
//$prompt = 'Extract a list of project events with all the details from the provided input in JSON format using schema: <|json_schema|>'; | ||
//$responseModel = Sequence::of(ProjectEvent::class); | ||
|
||
$runner = new Runner( | ||
executor: new RunInstructor($data), | ||
evaluator: new CompanyEval(), | ||
); | ||
|
||
$outputs = $runner->execute($cases); |
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,51 @@ | ||
<?php | ||
|
||
namespace Cognesy\Evals\LLMModes; | ||
|
||
use Cognesy\Instructor\Enums\Mode; | ||
use Cognesy\Instructor\Extras\Evals\Contracts\CanEvaluateExperiment; | ||
use Cognesy\Instructor\Extras\Evals\Experiment; | ||
use Cognesy\Instructor\Extras\Evals\Metrics\BooleanCorrectness; | ||
use Cognesy\Instructor\Extras\Evals\Contracts\Metric; | ||
use Cognesy\Instructor\Utils\Str; | ||
|
||
class CompanyEval implements CanEvaluateExperiment | ||
{ | ||
private array $expectations; | ||
|
||
public function __construct(array $expectations) { | ||
$this->expectations = $expectations; | ||
} | ||
|
||
public function evaluate(Experiment $experiment) : Metric { | ||
$isCorrect = match ($experiment->mode) { | ||
Mode::Text => $this->validateText($experiment), | ||
Mode::Tools => $this->validateToolsData($experiment), | ||
default => $this->validateDefault($experiment), | ||
}; | ||
return new BooleanCorrectness($isCorrect); | ||
} | ||
|
||
private function validateToolsData(Experiment $experiment) : bool { | ||
$data = $experiment->response->toolsData; | ||
return 'store_company' === ($data[0]['name'] ?? '') | ||
&& 'ACME' === ($data[0]['arguments']['name'] ?? '') | ||
&& 2020 === (int) ($data[0]['arguments']['year'] ?? 0); | ||
} | ||
|
||
private function validateDefault(Experiment $experiment) : bool { | ||
$decoded = json_decode($experiment->response->json(), true); | ||
return $this->expectations['name'] === ($decoded['name'] ?? '') | ||
&& $this->expectations['foundingYear'] === ($decoded['year'] ?? 0); | ||
} | ||
|
||
private function validateText(Experiment $experiment) : bool { | ||
return Str::contains( | ||
$experiment->response->content(), | ||
[ | ||
$this->expectations['name'], | ||
(string) $this->expectations['foundingYear'] | ||
] | ||
); | ||
} | ||
} |
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,8 @@ | ||
<?php | ||
|
||
namespace Cognesy\Evals\SimpleExtraction; | ||
|
||
class Company { | ||
public string $name; | ||
public int $foundingYear; | ||
} |
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,24 @@ | ||
<?php | ||
|
||
namespace Cognesy\Evals\SimpleExtraction; | ||
|
||
use Cognesy\Instructor\Extras\Evals\Contracts\CanEvaluateExperiment; | ||
use Cognesy\Instructor\Extras\Evals\Experiment; | ||
use Cognesy\Instructor\Extras\Evals\Metrics\BooleanCorrectness; | ||
use Cognesy\Instructor\Extras\Evals\Contracts\Metric; | ||
|
||
class CompanyEval implements CanEvaluateExperiment | ||
{ | ||
private array $expectations; | ||
|
||
public function __construct(array $expectations) { | ||
$this->expectations = $expectations; | ||
} | ||
|
||
public function evaluate(Experiment $experiment) : Metric { | ||
$company = $experiment->response->value(); | ||
$isCorrect = $company->name === $this->expectations['name'] | ||
&& $company->foundingYear === $this->expectations['foundingYear']; | ||
return new BooleanCorrectness($isCorrect); | ||
} | ||
} |
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
Oops, something went wrong.