提交
This commit is contained in:
parent
14d114771b
commit
2c0293d3b1
@ -89,6 +89,12 @@ class Question extends BaseModel
|
||||
$deepseek_local_url = config("site.deepseek_local_url");
|
||||
//得到切换开关
|
||||
$deepseek_switch = config("site.deepseek_switch");
|
||||
|
||||
//请求池
|
||||
$deepseek_pool = config("site.deepseek_pool");
|
||||
//重置下标
|
||||
$deepseek_pool = array_values($deepseek_pool);
|
||||
|
||||
$reasoning =true;
|
||||
$last = end($messages);
|
||||
if ($last['role'] == 'assistant') {
|
||||
@ -97,6 +103,19 @@ class Question extends BaseModel
|
||||
throw new \Exception('messages参数错误!');
|
||||
}
|
||||
}
|
||||
//默认截断下标为末尾
|
||||
$cut = count($messages);
|
||||
//遍历素$messages,如果其中有一项assistant的content为空,则从这一项assistant在内截断不要了
|
||||
foreach ($messages as $keys => $message){
|
||||
if ($message['role'] == 'assistant' && !$message['content']) {
|
||||
$cut = $keys;
|
||||
break;
|
||||
}
|
||||
}
|
||||
$messages = array_slice($messages, 0, $cut);
|
||||
|
||||
|
||||
|
||||
|
||||
//访问deepseek脚本存在超时可能会超时,所以需要设置超时时间为10分钟
|
||||
set_time_limit(6000);
|
||||
@ -110,7 +129,7 @@ class Question extends BaseModel
|
||||
//"temperature":0.6
|
||||
//}
|
||||
$data = [
|
||||
'model' => $deepseek_switch ? 'qwen':'deepseek-reasoner',
|
||||
'model' => $deepseek_switch ? 'deepseek-r1':'deepseek-reasoner',
|
||||
'messages' => $messages,
|
||||
'stream' => true,
|
||||
'temperature' => 0.6,
|
||||
@ -141,34 +160,79 @@ class Question extends BaseModel
|
||||
|
||||
|
||||
// 判断逻辑
|
||||
$url = $deepseek_switch ? $deepseek_local_url : $deepseek_web_url;
|
||||
if($deepseek_switch) {
|
||||
//本地逻辑
|
||||
//得到池里的链接数量
|
||||
$pool_count = count($deepseek_pool);
|
||||
if ($pool_count == 0) {
|
||||
throw new \Exception('链接池为空!');
|
||||
}
|
||||
// //随机获取一个链接
|
||||
// $pool_index = array_rand($deepseek_pool);
|
||||
// $url = $deepseek_pool[$pool_index];
|
||||
// $url = $url .$deepseek_local_url;
|
||||
//通过cache计数当前请求数
|
||||
$pool_index = cache('deepseek_pool_index') ?? 0;
|
||||
$pool_index = $pool_index % $pool_count;
|
||||
// var_dump($pool_index);
|
||||
cache('deepseek_pool_index', $pool_index + 1);
|
||||
$url = $deepseek_pool[$pool_index] . $deepseek_local_url;
|
||||
|
||||
|
||||
}else{
|
||||
//远程逻辑
|
||||
$url = $deepseek_web_url;
|
||||
}
|
||||
|
||||
|
||||
// $url = $deepseek_switch ? $deepseek_local_url : $deepseek_web_url;
|
||||
$headers = $deepseek_switch ? [] : ['Authorization:' . $deepseek_key];
|
||||
|
||||
|
||||
// file_put_contents("test.txt",$messages."\r\n",FILE_APPEND);
|
||||
// 发送curl post json格式的 stream式请求
|
||||
$ch = curl_init();
|
||||
curl_setopt($ch, CURLOPT_URL, $url);
|
||||
//设置等待=时间无限
|
||||
curl_setopt($ch, CURLOPT_TIMEOUT, 0);
|
||||
curl_setopt($ch, CURLOPT_POST, 1);
|
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
|
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, array_merge(['Content-Type: application/json'], $headers));
|
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
|
||||
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) use (&$response,$question_id,$deepseek_switch,&$reasoning,$session_key) {
|
||||
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function ($ch, $data) use (&$response,$question_id,$deepseek_switch,&$reasoning,$session_key,$url) {
|
||||
// var_dump($data);
|
||||
file_put_contents("test.txt",$url.$data."\r\n",FILE_APPEND);
|
||||
// if (!$data) {
|
||||
// return 0; // 结束流式请求
|
||||
// }
|
||||
|
||||
if (!$data) {
|
||||
return 0; // 结束流式请求
|
||||
}
|
||||
|
||||
// var_dump($data);
|
||||
$data = htmlspecialchars_decode($data);
|
||||
// 处理每一部分数据
|
||||
// $lines = explode("\n", trim($data));
|
||||
$lines = explode("\n",$data);
|
||||
|
||||
if(!$lines){
|
||||
throw new \Exception($data);
|
||||
}
|
||||
|
||||
|
||||
foreach ($lines as $line) {
|
||||
|
||||
if (strpos($line, "data: ") === 0) {
|
||||
|
||||
$json = substr($line, 6);
|
||||
$chunk = json_decode($json, true);
|
||||
|
||||
if(isset($chunk['error']) && $chunk['error']){
|
||||
throw new \Exception($chunk['error']);
|
||||
}
|
||||
|
||||
// if(!isset($chunk['choices'][0]['delta']['reasoning_content']) && !isset($chunk['choices'][0]['delta']['content'])){
|
||||
// throw new \Exception($data);
|
||||
// }
|
||||
|
||||
|
||||
|
||||
if (isset($chunk['choices'][0]['delta']['reasoning_content'])) {
|
||||
$chunk['choices'][0]['delta']['reasoning_content'] = htmlspecialchars_decode($chunk['choices'][0]['delta']['reasoning_content']);
|
||||
@ -186,7 +250,7 @@ class Question extends BaseModel
|
||||
//本地部署</think>为结尾
|
||||
if(strpos($chunk['choices'][0]['delta']['content'],'</think>')!==false){
|
||||
$reasoning = false;
|
||||
}else{
|
||||
}elseif(strpos($chunk['choices'][0]['delta']['content'],'<think>')===false){
|
||||
$this->saveStreamData($chunk['choices'][0]['delta']['content'],$chunk,$question_id,$reasoning,$session_key);
|
||||
}
|
||||
|
||||
@ -202,6 +266,16 @@ class Question extends BaseModel
|
||||
}
|
||||
|
||||
}
|
||||
}else{
|
||||
// throw new \Exception($data);
|
||||
$chunk = json_decode($line, true);
|
||||
|
||||
if($chunk && isset($chunk['error']) && $chunk['error']){
|
||||
throw new \Exception($chunk['error']);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user