commit
1b935a872e
@ -0,0 +1,87 @@ |
||||
<?php |
||||
/* For license terms, see /license.txt */ |
||||
|
||||
require_once 'Url.php'; |
||||
|
||||
class DeepSeek |
||||
{ |
||||
private $apiKey; |
||||
private $headers; |
||||
|
||||
public function __construct(string $apiKey) |
||||
{ |
||||
$this->apiKey = $apiKey; |
||||
$this->headers = [ |
||||
'Content-Type: application/json', |
||||
"Authorization: Bearer {$this->apiKey}", |
||||
]; |
||||
} |
||||
|
||||
/** |
||||
* Generate questions using the DeepSeek API. |
||||
* |
||||
* @param array $payload Data to send to the API |
||||
* @return string Decoded response from the API |
||||
* @throws Exception If an error occurs during the request |
||||
*/ |
||||
public function generateQuestions(array $payload): string |
||||
{ |
||||
$url = Url::completionsUrl(); |
||||
$response = $this->sendRequest($url, 'POST', $payload); |
||||
|
||||
if (empty($response)) { |
||||
throw new Exception('The DeepSeek API returned no response.'); |
||||
} |
||||
|
||||
$result = json_decode($response, true); |
||||
|
||||
// Validate errors returned by the API |
||||
if (isset($result['error'])) { |
||||
throw new Exception("DeepSeek API Error: {$result['error']['message']}"); |
||||
} |
||||
|
||||
// Ensure the response contains the expected "choices" field |
||||
if (!isset($result['choices'][0]['message']['content'])) { |
||||
throw new Exception('Unexpected response format from the DeepSeek API.'); |
||||
} |
||||
|
||||
return $result['choices'][0]['message']['content']; |
||||
} |
||||
|
||||
/** |
||||
* Send a request to the DeepSeek API. |
||||
* |
||||
* @param string $url Endpoint to send the request to |
||||
* @param string $method HTTP method (e.g., GET, POST) |
||||
* @param array $data Data to send as JSON |
||||
* @return string Raw response from the API |
||||
* @throws Exception If a cURL error occurs |
||||
*/ |
||||
private function sendRequest(string $url, string $method, array $data = []): string |
||||
{ |
||||
$ch = curl_init($url); |
||||
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); |
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers); |
||||
|
||||
$response = curl_exec($ch); |
||||
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
||||
|
||||
if (curl_errno($ch)) { |
||||
$errorMessage = curl_error($ch); |
||||
curl_close($ch); |
||||
throw new Exception("cURL Error: {$errorMessage}"); |
||||
} |
||||
|
||||
curl_close($ch); |
||||
|
||||
// Validate HTTP status codes |
||||
if ($httpCode < 200 || $httpCode >= 300) { |
||||
throw new Exception("Request to DeepSeek failed with HTTP status code: {$httpCode}"); |
||||
} |
||||
|
||||
return $response; |
||||
} |
||||
} |
@ -0,0 +1,17 @@ |
||||
<?php |
||||
/* For license terms, see /license.txt */ |
||||
|
||||
class Url |
||||
{ |
||||
private const BASE_URL = 'https://api.deepseek.com/chat'; |
||||
|
||||
/** |
||||
* Get the endpoint URL for chat completions. |
||||
* |
||||
* @return string URL for the chat completions endpoint |
||||
*/ |
||||
public static function completionsUrl(): string |
||||
{ |
||||
return self::BASE_URL . '/completions'; |
||||
} |
||||
} |
Loading…
Reference in new issue