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()
]