PHP code example of cattyneo / laravel-genai

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

    

cattyneo / laravel-genai example snippets


'defaults' => [
    'timeout' => 30,
    'async' => true,
    'provider' => 'openai',
    'model' => 'gpt-4.1-mini',
    'options' => [
        'temperature' => 0.7,
        'max_tokens' => 2000,
        // ... additional options
    ],
],

'cache' => [
    'enabled' => true,
    'driver' => 'redis',
    'ttl' => 3600,
    'prefix' => 'genai_cache',
    'tags' => ['genai'],
],

'notifications' => [
    'deprecation_channels' => ['log', 'mail'],
    'cost_alert_channels' => ['log', 'mail'],
    'cost_thresholds' => [
        'warning' => 10000,  // ¥10,000
        'critical' => 50000, // ¥50,000
    ],
],

use CattyNeo\LaravelGenAI\Facades\GenAI;

// Simple question
$response = GenAI::ask('Hello, how are you?');

// With specific provider and model
$response = GenAI::provider('openai')
    ->model('gpt-4o')
    ->ask('Explain Laravel in simple terms');

// Using presets
$response = GenAI::preset('blog')
    ->prompt('Write about AI and Laravel')
    ->request();

// With custom options
$response = GenAI::provider('claude')
    ->model('claude-3-opus')
    ->options([
        'temperature' => 0.7,
        'max_tokens' => 1000,
    ])
    ->ask('Create a detailed project plan');

// With streaming
$response = GenAI::provider('openai')
    ->options(['stream' => true])
    ->ask('Generate a long story')
    ->stream();

// Async requests
$response = GenAI::preset('analysis')
    ->options(['async' => true])
    ->prompt('Analyze this data: ' . $data)
    ->request();

$response = GenAI::preset('blog')
    ->vars(['topic' => 'AI Ethics', 'tone' => 'professional'])
    ->request();

use CattyNeo\LaravelGenAI\Services\GenAI\NotificationService;

$notificationService = app(NotificationService::class);

// Send cost alert
$notificationService->sendCostAlert([
    'current_cost' => 150.0,
    'budget' => 100.0,
    'period' => 'daily'
]);

// Send performance alert
$notificationService->sendPerformanceAlert([
    'metric' => 'response_time',
    'current_value' => 5500,
    'threshold' => 5000,
    'degradation_percent' => 10.0
]);

// Cache configuration
'cache' => [
    'enabled' => true,
    'driver' => 'redis',
    'ttl' => 3600,
    'prefix' => 'genai:',
]

'rate_limits' => [
    'default' => ['requests' => 100, 'per' => 'minute'],
    'openai' => ['requests' => 60, 'per' => 'minute'],
    'claude' => ['requests' => 40, 'per' => 'minute'],
]

// In your tests
GenAI::fake([
    'ask' => 'Mocked response',
    'request' => 'Another mock',
]);

// Assert requests
GenAI::assertRequested('ask', 'Hello');
bash
php artisan genai:install
bash
# Install the package
php artisan genai:install

# Install with options
php artisan genai:install --force --skip-migration
bash
# Test all providers
php artisan genai:test

# Test specific provider
php artisan genai:test --provider=openai

# Test with custom prompt
php artisan genai:test --prompt="Hello, world!" --provider=claude
bash
# Show usage statistics
php artisan genai:stats

# Statistics for specific period
php artisan genai:stats --days=30

# Detailed breakdown
php artisan genai:stats --detailed --provider=openai