PHP code example of jpcaparas / deepseek-php-client

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

    

jpcaparas / deepseek-php-client example snippets




use DeepSeek\DeepSeekClient;

$client = new DeepSeekClient('your-api-key');

$response = $client
    ->setMessage('user', 'What is 2+2?')
    ->send();

print_r($response);

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

// Add a message to the conversation
$client->setMessage('system', 'You are a helpful assistant');
$client->setMessage('user', 'Hello'); // Returns self for chaining

// Clear conversation history
$client->clearMessages(); // Returns self for chaining

// Send conversation to API (returns array)
$response = $client->send();

[
    'choices' => [
        ['message' => ['content' => 'Response text here']]
    ]
]

[
    "id" => "d7b42b8b-5007-42c6-b607-7b3b7b7b7b7b",
    "object" => "chat.completion",
    "created" => 1735810490,
    "model" => "deepseek-chat",
    "choices" => [
      [
        "index" => 0,
        "message" => [
          "role" => "assistant",
          "content" => "Hello! How can I assist you today? 😊",
        ],
        "logprobs" => null,
        "finish_reason" => "stop",
      ],
    ],
    "usage" => [
      "prompt_tokens" => 9,
      "completion_tokens" => 11,
      "total_tokens" => 20,
      "prompt_cache_hit_tokens" => 0,
      "prompt_cache_miss_tokens" => 9,
    ],
    "system_fingerprint" => "d7b42b8b-5007-42c6-b607-7b3b7b7b7b7b",
  ]
  

try {
    $response = $client
        ->setMessage('user', 'Hello')
        ->send();
} catch (\Exception $e) {
    echo $e->getMessage();
}
bash
composer