Skip to main content

Artisan Commands

The package provides several Artisan commands to help with development and testing.

instructor:install

Installs and configures the Instructor package.
php artisan instructor:install
# @doctest id="4239"

What It Does

  1. Publishes the configuration file (config/instructor.php)
  2. Checks for API key configuration in .env
  3. Shows next steps for getting started

Options

OptionDescription
--forceOverwrite existing configuration files

Example Output

Installing Instructor for Laravel...

Publishing configuration... done
Checking API key configuration... ✓

Next steps:

  1. Configure your API keys in .env:
     OPENAI_API_KEY=your-key-here

  2. Create a response model:
     php artisan make:response-model PersonData

  3. Extract structured data:
     $person = StructuredOutput::with(
         messages: "John is 30 years old",
         responseModel: PersonData::class,
     )->get();

  4. Test your installation:
     php artisan instructor:test

Instructor installed successfully!
// @doctest id="d99e"

instructor:test

Tests your Instructor installation and API configuration.
php artisan instructor:test
# @doctest id="f043"

What It Does

  1. Displays current configuration
  2. Makes a test API call
  3. Verifies the response

Options

OptionDescription
--preset=Test a specific connection preset
--inferenceTest raw inference instead of structured output

Examples

# Test default connection
php artisan instructor:test

# Test specific connection
php artisan instructor:test --preset=anthropic

# Test raw inference
php artisan instructor:test --inference
# @doctest id="e1ec"

Example Output

Testing Instructor installation...

Preset ............................................. openai
Driver ............................................. openai
Model .............................................. gpt-4o-mini
API Key ............................................ sk-...abc ✓

Testing structured output extraction... ✓

Structured output test completed!
// @doctest id="d167"

make:response-model

Generates a new response model class.
php artisan make:response-model {name}
# @doctest id="88f1"

Arguments

ArgumentDescription
nameThe name of the response model class

Options

OptionDescription
--collection, -cCreate a collection response model
--nested, -nCreate a nested objects response model
--description=, -dSet the class description
--force, -fOverwrite existing file

Examples

Basic Response Model

php artisan make:response-model PersonData
# @doctest id="8d7a"
Creates app/ResponseModels/PersonData.php:
<?php

declare(strict_types=1);

namespace App\ResponseModels;

/**
 * TODO: Add description for this response model
 */
final class PersonData
{
    public function __construct(
        /** The name of the person */
        public readonly string $name,

        /** The age of the person in years */
        public readonly int $age,

        /** Optional email address */
        public readonly ?string $email = null,
    ) {}
}
// @doctest id="8c03"

Collection Response Model

php artisan make:response-model ProductList --collection
# @doctest id="e7f8"
Creates a model with an array of items:
final class ProductList
{
    public function __construct(
        /** List of extracted items */
        public readonly array $items,
    ) {}
}

final class ProductListItem
{
    public function __construct(
        public readonly string $name,
        public readonly ?string $description = null,
    ) {}
}
// @doctest id="b351"

Nested Objects Response Model

php artisan make:response-model CompanyProfile --nested
# @doctest id="3043"
Creates a model with nested objects:
final class CompanyProfile
{
    public function __construct(
        public readonly string $title,
        public readonly CompanyProfileContact $contact,
        public readonly ?CompanyProfileAddress $address = null,
    ) {}
}

final class CompanyProfileContact
{
    public function __construct(
        public readonly string $name,
        public readonly string $email,
        public readonly ?string $phone = null,
    ) {}
}

final class CompanyProfileAddress
{
    public function __construct(
        public readonly string $street,
        public readonly string $city,
        public readonly string $country,
        public readonly ?string $postalCode = null,
    ) {}
}
// @doctest id="7a5f"

With Description

php artisan make:response-model Invoice --description="Represents an invoice extracted from PDF documents"
# @doctest id="e48e"
Creates a model with the specified description in the docblock.

Customizing Stubs

Publish the stubs to customize them:
php artisan vendor:publish --tag=instructor-stubs
# @doctest id="4154"
This copies stubs to stubs/instructor/:
stubs/instructor/
├── response-model.stub
├── response-model.collection.stub
└── response-model.nested.stub
// @doctest id="0207"
Edit these files to customize generated response models.

Stub Placeholders

PlaceholderDescription
{{ namespace }}The class namespace
{{ class }}The class name
{{ description }}The class description

Creating Custom Commands

Extend the package commands for your specific needs:
// app/Console/Commands/ExtractInvoiceCommand.php
namespace App\Console\Commands;

use App\ResponseModels\InvoiceData;
use Cognesy\Instructor\Laravel\Facades\StructuredOutput;
use Illuminate\Console\Command;

class ExtractInvoiceCommand extends Command
{
    protected $signature = 'invoice:extract {file}';
    protected $description = 'Extract invoice data from a file';

    public function handle(): int
    {
        $content = file_get_contents($this->argument('file'));

        $invoice = StructuredOutput::with(
            messages: $content,
            responseModel: InvoiceData::class,
        )->get();

        $this->info("Invoice Number: {$invoice->number}");
        $this->info("Amount: \${$invoice->amount}");
        $this->info("Due Date: {$invoice->dueDate}");

        return self::SUCCESS;
    }
}
// @doctest id="0c6e"

Command Reference

CommandDescription
instructor:installInstall and configure the package
instructor:testTest API configuration
make:response-modelGenerate a response model class