PHP code example of solicode / deepseek-php-client

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

    

solicode / deepseek-php-client example snippets




oliCode\DeepSeekClient;

try {
    // Initialize client
    $client = new DeepSeekClient('your-api-key');

    // List available models
    $models = $client->models();
    echo "<pre>Available models:\n";
    print_r($models['data']);
    echo "</pre>";

    // Generate chat completion
    $response = $client->chat(
        model: 'deepseek-chat',
        messages: [
            ['role' => 'user', 'content' => 'Explain quantum entanglement simply']
        ]
    );

    echo "<pre>\nResponse:\n" . htmlspecialchars($response['choices'][0]['message']['content']) . "</pre>";

} catch (InsufficientBalanceException $e) {
    die("<pre>Error: Add funds to your DeepSeek account</pre>");
} catch (\Exception $e) {
    die("<pre>Error: " . htmlspecialchars($e->getMessage()) . "</pre>");
}


$client = new DeepSeekClient(
    apiKey: 'your-key',
    baseUrl: 'https://api.deepseek.com/v1', // Custom endpoint
    timeout: 15, // Seconds
    headers: ['X-Custom-Header' => 'value']
);

$stream = $client->chat(
    'deepseek-chat',
    [['role' => 'user', 'content' => 'Explain recursion']],
    ['stream' => true]
);

foreach ($stream as $chunk) {
    echo $chunk['choices'][0]['delta']['content'] ?? '';
}

try {
    // API operations
} catch (InsufficientBalanceException $e) {
    // Handle payment issues (402 errors)
} catch (InvalidRequestException $e) {
    // Handle bad parameters (400 errors)
} catch (AuthenticationException $e) {
    // Handle invalid API keys (401 errors)
} catch (APIConnectionException $e) {
    // Handle network issues
} catch (\Exception $e) {
    // Generic fallback
}
bash
composer