Skip to content

Instructor for PHP

InstructorPHP is a modular AI toolkit for PHP 8.3 and later. Use a package on its own or combine packages according to your application's needs.

Choose a package

  • Polyglot provides LLM inference, embeddings, streaming, and typed decision models through a shared PHP API.
  • Instructor extracts validated PHP objects from LLM responses.
  • Agents provides an agent loop with tools, state, and execution controls.
  • AgentCtrl orchestrates coding-agent CLIs from PHP.

Install the complete toolkit:

composer require cognesy/instructor-php

For structured extraction alone, install cognesy/instructor-struct. See the Instructor quickstart for the standalone setup.

First structured extraction

Set OPENAI_API_KEY in your environment, then run:

<?php
require __DIR__ . '/vendor/autoload.php';

use Cognesy\Instructor\StructuredOutput;

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

$person = StructuredOutput::using('openai')
    ->with(
        messages: 'Jason is 28 years old.',
        responseModel: Person::class,
    )
    ->get();

echo $person->name;
echo $person->age;

Credentials and model availability depend on your provider. Keep secrets outside source code and check the documentation for the version installed by Composer.

Documentation and community