apiSS1E.php 21 KB

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