code) != 5){ echo "event: verify\ndata: $information\n\n"; exit; } //获取最终的核心参数参与模型输入 $modelId = $informationObject->modelId; $content = $informationObject->input; $contentmcp = $informationObject->input; $ragId = $informationObject->ragId; // if(is_array($content)){ // $content = json_encode($content,JSON_UNESCAPED_UNICODE); // } // 模型定制开始------------------------------------------------------------------- // //emoon-E1-13B线上 // if($modelId == 9){ // $apiKey = 'sk-800990e9cdb34613a2133f0fc333a7ff'; // 请替换为你的实际 API 密钥 // $url = 'https://api.deepseek.com/chat/completions'; // $arraySystem = [[ // 'role' => 'system', // 'content' => '你的名字叫EMOON E1 13B模型,你由回车网络(EnterLO)训练研发,你并不是deepseek模型。你是一名三甲医院主治医师级别的AI助手,最擅长医疗专业知识的解读和分析,遵循《中国临床诊疗指南》和NCCN指南。必须:1) 先询问关键症状细节 2) 标注证据等级 3) 拒绝非循证医学建议' // ]]; // // $arrayContent = json_decode($content); // $arrayMessage = array_merge($arraySystem,$content); // // 请求数据 // $data = [ // 'model' => 'deepseek-chat', // 'messages' => $arrayMessage, // 'stream' => true // ]; // // 设置请求头 // $headers = [ // 'Content-Type: application/json', // 'Authorization: Bearer ' . $apiKey // ]; // } if($ragId != ''){ $content = stdClassObjToArray($content); $dataRag['input'] = $content[count($content)-1]['content']; $dataRag['kid'] = $ragId; $urlRAG = 'http://192.168.10.115:3338/apis/ragData'; $ragResult = curlPost($urlRAG,$dataRag); $ragResult = json_decode($ragResult); $ragResult = stdClassObjToArray($ragResult); // if( $ragResult['code'] == 200){ // $ragInfomation = json_encode($ragResult['nearest']); // } if(json_encode($ragResult['nearest'],JSON_UNESCAPED_UNICODE) != "[]" ){ if($ragId == "1947484295610253313" || $ragId == "1949641433623703553"){ $content[count($content)-1]['content'] = '我的提问是:“'.$content[count($content)-1]['content'].'”,下面是基于我的提问查到的知识库相关内容:“'.json_encode($ragResult['nearest'],JSON_UNESCAPED_UNICODE).'”,请按照查到的知识库相关内容,进行病历质控,根据知识库相关内容条件生成一个包含规则,根据规则数、扣除分数和原因列表。输出结果需要以 JSON 格式呈现,例如: 规则 1:主诉症状明确性(3分)标准与细则:必须描述具体症状或体征(如“头痛”,“发热”),禁用诊断名称(如“糖尿病”)。扣分细则:若主诉中使用了诊断名称或模糊描述,扣3分,反之不扣分。 规则 2:主诉时间准确性(2分)标准与细则:用阿拉伯数字+单位(如“3天”),禁用“三天”“数月”等模糊表述。扣分细则:若主诉时间使用模糊表述(如“三天”“数月”),扣2分,反之不扣分。 返回的数据应按照以下格式组织: [ { "规则": "规则1", "扣除分数": 3, "原因": "原因详细说明" }, { "规则": "规则2", "扣除分数": 2, "原因": "原因详细说明" } ] 只按照格式输出,不要输出其他内容';//请求向量数据库接口反参组装 $contentmcp = $content; } else{ $content[count($content)-1]['content'] = '我的提问是:“'.$content[count($content)-1]['content'].'”,下面是基于我的提问查到的知识库相关内容:“'.json_encode($ragResult['nearest'],JSON_UNESCAPED_UNICODE).'”。';//请求向量数据库接口反参组装 $contentmcp = $content; } } } // $content = stdClassObjToArray($content); // echo(json_encode($ragResult,JSON_UNESCAPED_UNICODE)); // exit; if($thinkid != ''){ $urlgetreply = $urlCommon.'/apis/getreply'; $reply = curlPost($urlgetreply,$dataReply); $content = stdClassObjToArray($content); $content[count($content)-1]['content'] = '我的提问是:“'.$content[count($content)-1]['content'].'”,下面是基于我的提问得到的回答:“'.$reply.'”,现在请根据回答生成一个思考过程,严格遵守以下要求:只回复思考过程,不输出其他内容。';//请求向量数据库接口反参组装 } // echo(json_encode($content,JSON_UNESCAPED_UNICODE)); // exit; //deepseek-R1-70B线下 if($modelId == 12){ $url = 'http://192.168.10.115:19997/v1/chat/completions'; $headers = [ 'accept: application/json', 'Content-Type: application/json' ]; $arraySystem = [[ 'role' => 'system', 'content' => '你是一个人工智能助手。' ], ]; //$arrayContent = json_decode($content, true); $arrayMessage = array_merge($arraySystem, $content); $data = [ "messages" => $arrayMessage, "model" => "qwen3-32B", //X "stream" => true, "max_tokens" => 8192, "chat_template_kwargs" => '{"enable_thinking": false}', ]; } // echo(json_encode($data,JSON_UNESCAPED_UNICODE)); // exit; // 模型定制结束------------------------------------------------------------------- if($mcp == 0 || $mcp ==""){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data,JSON_UNESCAPED_UNICODE)); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt_array($ch, [ CURLOPT_TIMEOUT => 60, // 超时时间延长至60秒 CURLOPT_TCP_KEEPALIVE => 1, // 启用TCP保活 CURLOPT_TCP_KEEPIDLE => 10, CURLOPT_TCP_KEEPINTVL => 5 ]); //创建一个空字符串动态存储回复的文字 $reply = ''; // 添加连接状态检查到写入回调 curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$reply,$thinkid,$zhikong) { // 如果客户端已断开,立即终止 if (connection_aborted()) { return -1; // 返回-1会强制CURL中断请求 curl_close($ch); } $lines = explode("\n", $data); foreach ($lines as $line) { if (strpos($line, 'data:') === 0) { $contents = trim(substr($line, 5)); if ($contents === '[DONE]') { $output = "event: end\ndata: {\"status\":\"done\"}\n\n"; echo $output; // $reply .= $output; // 捕获输出内容 //完成输出后触发全部回复存储进入数据库 global $token; global $urlCommon; $dataReply['token'] = $token; $dataReply['reply'] = $reply; //将数据发送至数据库 $curlReplay = curl_init(); curl_setopt($curlReplay, CURLOPT_URL, $urlCommon.'/Apis/reply'); curl_setopt($curlReplay, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curlReplay, CURLOPT_SSL_VERIFYHOST, FALSE); // curl_setopt($curl, CURLOPT_HTTPHEADER,$headers); if ($json) { curl_setopt($curlReplay,CURLOPT_HTTPHEADER,[ "Content-Type: application/json" ]); curl_setopt($curlReplay, CURLOPT_POST, TRUE); curl_setopt($curlReplay,CURLOPT_POSTFIELDS,json_encode($dataReply)); }else { if (!empty($dataReply)) { curl_setopt($curlReplay, CURLOPT_POST, TRUE); curl_setopt($curlReplay, CURLOPT_POSTFIELDS,$dataReply); // curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } } curl_setopt($curlReplay, CURLOPT_RETURNTRANSFER,true); curl_exec($curlReplay); curl_close($curlReplay); } else { // 转发数据前再次检查连接 if (connection_aborted()) { return -1; curl_close($ch); } //构造$line global $modelId; $line = substr($line,6); $line = json_decode($line); $line = stdClassObjToArray($line); if($modelId == 9){ $line['model'] = 'emoon-E1-13B'; } $line = 'data: '.json_encode($line,JSON_UNESCAPED_UNICODE); $contentlines = trim(substr($line, 6)); $jsonline = json_decode($contentlines, true); $jsoncontent = $jsonline['choices'][0]['delta']['content']; if (($jsoncontent == "") || ($jsoncontent == "<")) { $output = 'data: {"id":"chatcmpl-1747186801165865728","object":"chat.completions.chunk","created":1747186801,"model":"merge","choices":[{"index":0,"finish_reason":null,"delta":{"role":"assistant","content":""}}]}'. "\n\n"; // echo $ouputs."\n"; // $output = "event: end\ndata: Stream ended\n\n"; } elseif($jsonline['choices'][0]['finish_reason'] === 'stop'){ // $output = "event: end\ndata: Stream ended\n\n"; if($thinkid == null && $zhikong == 1){ $output = 'data: {"id":"chatcmpl-1747186801165865728","object":"chat.completions.chunk","created":1747186801,"model":"merge","choices":[{"index":0,"finish_reason":null,"delta":{"role":"assistant","content":"ending"}}]}'. "\n\n"; }else{ $output = 'data: {"id":"chatcmpl-1747186801165865728","object":"chat.completions.chunk","created":1747186801,"model":"merge","choices":[{"index":0,"finish_reason":null,"delta":{"role":"assistant","content":""}}]}'. "\n\n"; } //完成输出后触发全部回复存储进入数据库 global $token; global $urlCommon; $dataReply['token'] = $token; $dataReply['reply'] = $reply; //将数据发送至数据库 $curlReplay = curl_init(); curl_setopt($curlReplay, CURLOPT_URL, $urlCommon.'/Apis/reply'); curl_setopt($curlReplay, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curlReplay, CURLOPT_SSL_VERIFYHOST, FALSE); // curl_setopt($curl, CURLOPT_HTTPHEADER,$headers); if ($json) { curl_setopt($curlReplay,CURLOPT_HTTPHEADER,[ "Content-Type: application/json" ]); curl_setopt($curlReplay, CURLOPT_POST, TRUE); curl_setopt($curlReplay,CURLOPT_POSTFIELDS,json_encode($dataReply)); }else { if (!empty($dataReply)) { curl_setopt($curlReplay, CURLOPT_POST, TRUE); curl_setopt($curlReplay, CURLOPT_POSTFIELDS,$dataReply); // curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); } } curl_setopt($curlReplay, CURLOPT_RETURNTRANSFER,true); curl_exec($curlReplay); curl_close($curlReplay); } else{ $output = $line . "\n\n"; } // $output = $line . "\n\n"; echo $output; //将回复数据存储入$reply if($thinkid == null){ $outputJson = substr($output,6); if(($modelId == 9) || ($modelId == 7) || ($modelId == 4) || ($modelId == 10) || ($modelId == 12) || ($modelId == 15)){ $outputJson = json_decode($outputJson); $outputJson = $outputJson->choices[0]->delta->content; if($outputJson){ $outputJson = json_encode($outputJson,JSON_UNESCAPED_UNICODE); $outputJson = str_replace('"', '', $outputJson); } } } $reply .= $outputJson; // 捕获输出内容 } ob_flush(); flush(); } } return strlen($data); }); // 错误处理 curl_setopt($ch, CURLOPT_TIMEOUT, 86400); curl_setopt($ch, CURLOPT_FAILONERROR, true); // 执行请求 try { curl_exec($ch); // 检查HTTP状态码 $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($statusCode !== 200) { echo "event: error\ndata: HTTP Status {$statusCode}\n\n"; ob_flush(); flush(); } } catch (Exception $e) { echo "event: error\ndata: " . $e->getMessage() . "\n\n"; ob_flush(); flush(); } }else{ @ini_set('output_buffering', 'off'); @ini_set('zlib.output_compression', false); while (ob_get_level() > 0) { ob_end_flush(); } ob_implicit_flush(true); // 设置流式响应头 $requestBody = file_get_contents('php://input'); $headers = getallheaders(); $authorizationHeader = isset($headers['Authorization']) ? $headers['Authorization'] : ''; $reply = ''; $RAGdata = [ "username" => "test", "password" => "test123" ]; // 调用时只需传递非 Content-Type/Accept 的 Header $headers = [ 'Cache-Control: no-cache', 'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36' ]; $urlRAG = "http://192.168.10.115:6039/auth/login"; // 替换为实际 URL $id = curlPostRAG($urlRAG, $RAGdata, $headers); $token = $id; // 默认启用 JSON 模式 $token = json_decode($token); $token = stdClassObjToArray($token); $token = $token['data']['token']; $url = "http://192.168.10.115:6039/chat/emoonSend"; // 替换成实际接口地址 $data = [ "messages" => $contentmcp, "model" => "qwen3-32B", //X "temperature" => 0.5, "top_p" => 1, "presence_penalty" => 0, "frequency_penalty" => 0, "kid" => "", "chat_type" => 0, "appId" => "", "agentId" => $mcp, "stream" => true ]; // 初始化 cURL $ch = curl_init(); // 设置 cURL 选项 curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 返回响应数据 curl_setopt($ch, CURLOPT_POST, true); // 使用 POST 方法 curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // 发送 JSON 数据 curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer " . $token, "Content-Type: application/json" // 声明请求体是 JSON ]); $chatId = 'chat' . uniqid() . bin2hex(random_bytes(8)); $created = time(); // for ($i = 0; $i < count($lines); $i++) { // echo $lines[0]; // } $lastContent = ''; curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use ($chatId, $created, &$lastContent,&$reply) { $lines = explode("\n", $data); $prefixLength = strlen("event:chat_completion"); $content = str_replace("event:chat_completion", "", $lines[0]); // foreach ($lines as $line) { // $line = substr($line,6); // $content = $matches[0]; // // 仅当内容变化时才输出 // if ($content !== $lastContent) { // $lastContent = $content; $jsonData = [ "id" => $chatId, "model" => "emoon-E1-13B", "created" => $created, "object" => "chat.completion.chunk", "choices" => [ [ "index" => 0, "delta" => [ "content" => $content ], "finish_reason" => null ] ], "usage" => null ]; echo "data: " . json_encode($jsonData,JSON_UNESCAPED_UNICODE) . "\n\n"; $outputJson = json_encode($jsonData,JSON_UNESCAPED_UNICODE); $outputJson = json_decode($outputJson); // $outputJson = stdClassObjToArray($outputJson); $outputJson = $outputJson->choices[0]->delta->content; if($outputJson){ $outputJson = json_encode($outputJson,JSON_UNESCAPED_UNICODE); $outputJson = str_replace('"', '', $outputJson); } $reply .= $outputJson; // } @ob_flush(); @flush(); // } return strlen($data); }); $res = curl_exec($ch); } // 清理资源 curl_close($ch); // 显式结束流 echo "event: end\ndata: Stream ended\n\n"; ob_flush(); flush(); $dataReply['reply'] = $reply; $url=$urlCommon.'/Apis/reply'; curlPost($url,$dataReply); //模型定制结束------------------------------------------------------------------- function curlPost($url, $data = null, $headers = [], $json = false) { $curl = curl_init($url); $options = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_POST => !empty($data) || $json, ]; if ($json) { $headers = array_filter($headers, function($h) { return stripos($h, 'Content-Type:') === false; }); $headers[] = 'Content-Type: application/json'; $data = json_encode($data, JSON_UNESCAPED_UNICODE); } if (!empty($headers)) { $options[CURLOPT_HTTPHEADER] = $headers; } if (!empty($data)) { $options[CURLOPT_POSTFIELDS] = $data; } curl_setopt_array($curl, $options); $result = curl_exec($curl); curl_close($curl); return $result; } function stdClassObjToArray($array) { if(is_object($array)) { $array = (array)$array; } if(is_array($array)) { foreach($array as $key=>$value) { $array[$key] = stdClassObjToArray($value); } } return $array; } function curlPostRAG($url, $data = null, $headers = [], $json = true) { $curl = curl_init($url); $options = [ CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, CURLOPT_POST => true, // 强制启用 POST(JSON 模式下始终 POST) ]; if ($json) { // 移除已有的 Content-Type 和 Accept 头,避免冲突 $headers = array_filter($headers, function($h) { return stripos($h, 'Content-Type:') === false && stripos($h, 'Accept:') === false; }); // 添加 JSON 专用头 $headers[] = 'Content-Type: application/json'; $headers[] = 'Accept: application/json'; // 编码数据为 JSON(空数据编码为 "{}" 避免服务器解析错误) $data = $data !== null ? json_encode($data, JSON_UNESCAPED_UNICODE) : '{}'; } // 设置请求头和数据 if (!empty($headers)) { $options[CURLOPT_HTTPHEADER] = $headers; } if ($data !== null) { $options[CURLOPT_POSTFIELDS] = $data; } curl_setopt_array($curl, $options); $result = curl_exec($curl); // 错误处理(调试时启用) if ($result === false) { $error = curl_error($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); throw new Exception("CURL请求失败: HTTP状态码 $httpCode, 错误信息: $error"); } curl_close($curl); return $result; } exit;