PHP code example of hojabbr / laravel-cliproxy

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

    

hojabbr / laravel-cliproxy example snippets


use App\Ai\Agents\SupportAgent;

$response = (new SupportAgent)->prompt('Summarise this ticket.', provider: 'cliproxy', model: 'my-alias');

'providers' => [
    'cliproxy' => [
        'driver' => 'cliproxy',
        'protocol' => 'chat-completions', // or 'messages'
        'models' => [
            'text' => ['default' => 'my-alias', 'cheapest' => 'my-fast-alias'],
            'image' => ['default' => 'my-art-alias'],
        ],
    ],
],

use Hojabbr\CliProxy\Ai\Gateways\MessagesGateway;

public function providerOptions(Lab|string $provider): array
{
    return [MessagesGateway::CACHE_TRAILING_MESSAGE => true];
}

use Laravel\Ai\Ai;

$image = Ai::imageProvider('cliproxy')->image('A lighthouse at dusk', model: 'my-art-alias', size: '16:9', quality: 'high');

$agent->prompt($question, provider: ['cliproxy' => 'my-alias', 'backup' => 'its-model']); // any laravel/ai provider

// config/cliproxy.php
'routes' => [
    'chat' => [
        'enabled' => env('CLIPROXY_CHAT', false),
        'model' => 'my-alias',
        'fallback' => ['backup' => 'its-model'], // any laravel/ai provider
    ],
],

use Hojabbr\CliProxy\Facades\CliProxy;

$route = CliProxy::route('chat');
$agent->prompt($question, provider: $route->providers());

// Or ad hoc, with a switch your application owns:
$route = CliProxy::routeTo('my-alias', ['backup' => 'its-model'], enabled: $settings->gatewayOn);
$route->usesGateway(); // bool

CliProxy::isUp();              // does the gateway answer?
CliProxy::serves('my-alias');  // does it list this model?
CliProxy::models();            // everything it lists
CliProxy::readiness()->forget();

$management = CliProxy::management();

$management->apiKeys();              // client keys the gateway accepts
$management->setApiKeys([...]);     // replace them
$management->credentials();          // upstream credentials and their status
$management->quotaProviders();
$management->quota($authIndex);      // one credential's quota windows, fetched now
$management->config();
$management->request()->get('...');  // anything else under /v0/management

$batch = CliProxy::batches()->create([
    ['custom_id' => 'ticket-1', 'params' => ['model' => 'my-alias', 'messages' => [['role' => 'user', 'content' => '...']]]],
    ['custom_id' => 'ticket-2', 'params' => ['model' => 'my-alias', 'messages' => [['role' => 'user', 'content' => '...']]]],
]);

$status = CliProxy::batches()->retrieve($batch->id);  // processingStatus, requestCounts
foreach (CliProxy::batches()->results($batch->id) as $result) {
    $result->customId; $result->type; $result->text(); $result->message; $result->error;
}
CliProxy::batches()->cancel($batch->id);
CliProxy::batches()->list();

use Hojabbr\CliProxy\Facades\CliProxy;

$gateway = CliProxy::fake(['my-alias']);   // readiness from a fixed list
$gateway->down();                          // ...then watch your routes fall back
bash
php artisan vendor:publish --tag=cliproxy-config
php artisan vendor:publish --tag=cliproxy-migrations
php artisan migrate