<?php
require 'examples/boot.php';
use Cognesy\Instructor\Extras\Scalar\Scalar;
use Cognesy\Instructor\StructuredOutput;
class EstimateUncertainty {
public function __invoke(int $k = 5) : float {
$values = [];
for ($i = 0; $i < $k; $i++) {
$values[] = $this->queryHeight();
}
return $this->disagreement($values);
}
private function queryHeight() : int {
return StructuredOutput::using('openai')->with(
messages: [['role' => 'user', 'content' => 'How tall is the Empire State Building in meters?']],
responseModel: Scalar::integer('height'),
)->get();
}
private function disagreement(array $responses) : float {
$n = count($responses);
if ($n === 0) return 0.0;
return count(array_unique($responses)) / $n;
}
}
$score = (new EstimateUncertainty)(k: 5);
dump($score);
assert(is_float($score));
assert($score >= 0.0 && $score <= 1.0);
?>