PHP code example of katema / laravel-genai

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

    

katema / laravel-genai example snippets


use Katema\LaravelGenAI\Facades\AI;

$response = AI::text('Write a product description for a coffee mug');
echo $response->content;

$messages = [
    ['role' => 'user', 'content' => 'What is Laravel?'],
];

$response = AI::chat($messages);
echo $response->content;

$data = AI::json('Generate a user profile with name, email, and bio');
// Returns: ['name' => 'John Doe', 'email' => '[email protected]', 'bio' => '...']

$response = AI::withContext([
    'user' => auth()->user()->name,
    'company' => 'Acme Corp'
])->text('Write a welcome email');

$response = AI::prompt('marketing.product_description', [
    'product_name' => 'Smart Coffee Maker',
    'category' => 'Kitchen Appliances',
    'features' => 'WiFi enabled, programmable, auto-shutoff'
]);

use Katema\LaravelGenAI\Traits\HasAI;

class Product extends Model
{
    use HasAI;
}

$product = Product::find(1);

// Generate a summary
$summary = $product->summarize();

// Generate a description
$description = $product->describe();

// Ask questions about the model
$insights = $product->insights('What makes this product unique?');

// Use OpenAI
$response = AI::driver('openai')->text('Hello');

// Use Claude
$response = AI::driver('claude')->text('Hello');

'providers' => [
    'openai' => [
        'api_key' => env('OPENAI_API_KEY'),
        'model' => env('OPENAI_MODEL', 'gpt-4-turbo-preview'),
    ],
    'claude' => [
        'api_key' => env('CLAUDE_API_KEY'),
        'model' => env('CLAUDE_MODEL', 'claude-3-5-sonnet-20241022'),
    ],
],

$response = AI::withSystemPrompt('You are a helpful marketing assistant')
    ->chat($messages);

$response = AI::text('Write a story', [
    'temperature' => 0.9,
    'max_tokens' => 500,
    'model' => 'gpt-4'
]);

AI::withContext(['user' => 'Alice'])
    ->text('First request');

// Clear context for next request
AI::fresh()->text('Second request');

$response = AI::text('Hello');

echo $response->tokensUsed; // 150
echo $response->cost;       // 0.0045
echo $response->model;      // gpt-4-turbo-preview
echo $response->provider;   // openai

// Default provider
'default' => env('GENAI_PROVIDER', 'openai'),

// Rate limiting
'rate_limits' => [
    'enabled' => true,
    'max_requests_per_minute' => 60,
    'max_tokens_per_day' => 100000,
],

// Safety
'safety' => [
    'prompt_injection_detection' => true,
    'output_validation' => true,
    'max_prompt_length' => 10000,
],
bash
php artisan vendor:publish --tag=genai-config
bash
# Install the package
php artisan genai:install

# Create a new prompt template
php artisan genai:prompt marketing.email

# Test a prompt
php artisan genai:test marketing.email --var="product=Coffee"