| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306 |
- <?php
- header("Access-Control-Allow-Origin: *"); // 允许所有来源
- header("Access-Control-Allow-Methods: GET"); // 允许的 HTTP 方法
- header("Access-Control-Allow-Headers: Content-Type"); // 允许的请求头
- //创建公共url
- $urlCommon = 'http://192.168.1.8:3338';
- //获取对话短令牌
- $input['token'] = $_POST['token'];
- $token = $_POST['token'];
- $mcp = $_POST['mcpid'];
- $ragIdGet = $_POST['ragId'];
- $json = file_get_contents('php://input');
- $json = json_decode($json);
- $json = stdClassObjToArray($json);
- if($json['token'] != ""){
- $token = $json['token'];
- $input['token'] = $json['token'];
- }
- if($json['mcpid'] != ""){
- $mcp = $json['mcpid'];
- }
- if($json['ragId'] != ""){
- $ragIdGet = $json['ragId'];
- }
- if(($ragIdGet == 'null') || ($ragIdGet == null)){
- $input['ragId'] = 'empty';
- }
- else{
- $input['ragId'] = $ragIdGet;
- }
- //将请求的完整内容发送至api控制器鉴权
- $urlCheck = $urlCommon.'/apis/check';
- $information = curlPost($urlCheck,$input);
- //判断鉴权的结果,如果鉴权失败,直接退出
- $informationObject = json_decode($information);
- if(($informationObject->code) != 5){
- echo $information;
- exit;
- }
- //获取最终的核心参数参与模型输入
- $modelId = $informationObject->modelId;
- $content = $informationObject->input;
- $contentmcp = $informationObject->input;
- $ragId = $informationObject->ragId;
- // if(is_array($content)){
- // $content = json_encode($content,JSON_UNESCAPED_UNICODE);
- // }
- if($ragId != ''){
- $content = stdClassObjToArray($content);
- $dataRag['input'] = $content[count($content)-1]['content'];
- $dataRag['kid'] = $ragId;
- $urlRAG = 'http://192.168.1.8: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).'”,请按照查到的知识库相关内容,进行病历质控,如果不符合哪项指控规则则按以下格式进行返回,返回格式:质控结果如下,该份病例不符合以下质控规则:规则1:XXXX-扣分x,原因:xxx,规则2:XXXX-扣分x,原因:xxx,……。扣除的分数严格与知识库中内容对应,符合要求的规则不回复输出显示,无法准确判断的规则不回复输出,只回复显示不符合的质控规则。 ';//请求向量数据库接口反参组装
- $contentmcp = $content;
- }
- else{
- $content[count($content)-1]['content'] = '我的提问是:“'.$content[count($content)-1]['content'].'”,下面是基于我的提问查到的知识库相关内容:“'.json_encode($ragResult['nearest'],JSON_UNESCAPED_UNICODE).'”。';//请求向量数据库接口反参组装
- $contentmcp = $content;
- }
- }
- }
- if($mcp != ''){
-
- @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'] : '';
-
- $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.1.8:6039/auth/login"; // 替换为实际 URL
-
- $id = curlPostRAG($urlRAG, $RAGdata, $headers);
-
- $token = $id; // 默认启用 JSON 模式
- $token = json_decode($token);
- $token = stdClassObjToArray($token);
- $token = $token['data']['token'];
- $urlAI = "http://192.168.1.8:6039/chat/emoonSend"; // 替换成实际接口地址
- $dataAI = [
- "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" => false,
- "chat_template_kwargs" => '{"enable_thinking": false}',
- ];
- $headers = [
- "Authorization: Bearer " . $token,
- 'Content-Type: application/json'
- ];
- }
- else{
- //emoon-E1-13B线上
- if($modelId == 9){
- $urlAI = 'http://60.164.133.40: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);
-
- $dataAI = [
- "messages" => $arrayMessage,
- "model" => "qwen3-32B", //X
- "stream" => false,
- "chat_template_kwargs" => '{"enable_thinking": false}',
- ];
- }
- }
- //发起AI请求
- $response = curlPost($urlAI,$dataAI,$headers,true);
- if($mcp != ''){
- echo($response);
- }
- else{
- //emoon-E1-13B线上
- if($modelId == 9){
- $response = json_decode($response);
- $response = stdClassObjToArray($response);
- $response['model']='qwen3-32B';
- // 输出结果
- $response = json_encode($response,JSON_UNESCAPED_UNICODE);
- echo $response;
- //按照模型提取AI回复的信息
- $outputJson = json_decode($response);
- $outputJson = $outputJson->choices[0]->message->content;
- }
- //ollama请求标准deepseek-R1-8B本地输出
- if($modelId == 11){
- echo $response;
- //按照模型提取AI回复的信息
- $outputJson = json_decode($response);
- $outputJson = $outputJson->message->content;
- }
- //gstcm-G1-7B本地输出
- if($modelId == 12){
- $response = json_decode($response);
- $response = stdClassObjToArray($response);
- $response['model']='gstcm-G1-7B';
- // 输出结果
- $response = json_encode($response,JSON_UNESCAPED_UNICODE);
- echo $response;
- //按照模型提取AI回复的信息
- $outputJson = json_decode($response);
- $outputJson = $outputJson->choices[0]->message->content;
- }
- //emoon-data-E2-7B线上
- if($modelId == 13){
- $response = json_decode($response);
- $response = stdClassObjToArray($response);
- $response['model']='emoon-data-E2-7B';
- // 输出结果
- $response = json_encode($response,JSON_UNESCAPED_UNICODE);
- echo $response;
- //按照模型提取AI回复的信息
- $outputJson = json_decode($response);
- $outputJson = $outputJson->choices[0]->message->content;
- }
- }
- //存储AI回复的内容
- $dataReply['token'] = $input['token'];
- $dataReply['reply'] = 'HTTP: '.$outputJson;
- $urlReply = $urlCommon.'/Apis/reply';
- //将数据发送至数据库
- curlPost($urlReply,$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;
- }
|