PHP code example of tacman / ai-batch-bundle

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

    

tacman / ai-batch-bundle example snippets


Tacman\AiBatch\TacmanAiBatchBundle::class => ['all' => true],

use Tacman\AiBatch\Model\BatchRequest;
use Tacman\AiBatch\Service\AiBatchBuilder;

$batch = $batchBuilder->build(
    datasetKey:     'my-collection',
    task:           'image_enrichment',
    records:        $normalizedRecords,         // iterable
    requestFactory: fn(array $row) => new BatchRequest(
        customId:     $row['id'],
        systemPrompt: 'You are a museum cataloguer...',
        userPrompt:   'Describe this image and extract keywords.',
        model:        'gpt-4o-mini',
        imageUrl:     $row['thumbnail_url'],    // image_url, not base64
    ),
);

$batch = $batchBuilder->submit($batch);
$entityManager->persist($batch);
$entityManager->flush();

echo "Batch #{$batch->id} submitted: {$batch->providerBatchId}";

$job = $batchClient->checkBatch($batch->providerBatchId);

if ($job->isComplete()) {
    foreach ($batchClient->fetchResults($job) as $result) {
        // $result->customId maps back to your record id
        // $result->content is the parsed JSON response
        $enrichment = MediaEnrichment::fromNormalized($records[$result->customId]);
        $enrichment->applyAiEnrichment($result->content);
        // push to zm, update DB, etc.
    }
}

// In your app — implement a handler that calls checkBatch() on all
// AiBatch entities with status='processing'

interface BatchCapablePlatformInterface
{
    public function supportsBatch(): bool;
    public function submitBatch(array $requests, array $options = []): BatchJob;
    public function checkBatch(string $batchId): BatchJob;
    public function fetchResults(BatchJob $job): iterable;  // yields BatchResult
    public function cancelBatch(string $batchId): BatchJob;
}