Skip to content

Providing example inputs and outputs

Overview

To improve the results of LLM inference you can provide examples of the expected output. This will help LLM to understand the context and the expected structure of the output.

It is typically useful in the OutputMode::Json and OutputMode::MdJson modes, where the output is expected to be a JSON object.

This run prints the demonstrations, the new input, and the result so you can see the pattern being provided to the model. It does not print HTTP transport events; use a wiretap when you need to inspect the rendered request.

Example

<?php
require 'examples/boot.php';

use Cognesy\Instructor\Extras\Example\Example;
use Cognesy\Instructor\StructuredOutput;
use Cognesy\Instructor\StructuredOutputRuntime;
use Cognesy\Instructor\Enums\OutputMode;
use Cognesy\Polyglot\Inference\LLMProvider;

class User {
    public int $age;
    public string $name;
}

$examples = [
    new Example(
        input: "John is 50 and works as a teacher.",
        output: ['name' => 'John', 'age' => 50]
    ),
    new Example(
        input: "We have recently hired Ian, who is 27 years old.",
        output: ['name' => 'Ian', 'age' => 27],
        template: "example input:\n<|input|>\noutput:\n```json\n<|output|>\n```\n",
    ),
];

$input = 'Our user Jason is 25 years old.';

echo "\nFEW-SHOT EXAMPLES PROVIDED TO THE MODEL:\n";
foreach ($examples as $number => $example) {
    $output = json_encode($example->output(), JSON_UNESCAPED_SLASHES);
    echo sprintf("  %d. %s\n     => %s\n", $number + 1, $example->inputString(), $output);
}
echo "\nNEW INPUT:\n  {$input}\n";

$runtime = StructuredOutputRuntime::fromProvider(LLMProvider::using('openai'))
    ->withOutputMode(OutputMode::Json);

$user = (new StructuredOutput($runtime))
    ->withMessages($input)
    ->withResponseClass(User::class)
    ->withExamples($examples)
    ->get();

echo "\nSTRUCTURED RESULT FOR THE NEW INPUT:\n";
dump($user);
assert($user->name === 'Jason');
assert($user->age === 25);
?>