PHP code example of laravilt / ai

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

    

laravilt / ai example snippets


use Laravilt\AI\AIAgent;
use Laravilt\Panel\Resources\Resource;

class ProductResource extends Resource
{
    public static function ai(AIAgent $agent): AIAgent
    {
        return $agent
            ->name('product_assistant')
            ->description('AI assistant for managing products')
            ->searchable(['name', 'description', 'sku'])
            ->canCreate(true)
            ->canUpdate(true)
            ->canDelete(false)
            ->canQuery(true)
            ->systemPrompt('You are a helpful product management assistant.');
    }
}

use Laravilt\AI\AIManager;

$ai = app(AIManager::class);
$response = $ai->provider('openai')->chat([
    ['role' => 'user', 'content' => 'Hello!'],
]);

$response = $ai->provider('anthropic')->chat([
    ['role' => 'user', 'content' => 'Hello!'],
]);

$response = $ai->provider('gemini')->chat([
    ['role' => 'user', 'content' => 'Hello!'],
]);

$response = $ai->provider('deepseek')->chat([
    ['role' => 'user', 'content' => 'Hello!'],
]);

$ai = app(AIManager::class);
$stream = $ai->provider()->streamChat([
    ['role' => 'user', 'content' => 'Write a story'],
]);

foreach ($stream as $chunk) {
    echo $chunk;
}

$response = $ai->provider()->chatWithTools(
    messages: [
        ['role' => 'user', 'content' => 'What is the weather in Tokyo?'],
    ],
    tools: [
        [
            'name' => 'get_weather',
            'description' => 'Get weather information for a city',
            'parameters' => [
                'type' => 'object',
                'properties' => [
                    'city' => ['type' => 'string', 'description' => 'City name'],
                ],
                '

use Laravilt\AI\GlobalSearch;

app(GlobalSearch::class)
    ->registerResource(
        resource: 'products',
        model: Product::class,
        searchable: ['name', 'description', 'sku'],
        label: 'Products',
        icon: 'Package',
        url: '/admin/products/{id}'
    )
    ->limit(5)
    ->useAI(true);
bash
php artisan vendor:publish --tag="laravilt-ai-config"
bash
composer analyse