apiHTTP.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. //创建公共url
  6. $urlCommon = 'http://192.168.1.8:3338';
  7. //获取对话短令牌
  8. $input['token'] = $_POST['token'];
  9. $token = $_POST['token'];
  10. $mcp = $_POST['mcpid'];
  11. $ragIdGet = $_POST['ragId'];
  12. $json = file_get_contents('php://input');
  13. $json = json_decode($json);
  14. $json = stdClassObjToArray($json);
  15. if($json['token'] != ""){
  16. $token = $json['token'];
  17. $input['token'] = $json['token'];
  18. }
  19. if($json['mcpid'] != ""){
  20. $mcp = $json['mcpid'];
  21. }
  22. if($json['ragId'] != ""){
  23. $ragIdGet = $json['ragId'];
  24. }
  25. if(($ragIdGet == 'null') || ($ragIdGet == null)){
  26. $input['ragId'] = 'empty';
  27. }
  28. else{
  29. $input['ragId'] = $ragIdGet;
  30. }
  31. //将请求的完整内容发送至api控制器鉴权
  32. $urlCheck = $urlCommon.'/apis/check';
  33. $information = curlPost($urlCheck,$input);
  34. //判断鉴权的结果,如果鉴权失败,直接退出
  35. $informationObject = json_decode($information);
  36. if(($informationObject->code) != 5){
  37. echo $information;
  38. exit;
  39. }
  40. //获取最终的核心参数参与模型输入
  41. $modelId = $informationObject->modelId;
  42. $content = $informationObject->input;
  43. $contentmcp = $informationObject->input;
  44. $ragId = $informationObject->ragId;
  45. // if(is_array($content)){
  46. // $content = json_encode($content,JSON_UNESCAPED_UNICODE);
  47. // }
  48. if($ragId != ''){
  49. $content = stdClassObjToArray($content);
  50. $dataRag['input'] = $content[count($content)-1]['content'];
  51. $dataRag['kid'] = $ragId;
  52. $urlRAG = 'http://192.168.1.8:3338/apis/ragData';
  53. $ragResult = curlPost($urlRAG,$dataRag);
  54. $ragResult = json_decode($ragResult);
  55. $ragResult = stdClassObjToArray($ragResult);
  56. // if( $ragResult['code'] == 200){
  57. // $ragInfomation = json_encode($ragResult['nearest']);
  58. // }
  59. if(json_encode($ragResult['nearest'],JSON_UNESCAPED_UNICODE) != "[]" ){
  60. if($ragId == "1947484295610253313" || $ragId == "1949641433623703553"){
  61. $content[count($content)-1]['content'] = '我的提问是:“'.$content[count($content)-1]['content'].'”,下面是基于我的提问查到的知识库相关内容:“'.json_encode($ragResult['nearest'],JSON_UNESCAPED_UNICODE).'”,请按照查到的知识库相关内容,进行病历质控,如果不符合哪项指控规则则按以下格式进行返回,返回格式:质控结果如下,该份病例不符合以下质控规则:规则1:XXXX-扣分x,原因:xxx,规则2:XXXX-扣分x,原因:xxx,……。扣除的分数严格与知识库中内容对应,符合要求的规则不回复输出显示,无法准确判断的规则不回复输出,只回复显示不符合的质控规则。 ';//请求向量数据库接口反参组装
  62. $contentmcp = $content;
  63. }
  64. else{
  65. $content[count($content)-1]['content'] = '我的提问是:“'.$content[count($content)-1]['content'].'”,下面是基于我的提问查到的知识库相关内容:“'.json_encode($ragResult['nearest'],JSON_UNESCAPED_UNICODE).'”。';//请求向量数据库接口反参组装
  66. $contentmcp = $content;
  67. }
  68. }
  69. }
  70. if($mcp != ''){
  71. @ini_set('output_buffering', 'off');
  72. @ini_set('zlib.output_compression', false);
  73. while (ob_get_level() > 0) {
  74. ob_end_flush();
  75. }
  76. ob_implicit_flush(true);
  77. // 设置流式响应头
  78. $requestBody = file_get_contents('php://input');
  79. $headers = getallheaders();
  80. $authorizationHeader = isset($headers['Authorization']) ? $headers['Authorization'] : '';
  81. $RAGdata = [
  82. "username" => "test",
  83. "password" => "test123"
  84. ];
  85. // 调用时只需传递非 Content-Type/Accept 的 Header
  86. $headers = [
  87. 'Cache-Control: no-cache',
  88. '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'
  89. ];
  90. $urlRAG = "http://192.168.1.8:6039/auth/login"; // 替换为实际 URL
  91. $id = curlPostRAG($urlRAG, $RAGdata, $headers);
  92. $token = $id; // 默认启用 JSON 模式
  93. $token = json_decode($token);
  94. $token = stdClassObjToArray($token);
  95. $token = $token['data']['token'];
  96. $urlAI = "http://192.168.1.8:6039/chat/emoonSend"; // 替换成实际接口地址
  97. $dataAI = [
  98. "messages" => $contentmcp,
  99. "model" => "qwen3-32B", //X
  100. "temperature" => 0.5,
  101. "top_p" => 1,
  102. "presence_penalty" => 0,
  103. "frequency_penalty" => 0,
  104. "kid" => "",
  105. "chat_type" => 0,
  106. "appId" => "",
  107. "agentId" => $mcp,
  108. "stream" => false,
  109. "chat_template_kwargs" => '{"enable_thinking": false}',
  110. ];
  111. $headers = [
  112. "Authorization: Bearer " . $token,
  113. 'Content-Type: application/json'
  114. ];
  115. }
  116. else{
  117. //emoon-E1-13B线上
  118. if($modelId == 9){
  119. $urlAI = 'http://60.164.133.40:19997/v1/chat/completions';
  120. $headers = [
  121. 'accept: application/json',
  122. 'Content-Type: application/json'
  123. ];
  124. $arraySystem = [[
  125. 'role' => 'system',
  126. 'content' => '你是一个人工智能助手。'
  127. ],
  128. ];
  129. //$arrayContent = json_decode($content, true);
  130. $arrayMessage = array_merge($arraySystem, $content);
  131. $dataAI = [
  132. "messages" => $arrayMessage,
  133. "model" => "qwen3-32B", //X
  134. "stream" => false,
  135. "chat_template_kwargs" => '{"enable_thinking": false}',
  136. ];
  137. }
  138. }
  139. //发起AI请求
  140. $response = curlPost($urlAI,$dataAI,$headers,true);
  141. if($mcp != ''){
  142. echo($response);
  143. }
  144. else{
  145. //emoon-E1-13B线上
  146. if($modelId == 9){
  147. $response = json_decode($response);
  148. $response = stdClassObjToArray($response);
  149. $response['model']='qwen3-32B';
  150. // 输出结果
  151. $response = json_encode($response,JSON_UNESCAPED_UNICODE);
  152. echo $response;
  153. //按照模型提取AI回复的信息
  154. $outputJson = json_decode($response);
  155. $outputJson = $outputJson->choices[0]->message->content;
  156. }
  157. //ollama请求标准deepseek-R1-8B本地输出
  158. if($modelId == 11){
  159. echo $response;
  160. //按照模型提取AI回复的信息
  161. $outputJson = json_decode($response);
  162. $outputJson = $outputJson->message->content;
  163. }
  164. //gstcm-G1-7B本地输出
  165. if($modelId == 12){
  166. $response = json_decode($response);
  167. $response = stdClassObjToArray($response);
  168. $response['model']='gstcm-G1-7B';
  169. // 输出结果
  170. $response = json_encode($response,JSON_UNESCAPED_UNICODE);
  171. echo $response;
  172. //按照模型提取AI回复的信息
  173. $outputJson = json_decode($response);
  174. $outputJson = $outputJson->choices[0]->message->content;
  175. }
  176. //emoon-data-E2-7B线上
  177. if($modelId == 13){
  178. $response = json_decode($response);
  179. $response = stdClassObjToArray($response);
  180. $response['model']='emoon-data-E2-7B';
  181. // 输出结果
  182. $response = json_encode($response,JSON_UNESCAPED_UNICODE);
  183. echo $response;
  184. //按照模型提取AI回复的信息
  185. $outputJson = json_decode($response);
  186. $outputJson = $outputJson->choices[0]->message->content;
  187. }
  188. }
  189. //存储AI回复的内容
  190. $dataReply['token'] = $input['token'];
  191. $dataReply['reply'] = 'HTTP: '.$outputJson;
  192. $urlReply = $urlCommon.'/Apis/reply';
  193. //将数据发送至数据库
  194. curlPost($urlReply,$dataReply);
  195. //------------------------------------------------------------
  196. function curlPost($url, $data = null, $headers = [], $json = false) {
  197. $curl = curl_init($url);
  198. $options = [
  199. CURLOPT_RETURNTRANSFER => true,
  200. CURLOPT_SSL_VERIFYPEER => false,
  201. CURLOPT_SSL_VERIFYHOST => false,
  202. CURLOPT_POST => !empty($data) || $json,
  203. ];
  204. if ($json) {
  205. $headers = array_filter($headers, function($h) {
  206. return stripos($h, 'Content-Type:') === false;
  207. });
  208. $headers[] = 'Content-Type: application/json';
  209. $data = json_encode($data, JSON_UNESCAPED_UNICODE);
  210. }
  211. if (!empty($headers)) {
  212. $options[CURLOPT_HTTPHEADER] = $headers;
  213. }
  214. if (!empty($data)) {
  215. $options[CURLOPT_POSTFIELDS] = $data;
  216. }
  217. curl_setopt_array($curl, $options);
  218. $result = curl_exec($curl);
  219. curl_close($curl);
  220. return $result;
  221. }
  222. function stdClassObjToArray($array) {
  223. if(is_object($array)) {
  224. $array = (array)$array;
  225. }
  226. if(is_array($array)) {
  227. foreach($array as $key=>$value) {
  228. $array[$key] = stdClassObjToArray($value);
  229. }
  230. }
  231. return $array;
  232. }
  233. function curlPostRAG($url, $data = null, $headers = [], $json = true) {
  234. $curl = curl_init($url);
  235. $options = [
  236. CURLOPT_RETURNTRANSFER => true,
  237. CURLOPT_SSL_VERIFYPEER => false,
  238. CURLOPT_SSL_VERIFYHOST => false,
  239. CURLOPT_POST => true, // 强制启用 POST(JSON 模式下始终 POST)
  240. ];
  241. if ($json) {
  242. // 移除已有的 Content-Type 和 Accept 头,避免冲突
  243. $headers = array_filter($headers, function($h) {
  244. return stripos($h, 'Content-Type:') === false && stripos($h, 'Accept:') === false;
  245. });
  246. // 添加 JSON 专用头
  247. $headers[] = 'Content-Type: application/json';
  248. $headers[] = 'Accept: application/json';
  249. // 编码数据为 JSON(空数据编码为 "{}" 避免服务器解析错误)
  250. $data = $data !== null ? json_encode($data, JSON_UNESCAPED_UNICODE) : '{}';
  251. }
  252. // 设置请求头和数据
  253. if (!empty($headers)) {
  254. $options[CURLOPT_HTTPHEADER] = $headers;
  255. }
  256. if ($data !== null) {
  257. $options[CURLOPT_POSTFIELDS] = $data;
  258. }
  259. curl_setopt_array($curl, $options);
  260. $result = curl_exec($curl);
  261. // 错误处理(调试时启用)
  262. if ($result === false) {
  263. $error = curl_error($curl);
  264. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  265. throw new Exception("CURL请求失败: HTTP状态码 $httpCode, 错误信息: $error");
  266. }
  267. curl_close($curl);
  268. return $result;
  269. }