PHP code example of basillangevin / instructor-laravel

1. Go to this page and download the library: Download basillangevin/instructor-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

basillangevin / instructor-laravel example snippets


use EchoLabs\Prism\Enums\Provider;

// config for BasilLangevin/InstructorLaravel
return [
    /*
    |--------------------------------------------------------------------------
    | Default LLM Provider
    |--------------------------------------------------------------------------
    |
    | This value is the default LLM provider that this package will use to
    | generate a structured response that it will transform into a Data
    | object. You may also set LLM providers on a per-request basis.
    */
    'provider' => Provider::OpenAI,

    /*
    |--------------------------------------------------------------------------
    | Default LLM Model
    |--------------------------------------------------------------------------
    |
    | This value is the default LLM model that this package will use
    | for the LLM provider when generating a structured response.
    | You may also set the model each time you make a request.
    */
    'model' => 'gpt-4o',
];

use Spatie\LaravelData\Attributes\Min;
use Spatie\LaravelData\Data;

class BirdData extends Data
{
    public function __construct(
        public string $species,

        /** The average wingspan of the bird in centimeters. */
        public int $wingspan,

        #[In(['forest', 'prarie', 'wetland'])]
        public string $habitat,
    ) {}
}

use BasilLangevin\InstructorLaravel\Facades\Instructor;
use EchoLabs\Prism\Enums\Provider;

$bird = Instructor::make()
    ->withSchema(BirdData::class)
    ->using(Provider::OpenAI, 'gpt-4o')
    ->withPrompt('Tell me about a bird found on the West Coast of Canada.')
    ->generate();

BirdData {
  +species: "Western Bluebird"
  +wingspan: 34
  +habitat: "forest"
}

$birds = Instructor::make()
    ->withCollectionSchema(BirdData::class)
    ...

$birds = Instructor::make()
    ->withCollectionSchema(BirdData::class, BirdCollection::class)
    ...

$bird = Instructor::withSchema(BirdData::class)
    ->withRetries(5)
    ...
bash
php artisan vendor:publish --tag="instructor-laravel-config"