PHP code example of octopus-llm / php

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

    

octopus-llm / php example snippets




use OctopusLLM\Gateway\OctopusLLM;

// Store your keys in .env file:
// GROQ_KEYS=gsk_key1,gsk_key2,gsk_key3
// OPENROUTER_KEYS=sk-or-key1,sk-or-key2

// Initialize the Gateway config
$llm = new OctopusLLM([
    'providers' => [
        [
            'id' => 'groq',
            'baseURL' => 'https://api.groq.com/openai/v1',
            'model' => 'llama3-70b-8192',
            'priority' => 1,
            'keys' => explode(',', $_ENV['GROQ_KEYS'] ?? ''),
            'cooldown' => 60
        ],
        [
            'id' => 'openrouter',
            'baseURL' => 'https://openrouter.ai/api/v1',
            'model' => 'meta-llama/llama-3-70b-instruct',
            'priority' => 2,
            'keys' => explode(',', $_ENV['OPENROUTER_KEYS'] ?? ''),
            'cooldown' => 120
        ]
    ]
]);

// Use it like a regular OpenAI Client
try {
    $response = $llm->chat([
        ['role' => 'system', 'content' => 'You are a helpful assistant.'],
        ['role' => 'user', 'content' => 'Explain PHP decorators.']
    ]);

    echo $response->content;
    echo "Served by: " . $response->provider;
    
} catch (\OctopusLLM\Gateway\Exceptions\GatewayExhaustedException $e) {
    echo "All providers and keys failed!";
}

[
    // Array of AI Providers
    'providers' => [
        [
            'id' => 'provider1',            // string: Unique identifier
            'baseURL' => 'https://...',     // string: Provider base API URL
            'model' => 'model-name',        // string: Targeted internal model
            'priority' => 1,                // int: Lower number = higher priority
            'keys' => ['key1', 'key2'],     // array: API keys for this provider
            'extraHeaders' => [             // array: Extra HTTP headers (optional)
                'HTTP-Referer' => '...'
            ],
            'cooldown' => 60                // int: Circuit breaker recovery timeout (optional, default: 60)
        ]
    ],
    
    // Gateway security guards (optional)
    'guard' => [
        'timeoutSeconds' => 10,     // Network request timeout (default: 10)
        'maxInputTokens' => 4000,   // Guard max input tokens (approximated)
        'maxRetries' => 2,          // Retries per key on 5xx errors (default: 2)
        'maxOutputTokens' => 1000   // Global max generation tokens (default: 1000)
    ],
    
    // Recovery script configurations (optional)
    'recovery' => [
        'pingTimeout' => 5         // Timeout for `runRecovery()` pings in seconds
    ],
    
    // Failure thresholds (optional)
    'circuitBreaker' => [
        'failureThreshold' => 3    // Number of failures before marking key as inactive
    ],
    
    // Custom state storage (optional, default: JsonFileStorage)
    'storage' => new CustomStorageImpl() 
]

$llm->chat($messages, [
    'stream' => true,
    'onChunk' => function(string $chunk) {
        echo $chunk; // Directly stream into stdout
        flush();
    }
]);

public static function octopus(bool $getShared = true): \OctopusLLM\Gateway\OctopusLLM
{
    if ($getShared) {
        return static::getSharedInstance('octopus');
    }

    return new \OctopusLLM\Gateway\OctopusLLM([
        'providers' => [
            [
                'id'       => 'groq',
                'baseURL'  => 'https://api.groq.com/openai/v1',
                'model'    => 'llama-3.1-8b-instant',
                'keys'     => explode(',', env('GROQ_KEYS', '')),
                'priority' => 1,
                'cooldown' => 60,
            ],
        ],
    ]);
}



namespace App\Commands;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;

class OctopusRecover extends BaseCommand
{
    protected $group       = 'Octopus';
    protected $name        = 'octopus:recover';
    protected $description = 'Ping all inactive keys and reactivate if healthy.';

    public function run(array $params)
    {
        $octopus = \Config\Services::octopus();
        $report  = $octopus->runRecovery();

        CLI::write('Recovery complete.', 'green');
        CLI::write('Total pinged : ' . $report->total);
        CLI::write('Recovered    : ' . count($report->recovered));
        CLI::write('Still failed : ' . count($report->failed));
    }
}

$response = \Config\Services::octopus()->chat([
    ['role' => 'system', 'content' => 'You are a helpful assistant.'],
    ['role' => 'user',   'content' => $userMessage],
]);
echo $response->content;
bash
composer