去评论
dz插件网

开一帖:纯php搭建DeepSeek教程

镖师
2025/02/11 16:59:04
最近老坛们都在讨论DeepSeek。也看到有些大佬非要把简单的搞那么复杂,还搞个什么桌面端的就出来了。只能说这个对站长没有任何用处。谁会安装个CURL别人的加个壳的呢?其实最好的方法还是去他们官网上面提问聊天。毕竟免费的。如果实在要搬到自己的网站上面。官方也是有提供API地址的。(收费的居然也想CURL他的接口,充分说明是用来搞火车头采集的)。

若还有老坛们不是奔着采集的,那小编也可以简单写个php的CURL。分为流式的(打字机般输出体验号)与非流式的(便于火车采集)。下面上干货:
  1. <?php$apikey =$add_deepseekkey;//到deepseek申请$enwse=$_GET['moshi'];if($enwse==='sse'){ //流式输出接口的$postData = ["model"=>'deepseek-chat',"stream"=>true,"max_tokens"=>4000];}else{$postData = ["model"=>'deepseek-chat',"max_tokens"=>4000];        }$context = json_decode($_POST['context']?: "[]", true) ?: [];if (!empty($context)) {$context = array_slice($context, -5);foreach ($context as $message) {$postData['messages'][] = ['role' => 'user', 'content' => str_replace("\n", "\\n", $message[0])];$postData['messages'][] = ['role' => 'assistant', 'content' => str_replace("\n", "\\n", $message[1])];}}$postData['messages'][] = ['role' => 'user', 'content' =>$_POST['message']];$postData = json_encode($postData);$deepseekapiurl="https://api.deepseek.com/v1/chat/completions";//deepseek接口地址if($enwse==='sse'){ //流式输出接口header('Access-Control-Allow-Origin: *');header("Content-Type: text/event-stream");ini_set('output_buffering', 'off');ini_set('zlib.output_compression', false);while (@ob_end_flush()) {}header('Cache-Control: no-cache');header('Connection: keep-alive');header('X-Accel-Buffering: no');setcookie("errcode", ""); setcookie("errmsg", "");$ch = curl_init();$headers= ['Content-Type: application/json','Authorization: Bearer ' . $apikey];$callback = function ($ch, $data) {$complete = json_decode($data);if (isset($complete->error)) {setcookie("errcode", $complete->error->type);setcookie("errmsg", $complete->error->message);        echo "event: close".PHP_EOL;echo "data: Connection closed".PHP_EOL . PHP_EOL;ob_flush();flush();}echo $data;        ob_flush();flush();        return strlen($data);};curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);curl_setopt($ch, CURLOPT_URL, $deepseekapiurl);curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($ch, CURLOPT_WRITEFUNCTION, $callback);curl_exec($ch);curl_close($ch);}else{//非流式$ch = curl_init();        $headers= ['Content-Type: application/json','Authorization: Bearer ' . $apikey];curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);curl_setopt($ch, CURLOPT_URL, $deepseekapiurl);curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);$response=curl_exec($ch);$response=json_decode($response, true);$result = array('code'=> 200,'asktext'=>str_replace( "\\n", "\n", $response['choices'][0]['message']['content']),'time'=>date('Y-m-d H:i:s'),);echo json_encode($result, JSON_UNESCAPED_UNICODE);exit(); }
代码没有对用户输入的 $_GET['moshi']、$_POST['context'] 和 $_POST['message'] 进行充分的验证和过滤,可能会导致安全问题。建议添加输入验证逻辑,确保输入数据的合法性。