PHP code example of refinephp / laravel-ai-batch

1. Go to this page and download the library: Download refinephp/laravel-ai-batch 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/ */

    

refinephp / laravel-ai-batch example snippets


'providers' => [
    'openai' => [
        'driver' => 'openai',
        'key' => env('OPENAI_API_KEY'),
        'url' => env('OPENAI_URL', 'https://api.openai.com/v1'),
        'store' => env('OPENAI_STORE', true),
    ],
],



declare(strict_types=1);

namespace App\Ai\Agents;

use Laravel\Ai\Contracts\Agent;
use Laravel\Ai\Promptable;

final class GeneratePullRequestSummary implements Agent
{
    use Promptable;

    public function instructions(): string
    {
        return 'Summarize the pull request accurately in at most five bullets.';
    }
}

$response = GeneratePullRequestSummary::make()->prompt($prompt);

use App\Ai\Agents\GeneratePullRequestSummary;
use Laravel\Ai\Enums\Lab;
use RefinePhp\LaravelAiBatch\Facades\AiBatch;

$firstPrompt = <<<'PROMPT'
Title: Add invoice export

Diff:
... first diff here ...
PROMPT;

$secondPrompt = <<<'PROMPT'
Title: Fix authorization policy

Diff:
... second diff here ...
PROMPT;

$batch = AiBatch::forProvider(Lab::OpenAI)
    ->name('pull-request-summaries')
    ->agent(GeneratePullRequestSummary::class)
    ->add('pr-123', prompt: $firstPrompt)
    ->add('pr-124', prompt: $secondPrompt, model: 'gpt-5.4-mini')
    ->submit();

$localId = $batch->id();
$openAiId = $batch->providerBatchId();

use Laravel\Ai\Files\LocalDocument;

$batch = AiBatch::forProvider(Lab::OpenAI)
    ->agent(AnalyzeDocument::class)
    ->add(
        'invoice-42',
        prompt: 'Extract the invoice fields.',
        attachments: [LocalDocument::fromPath(storage_path('app/invoice-42.pdf'))],
    )
    ->submit();

$request = AiBatch::resolve(
    agent: GeneratePullRequestSummary::make(),
    prompt: 'Generate the pull request summary.',
    provider: Lab::OpenAI,
);

$request->provider(); // openai
$request->method();   // POST
$request->endpoint(); // /v1/responses
$request->body();     // final provider-native body
$request->headers();  // always credential-free

$batch = AiBatch::forProvider(Lab::OpenAI)
    ->name('pull-request-summaries')
    ->addRequest('pr-123', $request)
    ->submit();

$batch = AiBatch::findOrFail($localId);
$batch = AiBatch::refresh($batch);

if ($batch->isTerminal()) {
    // Results or validation errors are now inspectable.
}

use Illuminate\Support\Facades\Schedule;

Schedule::command('ai:batch:poll --limit=100')
    ->everyFiveMinutes()
    ->withoutOverlapping();

$batch = AiBatch::findOrFail($localId);
$batch = AiBatch::refresh($batch);

$responsesByPullRequest = [];

foreach (AiBatch::results($batch) as $result) {
    if (! $result->successful()) {
        $error = $result->error();

        logger()->warning('AI batch item failed', [
            'custom_id' => $result->customId(),
            'code' => $error?->code(),
        ]);

        continue;
    }

    $responsesByPullRequest[$result->customId()] = $result->response();
}

foreach (AiBatch::errors($batch) as $error) {
    logger()->warning('AI batch item failed', [
        'custom_id' => $error->customId(),
        'code' => $error->code(),
        'parameter' => $error->parameter(),
        'line' => $error->line(),
    ]);
}

$batch = AiBatch::findOrFail($localId);
$batch = AiBatch::cancel($batch);

use Illuminate\Support\Facades\Http;

Http::preventStrayRequests();
Http::fake([
    'api.openai.com/v1/files' => Http::response(['id' => 'file-input'], 200),
    'api.openai.com/v1/batches' => Http::response([
        'id' => 'batch-test',
        'status' => 'validating',
        'input_file_id' => 'file-input',
        'request_counts' => ['total' => 1, 'completed' => 0, 'failed' => 0],
        'created_at' => 1_784_476_800,
    ], 200),
]);
bash
composer vendor:publish --tag=ai-batch-config
php artisan vendor:publish --tag=ai-batch-migrations
php artisan migrate
bash
php artisan ai:batch:poll --limit=100
php artisan queue:work
bash
php artisan ai:batch:cancel 019f7d2e-1234-7000-8000-123456789abc