Files

1 line
4.6 KiB
JSON

{"repo": "bamideleo/superai", "files": {"Modules/DeepSeek/Traits/DeepSeekApiTrait.php": {"26a9589df9f5f078fe023123afd6bf17d7fad933": {"t": 1785746161.929476, "n": 4343, "h": "ea05927e3ad63724", "c": "<?php\n\nnamespace Modules\\DeepSeek\\Traits;\n\nuse GuzzleHttp\\Client;\nuse Illuminate\\Http\\Response;\n\nuse Modules\\DeepSeek\\Responses\\StreamResponse;\n\ntrait DeepSeekApiTrait\n{\n /**\n * The URL for the DeepSeek API messages endpoint.\n */\n private $url = 'https://api.deepseek.com/';\n\n public function aiKey()\n {\n $apiKey = config('aiKeys.DEEPSEEK.API_KEY');\n\n if (empty($apiKey)) {\n throw new \\Exception(__(\"There's an issue with the :x API key. Kindly reach out to the administration for assistance.\", ['x' => __('DeepSeek')]), Response::HTTP_UNAUTHORIZED);\n }\n\n return $apiKey;\n }\n\n /**\n * Method to send a chat message using cURL to the Anthropic API.\n *\n * @return array|null The response from the API in JSON format, or null if an error occurred.\n */\n public function chat()\n {\n $curl = curl_init();\n\n // Set cURL options\n curl_setopt_array($curl, array(\n CURLOPT_URL => $this->url . 'chat/completions',\n CURLOPT_RETURNTRANSFER => true,\n CURLOPT_ENCODING => \"\",\n CURLOPT_MAXREDIRS => 10,\n CURLOPT_TIMEOUT => 60,\n CURLOPT_CONNECTTIMEOUT => 10, // Limit connection time\n CURLOPT_SSL_VERIFYHOST => config('openAI.ssl_verify_host'),\n CURLOPT_SSL_VERIFYPEER => config('openAI.ssl_verify_peer'),\n CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,\n CURLOPT_CUSTOMREQUEST => \"POST\",\n CURLOPT_POSTFIELDS => json_encode($this->processedData),\n CURLOPT_HTTPHEADER => array(\n \"Content-Type: application/json\",\n \"Authorization: Bearer \" . $this->aiKey()\n ),\n ));\n\n // Make API request\n $response = curl_exec($curl);\n $err = curl_error($curl);\n\n $curlStatusCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);\n\n // Close cURL session\n curl_close($curl);\n $response = !empty($response) ? $response : $err;\n\n return [\n 'code' => $curlStatusCode,\n 'body' => json_decode($response, true), \n ];\n }\n\n /**\n * Method to repeatedly call the 'chat' method a specified number of times and store the responses in an array.\n *\n * @param int $outlineNumber The number of times to call the 'chat' method.\n * @return array An array containing the responses from calling the 'chat' method multiple times.\n */\n public function outlineChat(int $outlineNumber): array\n {\n $response = [];\n for ($i= 0; $i < $outlineNumber; $i++) {\n $response[] = $this->chat();\n }\n\n return $response;\n \n }\n\n /**\n * Method to send a chat message using GuzzleHttp\\Client to the Anthropic API.\n *\n * @return StreamResponse|null The response from the API wrapped in a StreamResponse object, or null if an error occurred.\n */\n public function chatStream()\n {\n $client = new Client();\n try {\n // Send the POST request\n $response = $client->request('POST', $this->url . 'chat/completions' , [\n 'json' => $this->processedData,\n 'headers' => [\n 'Content-Type' => 'application/json',\n 'Accept' => 'application/json',\n 'Authorization' => 'Bearer ' . $this->aiKey()\n ],\n 'stream' => true\n ]);\n\n return new StreamResponse($response);\n\n } catch (\\GuzzleHttp\\Exception\\RequestException $e) {\n // If an error occurs during the request, catch the exception and handle it\n if (strpos($e->getMessage(), '401 Unauthorized') !== false) {\n throw new \\Exception(__(\"There's an issue with your API key.\"));\n }\n\n $response = $e->getResponse();\n\n if ($response) {\n $body = $response->getBody()->getContents();\n $errorData = json_decode($body, true);\n\n if (isset($errorData['error']['message'])) {\n throw new \\Exception($errorData['error']['message']);\n }\n throw new \\Exception($errorData['error']);\n\n } else {\n throw new \\Exception($e->getMessage());\n }\n }\n }\n}"}}}}