Files

1 line
3.7 KiB
JSON

{"repo": "4len-png/diplom", "files": {"classes/DeepseekChatService.php": {"c4a534c754a4d39c3c0684f02b0825439cd4a7b0": {"t": 1785746157.6024797, "n": 3361, "h": "352bc0ba27a68cf2", "c": "<?php\n\nnamespace services;\n\nclass DeepseekChatService\n{\n private $apiKey;\n private $apiUrl = 'https://api.deepseek.com/chat/completions';\n private $chatHistory = []; // Массив для хранения истории сообщений\n\n public function __construct($apiKey)\n {\n $this->apiKey = $apiKey;\n }\n\n /**\n * Добавляет сообщение в историю чата.\n *\n * @param string $role Роль отправителя (system, user, assistant).\n * @param string $content Текст сообщения.\n */\n public function addMessage($role, $content)\n {\n $this->chatHistory[] = [\n 'role' => $role,\n 'content' => $content,\n ];\n }\n\n /**\n * Очищает историю чата.\n */\n public function clearHistory()\n {\n $this->chatHistory = [];\n }\n\n\n public function sendChat($model = 'deepseek-chat', $stream = false)\n {\n $data = [\n 'model' => $model,\n 'messages' => $this->chatHistory,\n 'stream' => $stream,\n ];\n\n $headers = [\n 'Content-Type: application/json',\n 'Authorization: Bearer ' . $this->apiKey,\n ];\n\n return $this->sendCurlRequest($this->apiUrl, $headers, $data);\n }\n\n\n private function sendCurlRequest($url, $headers = [], $data = [], $method = 'POST')\n {\n $ch = curl_init($url);\n curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);\n curl_setopt($ch, CURLOPT_TIMEOUT, 600);\n curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 600);\n\n if (!empty($data)) {\n curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));\n }\n\n if (!empty($headers)) {\n curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\n }\n\n do {\n $response = curl_exec($ch);\n if (curl_errno($ch)) {\n throw new Exception('Curl error: ' . curl_error($ch));\n }\n } while (trim($response) === '');\n\n curl_close($ch);\n return json_decode($response, true);\n }\n\n\n public function sendMessage($messages, $model = 'deepseek-chat', $stream = false)\n {\n $data = [\n 'model' => $model,\n 'messages' => $messages,\n 'stream' => $stream,\n ];\n\n $headers = [\n 'Content-Type: application/json',\n 'Authorization: Bearer ' . $this->apiKey,\n ];\n\n return $this->sendCurlRequest($this->apiUrl, $headers, $data);\n }\n public function sendMessageStream($messages, $model = 'deepseek-chat')\n {\n $data = [\n 'model' => $model,\n 'messages' => $messages,\n 'stream' => true,\n ];\n\n $headers = [\n 'Content-Type: application/json',\n 'Authorization: Bearer ' . $this->apiKey,\n ];\n\n $ch = curl_init($this->apiUrl);\n curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);\n curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\n curl_setopt($ch, CURLOPT_POST, true);\n curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));\n curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $chunk) {\n echo $chunk;\n flush();\n return strlen($chunk);\n });\n\n ob_implicit_flush(true);\n curl_exec($ch);\n curl_close($ch);\n }\n}\n"}}}}