Skip to content

Commit

Permalink
Eval fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ddebowczyk committed Oct 9, 2024
1 parent 25c7d2c commit b082abf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/Extras/Evals/Contracts/CanExecuteExperiment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

interface CanExecuteExperiment
{
public function executeFor(EvalInput $input): self;
public static function fromEvalInput(EvalInput $input): self;
public function execute(): void;
public function getLLMResponse(): LLMResponse;
public function getAnswer(): mixed;
}
14 changes: 8 additions & 6 deletions src/Extras/Evals/Evaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ class Evaluator {
private Display $display;
private string|array|object $schema;
/** @var class-string */
private CanExecuteExperiment $executor;
private string $executor;
private string|array $messages;
private Closure $evalFn;

public function __construct(
string|array $messages,
string|array|object $schema,
CanExecuteExperiment $executorClass,
string $executorClass,
Closure $evalFn,
) {
$this->messages = $messages;
Expand Down Expand Up @@ -74,11 +74,13 @@ private function executeSingle(

// execute and measure time
$time = microtime(true);
/** @var CanExecuteExperiment $execution */
$execution = $this->executor->executeFor($evalInput);
$llmResponse = $execution->getLLMResponse();

/** @var CanExecuteExperiment $experiment */
$experiment = ($this->executor)::fromEvalInput($evalInput);
$experiment->execute();
$llmResponse = $experiment->getLLMResponse();
$evalInput->withResponse($llmResponse);
$answer = $execution->getAnswer();

$timeElapsed = microtime(true) - $time;
$isCorrect = ($this->evalFn)($evalInput);

Expand Down
9 changes: 6 additions & 3 deletions src/Extras/Evals/Inference/RunInference.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function __construct(
$this->isStreamed = $isStreamed;
}

public function executeFor(EvalInput $input) : self {
public static function fromEvalInput(EvalInput $input) : self {
$instance = new RunInference(
query: $input->messages,
schema: $input->evalSchema(),
Expand All @@ -47,11 +47,14 @@ public function executeFor(EvalInput $input) : self {
isStreamed: $input->isStreamed,
maxTokens: $input->maxTokens,
);
$instance->response = $instance->makeLLMResponse();
$instance->answer = $instance->response->content();
return $instance;
}

public function execute() : void {
$this->response = $this->makeLLMResponse();
$this->answer = $this->response->content();
}

public function getAnswer() : string {
return $this->answer;
}
Expand Down
11 changes: 7 additions & 4 deletions src/Extras/Evals/Instructor/RunInstructor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(
readonly public string $model = '',
) {}

public static function executeFor(EvalInput $input) : self {
public static function fromEvalInput(EvalInput $input) : self {
$instance = new RunInstructor(
messages: $input->messages,
responseModel: $input->schema,
Expand All @@ -46,12 +46,15 @@ public static function executeFor(EvalInput $input) : self {
examples: [],
model: '',
);
$instance->instructorResponse = $instance->makeInstructorResponse();
$instance->llmResponse = $instance->instructorResponse->response();
$instance->answer = $instance->llmResponse->value();
return $instance;
}

public function execute() : void {
$this->instructorResponse = $this->makeInstructorResponse();
$this->llmResponse = $this->instructorResponse->response();
$this->answer = $this->llmResponse->value();
}

public function getAnswer() : mixed {
return $this->answer;
}
Expand Down

0 comments on commit b082abf

Please sign in to comment.