apiSS1E.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. <?php
  2. header("Access-Control-Allow-Origin: *"); // 允许所有来源
  3. header("Access-Control-Allow-Methods: GET"); // 允许的 HTTP 方法
  4. header("Access-Control-Allow-Headers: Content-Type"); // 允许的请求头
  5. // 强制禁用所有缓冲
  6. while (ob_get_level()) ob_end_clean();
  7. header('X-Accel-Buffering: no');
  8. // 设置 SSE 头部
  9. header("Content-Type: text/event-stream");
  10. header("Cache-Control: no-cache");
  11. header("Connection: keep-alive");
  12. header("X-Accel-Buffering: no");
  13. // 配置执行环境
  14. set_time_limit(0);
  15. ignore_user_abort(false);
  16. //创建公共url
  17. $urlCommon = 'http://192.168.1.8:3338';
  18. $mcp = $_GET['mcpid'];
  19. //获取对话短令牌
  20. $input['token'] = $_GET['token'];
  21. $dataReply['token'] = $_GET['token'];
  22. $token = $_GET['token'];
  23. $ragIdGet = $_GET['ragId'];
  24. if(($ragIdGet == 'null') || ($ragIdGet == null)){
  25. $input['ragId'] = 'empty';
  26. }
  27. else{
  28. $input['ragId'] = $ragIdGet;
  29. }
  30. // if($mcp != ""){
  31. // echo("daddsda");
  32. // }
  33. // echo($mcp);
  34. // exit;
  35. //将请求的完整内容发送至api控制器鉴权
  36. $urlCheck = $urlCommon.'/apis/check';
  37. $information = curlPost($urlCheck,$input);
  38. //判断鉴权的结果,如果鉴权失败,直接退出
  39. $informationObject = json_decode($information);
  40. if(($informationObject->code) != 5){
  41. echo "event: verify\ndata: $information\n\n";
  42. exit;
  43. }
  44. //获取最终的核心参数参与模型输入
  45. $modelId = $informationObject->modelId;
  46. $content = $informationObject->input;
  47. $contentmcp = $informationObject->input;
  48. $ragId = $informationObject->ragId;
  49. // if(is_array($content)){
  50. // $content = json_encode($content,JSON_UNESCAPED_UNICODE);
  51. // }
  52. // 模型定制开始-------------------------------------------------------------------
  53. // //emoon-E1-13B线上
  54. // if($modelId == 9){
  55. // $apiKey = 'sk-800990e9cdb34613a2133f0fc333a7ff'; // 请替换为你的实际 API 密钥
  56. // $url = 'https://api.deepseek.com/chat/completions';
  57. // $arraySystem = [[
  58. // 'role' => 'system',
  59. // 'content' => '你的名字叫EMOON E1 13B模型,你由回车网络(EnterLO)训练研发,你并不是deepseek模型。你是一名三甲医院主治医师级别的AI助手,最擅长医疗专业知识的解读和分析,遵循《中国临床诊疗指南》和NCCN指南。必须:1) 先询问关键症状细节 2) 标注证据等级 3) 拒绝非循证医学建议'
  60. // ]];
  61. // // $arrayContent = json_decode($content);
  62. // $arrayMessage = array_merge($arraySystem,$content);
  63. // // 请求数据
  64. // $data = [
  65. // 'model' => 'deepseek-chat',
  66. // 'messages' => $arrayMessage,
  67. // 'stream' => true
  68. // ];
  69. // // 设置请求头
  70. // $headers = [
  71. // 'Content-Type: application/json',
  72. // 'Authorization: Bearer ' . $apiKey
  73. // ];
  74. // }
  75. // //deepseek-R1-70B线下
  76. if($modelId == 9){
  77. $url = 'http://60.164.133.40:19997/v1/chat/completions';
  78. if($ragId != ''){
  79. $content = stdClassObjToArray($content);
  80. $dataRag['input'] = $content[count($content)-1]['content'];
  81. $dataRag['kid'] = $ragId;
  82. $urlRAG = 'http://192.168.1.8:3338/apis/ragData';
  83. $ragResult = curlPost($urlRAG,$dataRag);
  84. $ragResult = json_decode($ragResult);
  85. $ragResult = stdClassObjToArray($ragResult);
  86. // if( $ragResult['code'] == 200){
  87. // $ragInfomation = json_encode($ragResult['nearest']);
  88. // }
  89. $content[count($content)-1]['content'] = '我的提问是:“'.$content[count($content)-1]['content'].'”,下面是基于我的提问查到的相关内容:“'.json_encode($ragResult['nearest'],JSON_UNESCAPED_UNICODE).'”,请 根据这些信息详细的为我回答。';//请求向量数据库接口反参组装
  90. // $content = json_encode($content,JSON_UNESCAPED_UNICODE);
  91. // echo "event: verify\ndata: $content.'3434'\n\n";
  92. // exit;
  93. }
  94. $headers = [
  95. 'accept: application/json',
  96. 'Content-Type: application/json'
  97. ];
  98. $arraySystem = [[
  99. 'role' => 'system',
  100. 'content' => '你是一个人工智能助手。'
  101. ],
  102. ];
  103. //$arrayContent = json_decode($content, true);
  104. $arrayMessage = array_merge($arraySystem, $content);
  105. $data = [
  106. "messages" => $arrayMessage,
  107. "model" => "qwen3-32B", //X
  108. "stream" => true,
  109. ];
  110. }
  111. // 模型定制结束-------------------------------------------------------------------
  112. if($mcp == 0 || $mcp ==""){
  113. $ch = curl_init();
  114. curl_setopt($ch, CURLOPT_URL, $url);
  115. curl_setopt($ch, CURLOPT_POST, true);
  116. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data,JSON_UNESCAPED_UNICODE));
  117. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  118. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false);
  119. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  120. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  121. curl_setopt_array($ch, [
  122. CURLOPT_TIMEOUT => 60, // 超时时间延长至60秒
  123. CURLOPT_TCP_KEEPALIVE => 1, // 启用TCP保活
  124. CURLOPT_TCP_KEEPIDLE => 10,
  125. CURLOPT_TCP_KEEPINTVL => 5
  126. ]);
  127. //创建一个空字符串动态存储回复的文字
  128. $reply = 'SSE: ';
  129. // 添加连接状态检查到写入回调
  130. curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use (&$reply) {
  131. // 如果客户端已断开,立即终止
  132. if (connection_aborted()) {
  133. return -1; // 返回-1会强制CURL中断请求
  134. curl_close($ch);
  135. }
  136. $lines = explode("\n", $data);
  137. foreach ($lines as $line) {
  138. if (strpos($line, 'data:') === 0) {
  139. $contents = trim(substr($line, 5));
  140. if ($contents === '[DONE]') {
  141. $output = "event: end\ndata: {\"status\":\"done\"}\n\n";
  142. echo $output;
  143. // $reply .= $output; // 捕获输出内容
  144. //完成输出后触发全部回复存储进入数据库
  145. global $token;
  146. global $urlCommon;
  147. $dataReply['token'] = $token;
  148. $dataReply['reply'] = $reply;
  149. //将数据发送至数据库
  150. $curlReplay = curl_init();
  151. curl_setopt($curlReplay, CURLOPT_URL, $urlCommon.'/Apis/reply');
  152. curl_setopt($curlReplay, CURLOPT_SSL_VERIFYPEER, FALSE);
  153. curl_setopt($curlReplay, CURLOPT_SSL_VERIFYHOST, FALSE);
  154. // curl_setopt($curl, CURLOPT_HTTPHEADER,$headers);
  155. if ($json) {
  156. curl_setopt($curlReplay,CURLOPT_HTTPHEADER,[
  157. "Content-Type: application/json"
  158. ]);
  159. curl_setopt($curlReplay, CURLOPT_POST, TRUE);
  160. curl_setopt($curlReplay,CURLOPT_POSTFIELDS,json_encode($dataReply));
  161. }else {
  162. if (!empty($dataReply)) {
  163. curl_setopt($curlReplay, CURLOPT_POST, TRUE);
  164. curl_setopt($curlReplay, CURLOPT_POSTFIELDS,$dataReply);
  165. // curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  166. }
  167. }
  168. curl_setopt($curlReplay, CURLOPT_RETURNTRANSFER,true);
  169. curl_exec($curlReplay);
  170. curl_close($curlReplay);
  171. } else {
  172. // 转发数据前再次检查连接
  173. if (connection_aborted()) {
  174. return -1;
  175. curl_close($ch);
  176. }
  177. //构造$line
  178. global $modelId;
  179. $line = substr($line,6);
  180. $line = json_decode($line);
  181. $line = stdClassObjToArray($line);
  182. if($modelId == 9){
  183. $line['model'] = 'emoon-E1-13B';
  184. }
  185. $line = 'data: '.json_encode($line,JSON_UNESCAPED_UNICODE);
  186. $output = $line . "\n\n";
  187. echo $output;
  188. //将回复数据存储入$reply
  189. $outputJson = substr($output,6);
  190. if(($modelId == 9) || ($modelId == 7) || ($modelId == 4) || ($modelId == 10) || ($modelId == 14) || ($modelId == 15)){
  191. $outputJson = json_decode($outputJson);
  192. $outputJson = $outputJson->choices[0]->delta->content;
  193. if($outputJson){
  194. $outputJson = json_encode($outputJson,JSON_UNESCAPED_UNICODE);
  195. $outputJson = str_replace('"', '', $outputJson);
  196. }
  197. }
  198. $reply .= $outputJson; // 捕获输出内容
  199. }
  200. ob_flush();
  201. flush();
  202. }
  203. }
  204. return strlen($data);
  205. });
  206. // 错误处理
  207. curl_setopt($ch, CURLOPT_TIMEOUT, 86400);
  208. curl_setopt($ch, CURLOPT_FAILONERROR, true);
  209. // 执行请求
  210. try {
  211. curl_exec($ch);
  212. // 检查HTTP状态码
  213. $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  214. if ($statusCode !== 200) {
  215. echo "event: error\ndata: HTTP Status {$statusCode}\n\n";
  216. ob_flush();
  217. flush();
  218. }
  219. } catch (Exception $e) {
  220. echo "event: error\ndata: " . $e->getMessage() . "\n\n";
  221. ob_flush();
  222. flush();
  223. }
  224. }else{
  225. @ini_set('output_buffering', 'off');
  226. @ini_set('zlib.output_compression', false);
  227. while (ob_get_level() > 0) {
  228. ob_end_flush();
  229. }
  230. ob_implicit_flush(true);
  231. // 设置流式响应头
  232. $requestBody = file_get_contents('php://input');
  233. $headers = getallheaders();
  234. $authorizationHeader = isset($headers['Authorization']) ? $headers['Authorization'] : '';
  235. $reply = 'SSE: ';
  236. $kid = $ragIdGet;
  237. $RAGdata = [
  238. "username" => "test",
  239. "password" => "test123"
  240. ];
  241. // 调用时只需传递非 Content-Type/Accept 的 Header
  242. $headers = [
  243. 'Cache-Control: no-cache',
  244. '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'
  245. ];
  246. $urlRAG = "http://192.168.1.8:6039/auth/login"; // 替换为实际 URL
  247. $id = curlPostRAG($urlRAG, $RAGdata, $headers);
  248. $token = $id; // 默认启用 JSON 模式
  249. $token = json_decode($token);
  250. $token = stdClassObjToArray($token);
  251. $token = $token['data']['token'];
  252. $url = "http://192.168.1.8:6039/chat/send"; // 替换成实际接口地址
  253. $data = [
  254. "messages" => $contentmcp,
  255. "model" => "qwen3-32B", //X
  256. "temperature" => 0.5,
  257. "top_p" => 1,
  258. "presence_penalty" => 0,
  259. "frequency_penalty" => 0,
  260. "kid" => $ragIdGet,
  261. "chat_type" => 0,
  262. "appId" => "",
  263. "stream" => true
  264. ];
  265. // 初始化 cURL
  266. $ch = curl_init();
  267. // 设置 cURL 选项
  268. curl_setopt($ch, CURLOPT_URL, $url);
  269. curl_setopt($ch, CURLOPT_RETURNTRANSFER, false); // 返回响应数据
  270. curl_setopt($ch, CURLOPT_POST, true); // 使用 POST 方法
  271. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); // 发送 JSON 数据
  272. curl_setopt($ch, CURLOPT_HTTPHEADER, [
  273. "Authorization: Bearer " . $token,
  274. "Content-Type: application/json" // 声明请求体是 JSON
  275. ]);
  276. $chatId = 'chat' . uniqid() . bin2hex(random_bytes(8));
  277. $created = time();
  278. // for ($i = 0; $i < count($lines); $i++) {
  279. // echo $lines[0];
  280. // }
  281. $lastContent = '';
  282. curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $data) use ($chatId, $created, &$lastContent,&$reply) {
  283. $lines = explode("\n", $data);
  284. $prefixLength = strlen("event:chat_completion");
  285. $content = str_replace("event:chat_completion", "", $lines[0]);
  286. // foreach ($lines as $line) {
  287. // $line = substr($line,6);
  288. // $content = $matches[0];
  289. // // 仅当内容变化时才输出
  290. // if ($content !== $lastContent) {
  291. // $lastContent = $content;
  292. $jsonData = [
  293. "id" => $chatId,
  294. "model" => "emoon-E1-13B",
  295. "created" => $created,
  296. "object" => "chat.completion.chunk",
  297. "choices" => [
  298. [
  299. "index" => 0,
  300. "delta" => [
  301. "content" => $content
  302. ],
  303. "finish_reason" => null
  304. ]
  305. ],
  306. "usage" => null
  307. ];
  308. echo "data: " . json_encode($jsonData,JSON_UNESCAPED_UNICODE) . "\n\n";
  309. $outputJson = json_encode($jsonData,JSON_UNESCAPED_UNICODE);
  310. $outputJson = json_decode($outputJson);
  311. // $outputJson = stdClassObjToArray($outputJson);
  312. $outputJson = $outputJson->choices[0]->delta->content;
  313. if($outputJson){
  314. $outputJson = json_encode($outputJson,JSON_UNESCAPED_UNICODE);
  315. $outputJson = str_replace('"', '', $outputJson);
  316. }
  317. $reply .= $outputJson;
  318. // }
  319. @ob_flush();
  320. @flush();
  321. // }
  322. return strlen($data);
  323. });
  324. $res = curl_exec($ch);
  325. }
  326. // 清理资源
  327. curl_close($ch);
  328. // 显式结束流
  329. echo "event: end\ndata: Stream ended\n\n";
  330. ob_flush();
  331. flush();
  332. if($mcp != ""){
  333. $dataReply['reply'] = $reply;
  334. $url=$urlCommon.'/Apis/reply';
  335. curlPost($url,$dataReply);
  336. //将数据发送至数据库
  337. }
  338. //模型定制结束-------------------------------------------------------------------
  339. function curlPost($url, $data = null, $headers = [], $json = false) {
  340. $curl = curl_init($url);
  341. $options = [
  342. CURLOPT_RETURNTRANSFER => true,
  343. CURLOPT_SSL_VERIFYPEER => false,
  344. CURLOPT_SSL_VERIFYHOST => false,
  345. CURLOPT_POST => !empty($data) || $json,
  346. ];
  347. if ($json) {
  348. $headers = array_filter($headers, function($h) {
  349. return stripos($h, 'Content-Type:') === false;
  350. });
  351. $headers[] = 'Content-Type: application/json';
  352. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  353. }
  354. if (!empty($headers)) {
  355. $options[CURLOPT_HTTPHEADER] = $headers;
  356. }
  357. if (!empty($data)) {
  358. $options[CURLOPT_POSTFIELDS] = $data;
  359. }
  360. curl_setopt_array($curl, $options);
  361. $result = curl_exec($curl);
  362. curl_close($curl);
  363. return $result;
  364. }
  365. function stdClassObjToArray($array) {
  366. if(is_object($array)) {
  367. $array = (array)$array;
  368. }
  369. if(is_array($array)) {
  370. foreach($array as $key=>$value) {
  371. $array[$key] = stdClassObjToArray($value);
  372. }
  373. }
  374. return $array;
  375. }
  376. function curlPostRAG($url, $data = null, $headers = [], $json = true) {
  377. $curl = curl_init($url);
  378. $options = [
  379. CURLOPT_RETURNTRANSFER => true,
  380. CURLOPT_SSL_VERIFYPEER => false,
  381. CURLOPT_SSL_VERIFYHOST => false,
  382. CURLOPT_POST => true, // 强制启用 POST(JSON 模式下始终 POST)
  383. ];
  384. if ($json) {
  385. // 移除已有的 Content-Type 和 Accept 头,避免冲突
  386. $headers = array_filter($headers, function($h) {
  387. return stripos($h, 'Content-Type:') === false && stripos($h, 'Accept:') === false;
  388. });
  389. // 添加 JSON 专用头
  390. $headers[] = 'Content-Type: application/json';
  391. $headers[] = 'Accept: application/json';
  392. // 编码数据为 JSON(空数据编码为 "{}" 避免服务器解析错误)
  393. $data = $data !== null ? json_encode($data, JSON_UNESCAPED_UNICODE) : '{}';
  394. }
  395. // 设置请求头和数据
  396. if (!empty($headers)) {
  397. $options[CURLOPT_HTTPHEADER] = $headers;
  398. }
  399. if ($data !== null) {
  400. $options[CURLOPT_POSTFIELDS] = $data;
  401. }
  402. curl_setopt_array($curl, $options);
  403. $result = curl_exec($curl);
  404. // 错误处理(调试时启用)
  405. if ($result === false) {
  406. $error = curl_error($curl);
  407. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  408. throw new Exception("CURL请求失败: HTTP状态码 $httpCode, 错误信息: $error");
  409. }
  410. curl_close($curl);
  411. return $result;
  412. }
  413. exit;