PHP code example of pixelworxio / laravel-ai-action

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

    

pixelworxio / laravel-ai-action example snippets


// app/Ai/Actions/SummarizePost.php
final class SummarizePost implements AgentAction
{
    use InteractsWithAgent;

    public function instructions(AgentContext $context): string
    {
        return 'You are a concise technical writer. Summarize in three sentences.';
    }

    public function prompt(AgentContext $context): string
    {
        return sprintf("Summarize:\n\n%s", $context->record->body);
    }

    public function handle(AgentContext $context): AgentResult
    {
        return app(RunAgentAction::class)->execute($this, $context);
    }
}

// In a controller or job
$context = AgentContext::fromRecord($post);
$result  = $this->runner->execute(new SummarizePost(), $context);

echo $result->text;         // "This post covers..."
echo $result->inputTokens;  // 320

// In your AppServiceProvider::boot():
use Pixelworxio\LaravelAiAction\Mcp\Facades\AiActionMcp;

AiActionMcp::tool(\App\Ai\Actions\SummarizeInvoice::class);

public function middleware(): array
{
    return [
        new Idempotent(ttl: now()->addHour()),
        new FallbackProvider(['openai']),
        new RetryAgentCall(times: 3, backoffSeconds: [1, 5, 10]),
    ];
}
bash
php artisan vendor:publish --tag=ai-action-config
bash
php artisan make:ai-action SummarizePost
bash
php artisan make:ai-action SummarizeInvoice --mcp