PHP code example of pointerdev / ai-chat-laravel

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

    

pointerdev / ai-chat-laravel example snippets


use PointerDev\AIChat\Facades\AIChat;

$result = AIChat::chat([
    'message' => 'Hello from Laravel',
    'anon_uid' => 'demo-anon-user',
    'metadata' => ['source' => 'laravel-app'],
]);

$answer = $result['answer'] ?? null;

use PointerDev\AIChat\Facades\AIChat;

$client = AIChat::withEndUserToken('<END_USER_JWT>');

// Exchange once for short-lived runtime session token
$client->exchangeSessionToken();

$result = $client->chat([
    'message' => 'Hello from authenticated user',
]);

use PointerDev\AIChat\Facades\AIChat;

$client = AIChat::withEndUserToken($endUserJwtFromYourBackend);

// Exchange end-user token for runtime session token
$sessionAuth = $client->exchangeSessionToken();

// Chat now prefers runtime session token automatically
$result = $client->chat([
    'message' => 'Hello from authenticated user',
    'metadata' => ['source' => 'laravel-app'],
]);

// Optional explicit refresh/revoke
$client->refreshSessionToken();
$client->revokeSessionToken();

Route::middleware(['web', 'auth', 'ai-chat.runtime-session'])->group(function () {
    Route::get('/chat', function () {
        $result = app(\PointerDev\AIChat\AIChatClient::class)->chat([
            'message' => 'Hello from middleware flow',
            'metadata' => ['source' => 'laravel-runtime-adapter'],
        ]);

        return response()->json($result);
    });
});

app(\PointerDev\AIChat\Auth\AIChatRuntimeSessionManager::class)
    ->revokeForRequest(request());
bash
php artisan vendor:publish --tag=ai-chat-config