From 55d9f4d66938a005c2dbf56053e9f3168b14d257 Mon Sep 17 00:00:00 2001 From: chengxl Date: Tue, 8 Jul 2025 14:55:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AF=B9=E6=8E=A5OPENAI=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Aitest.php | 182 +++++++++++++++----------- 1 file changed, 104 insertions(+), 78 deletions(-) diff --git a/application/api/controller/Aitest.php b/application/api/controller/Aitest.php index 983a04a..762ee33 100644 --- a/application/api/controller/Aitest.php +++ b/application/api/controller/Aitest.php @@ -32,20 +32,24 @@ class Aitest extends Base } - //查询文章 + //查询文章及AI审稿 $aWhere = ['article_id' => $aParam['article_id']]; - $aArticle = Db::name('article')->field('article_id,abstrart,keywords,journal_id,title,state')->where('article_id',$aParam['article_id'])->find(); + $aResult = json_decode($this->get($aWhere),true); + + //文章信息 + $aArticle = empty($aResult['article_data']) ? [] : $aResult['article_data']; if(empty($aArticle)){ - return json_encode(array('status' => 3,'msg' => 'No articles requiring review were found' )); + json_encode(array('status' => 3,'msg' => 'No articles requiring review were found' )); } - //获取文章评测内容 - $aAiReview = Db::table('t_article_ai_review')->where('article_id',$aParam['article_id'])->find(); + + //AI审稿信息 + $aAiReview = empty($aResult['data']) ? [] : $aResult['data']; if(!empty($aAiReview)){ return json_encode(array('status' => 1,'msg' => 'AI has been reviewed','data' => $aAiReview)); } //根据期刊ID查询期刊信息 - $aJournal = Db::table('t_journal')->field('zname,scope,issn,journal_id')->where('journal_id',$aArticle['journal_id'])->find(); + $aJournal = Db::table('t_journal')->field('title,scope,issn,journal_id')->where('journal_id',$aArticle['journal_id'])->find(); if(empty($aJournal)){ return json_encode(array('status' => 4,'msg' => 'This article is not associated with a journal' )); } @@ -77,13 +81,15 @@ class Aitest extends Base $title = empty($aArticle['title']) ? '' : $aArticle['title'];//简介 $abstrart = empty($aArticle['abstrart']) ? '' : $aArticle['abstrart'];//简介 $keywords = empty($aArticle['keywords']) ? '' : $aArticle['keywords'];//关键词 + $aSearch['{title}'] = $title; + $aSearch['{abstrart}'] = $abstrart; + $aSearch['{keywords}'] = $keywords; //文章内容 - $sContent = '文章标题:'.$title.'文章摘要:'.$abstrart.'文章关键词:'.$keywords."文章内容"; - $sContent .= empty($aArticleMain) ? '' : implode('', array_unique($aArticleMain)); + $sContent = empty($aArticleMain) ? '' : implode('', array_unique($aArticleMain)); $sContent = preg_replace('/[\r\n]+/', '', $sContent); $sContent = preg_replace('/ +/', ' ', $sContent); // 合并连续空格 $aSearch['{content}'] = $sContent; - $aSearch['{journal_name}'] = empty($aJournal['zname']) ? '' : $aJournal['zname'];//期刊名 + $aSearch['{journal_name}'] = empty($aJournal['title']) ? '' : $aJournal['title'];//期刊名 if($aJournal['journal_id'] == 1){ $aSearch['{journal_name}'] = '传统医学研究'; } @@ -92,17 +98,12 @@ class Aitest extends Base $sJournalContent = empty($aJournalPaperArt['data']) ? '' : implode('', $aJournalPaperArt['data']); $sJournalContent = empty($sJournalContent) ? $aJournal['scope'] : $sJournalContent; $aSearch['{scope}'] = $sJournalContent;//期刊范围 - //获取问答内容 - $oOpenAi = new OpenAi; - $aMessage = $oOpenAi->buildReviewArticlePrompt($aSearch); - if(empty($aMessage)){ - return json_encode(['status' => 5,'msg' => 'AI Q&A content not obtained']); - } - //请求OPENAI接口 - $aParam = ['messages' => $aMessage,'model' => empty($aParam['api_model']) ? 'gpt-4.1' : $aParam['api_model']]; - $aResult = json_decode($oOpenAi->curlOpenAI($aParam),true); -echo '
';var_dump($aResult);exit;
+        //获取重要维度的问答信息
+        $oOpenAi = new OpenAi;
+        //请求OPENAI接口-重要维度单独请求获取答案
+        $aSearch['question'] = 'aArticleImportantPrompt';
+        $aResult = json_decode($oOpenAi->curlMultiOpenAIImportant($aSearch),true);
         $iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
         if($iStatus != 1){
             return json_encode($aResult);
@@ -112,42 +113,33 @@ echo '
';var_dump($aResult);exit;
         if(empty($aData)){
             return json_encode(['status' => 6,'msg' => 'OPENAI returns empty content']);
         }
+        //执行数据入库
+        $aData['article_id'] = $iArticleId;
+        $aData['journal_id'] = $aArticle['journal_id'];
+        $aResult = $this->addAiReview($aData);
+        if(empty($aResult['data'])){
+            return json_encode($aResult);
+        }
+
+        //请求OPENAI接口-非重要维度一次请求获取答案
+        //获取提示词
+        $aMessage = $oOpenAi->buildReviewPromptUnimportant($aSearch);
+        if(empty($aMessage)){
+            return json_encode(['status' => 5,'msg' => 'AI Q&A content not obtained']);
+        }
+        $aParam = ['messages' => $aMessage,'model' => empty($aParam['api_model']) ? 'gpt-4.1' : $aParam['api_model']];
+        $aResult = json_decode($oOpenAi->curlOpenAI($aParam),true);
+        //处理返回信息
+        $aData = empty($aResult['data']) ? [] : $aResult['data'];
         if(empty($aData)){
-            return json_encode(array('status' => 10,'msg' => 'OPENAI did not return data'));
+            return json_encode(['status' => 6,'msg' => empty($aResult['msg']) ? 'OPENAI returns empty content' : $aResult['msg']]);
         }
         //执行数据入库
         $aData['article_id'] = $iArticleId;
+        $aData['journal_id'] = $aArticle['journal_id'];
         $aResult = $this->addAiReview($aData);
         return json_encode($aResult);
        
-
-    }
-
-    /**
-     * 保留内容中的文字和图片(移除其他HTML标签)
-     * @param string $content 原始富文本内容
-     * @return string 处理后的内容
-     */
-    private function keepTextAndImages($content)
-    {
-        if (empty($content)) {
-            return '';
-        }
-        
-        // 步骤1:保留标签和文本,移除其他所有HTML标签
-        // 使用strip_tags保留标签,其他标签全部移除
-        // $filtered = strip_tags($content, '');
-        
-        // 步骤2:处理特殊字符和冗余空白
-        $filtered = html_entity_decode($content); // 解码HTML实体(如 )
-        $filtered = preg_replace('/\s+/', ' ', $filtered); // 合并连续空白为单个空格
-        $filtered = trim($filtered); // 去除首尾空格
-        
-        // 步骤3:修复可能被破坏的标签格式(如属性缺失引号)
-        $filtered = preg_replace('/]+)>/i', '', $filtered);
-        $filtered = preg_replace('/src=([^\s>]+)/i', 'src="$1"', $filtered); // 确保src属性带引号
-        
-        return $filtered;
     }
 
     /**
@@ -155,58 +147,89 @@ echo '
';var_dump($aResult);exit;
      * @param article_id 文章ID
      * @param content 内容
      */
-    protected function addAiReview($aParam = array()){
+    private function addAiReview($aParam = array()){
 
-        //返回数组
-        $aResult = ['status' => 1,'msg' => 'AI review successful'];
-        //必填参数验证
-        $aFields = ['journal_scope','attribute','contradiction','unreasonable','ethics','academic','conclusion','fund_number','hotspot','submit_direction','overall_evaluation','article_id','references_past_three','references_past_five','references_ratio_JCR1','references_ratio_JCR2','registration_assessment','cite_rate'];
-        $aInsert = ['create_time' => date('Y-m-d H:i:s')];
-        foreach($aFields as $val){
-            $aValue = empty($aParam[$val]) ? [] : $aParam[$val];
-            if(!isset($aValue)){
-                continue;
-            }
-            if(is_array($aValue)){
-                foreach ($aValue as $key => $value) {
-                    $sField = $val.'_'.$key;
-                    $aInsert[$sField] = empty($value) ? '' : addslashes($value);
-                }
-            }else{
-                $aInsert[$val] = empty($aValue) ? '' : addslashes($aValue);
-            }
-        }
-        //执行入库
-        if(empty($aInsert['article_id'])){
+        $iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
+        if(empty($iArticleId)){
             return ['status' => 2,'msg' => 'Please select the article to be reviewed'];
         }
-        if(!Db::name('article_ai_review')->insert($aInsert)){
-           $aResult =  ['status' => 2,'msg' => 'Failed to add AI audit content'];
+        $iJournalId = empty($aParam['journal_id']) ? 0 : $aParam['journal_id'];
+        if(empty($iJournalId)){
+            return ['status' => 2,'msg' => 'The journal to which the article belongs cannot be empty'];
         }
-        $aResult['data'] = $aInsert;
-        return $aResult;
+        //返回数组
+        $aResult = ['status' => 1,'msg' => 'AI review successful'];
+        //数据库参数
+        $aFields = ['journal_scope','attribute','contradiction','unreasonable','ethics','academic','conclusion','fund_number','hotspot','submit_direction','references_past_three','references_past_five','references_ratio_JCR1','references_ratio_JCR2','registration_assessment','cite_rate','references_num'];
+        foreach ($aParam as $key => $value) {
+            if(empty($value)){
+                continue;
+            }
+            if(is_array($value)){
+                if(!empty($value['assessment'])){
+                    $sField = $key.'_'.'assessment';
+                    $aInsert[$sField] = empty($value['assessment']) ? '' : htmlspecialchars($value['assessment']);
+                }
+                if(!empty($value['explanation'])){
+                    $sField = $key.'_'.'explanation';
+                    $aInsert[$sField] = empty($value['explanation']) ? '' : htmlspecialchars($value['explanation']);
+                }
+            }else{
+                $aInsert[$key] = empty($value) ? '' : htmlspecialchars($value);
+            }
+        }
+        if(empty($aInsert)){
+            return ['status' => 3,'msg' => 'Data is empty'];
+        }
+
+        //查询文章审核内容-判断新增或修改
+        $aWhere = ['article_id' => $iArticleId,'journal_id' => $iJournalId];
+        $aAiReview = Db::table('t_article_ai_review')->field('id')->where($aWhere)->find();
+        $iLogId = empty($aAiReview['id']) ? 0 : $aAiReview['id'];
+        //新增
+        if(empty($iLogId)){
+            $aInsert['create_time'] = date('Y-m-d H:i:s');
+            $aInsert['content'] = $iArticleId;
+            $iLogId = Db::name('article_ai_review')->insertGetId($aInsert);
+            if(empty($iLogId)){
+               $aResult =  ['status' => 4,'msg' => 'Failed to add AI audit content'];
+            }
+            $aResult['data'] = ['id' => $iLogId];
+            return $aResult;
+        }
+        if(!empty($iLogId)){
+            $aWhere = ['id' => $iLogId];
+            $aInsert['update_time'] = date('Y-m-d H:i:s');
+            if(!Db::name('article_ai_review')->where($aWhere)->limit(1)->update($aInsert)){
+               $aResult =  ['status' => 5,'msg' => 'Failed to add AI audit content'];
+            }
+            $aAiReview = Db::table('t_article_ai_review')->where($aWhere)->find();
+            $aResult['data'] = $aAiReview;
+            return $aResult;
+        }
+        return ['status' => 6,'msg' => 'illegal request'];
     }
 
     /**
      * @title 文章AI审核内容查询
      * @param article_id 文章ID
      */
-    public function get(){
+    public function get($aParam = []){
 
         //获取参数
-        $aParam = $this->request->post();
+        $aParam = empty($aParam) ? $this->request->post() : $aParam;
         if(empty($aParam['article_id'])){
             return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
         }
 
         //查询文章
-        $aArticle = Db::table('t_article')->field('article_id')->where('article_id',$aParam['article_id'])->find();
+        $aArticle = Db::table('t_article')->field('article_id,abstrart,keywords,journal_id,title,state')->where('article_id',$aParam['article_id'])->find();
         if(empty($aArticle)){
             return json_encode(array('status' => 3,'msg' => 'No articles requiring review were found' ));
         }
         //查询文章审核内容
         $aAiReview = Db::table('t_article_ai_review')->where('article_id',$aParam['article_id'])->find();
-        return json_encode(array('status' => 1,'msg' => 'Successfully obtained article review content','data' => $aAiReview));
+        return json_encode(array('status' => 1,'msg' => 'Successfully obtained article review content','data' => $aAiReview,'article_data' => $aArticle));
     }
 
     /**
@@ -216,12 +239,15 @@ echo '
';var_dump($aResult);exit;
      */
     public function recommendedReviewer(){
 
+        $oQueueJob = new QueueJob;
          //获取参数
         $aParam = $this->request->post();
         if(empty($aParam['article_id'])){
             return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
         }
 
+        $aQueue = $oQueueJob->setRedisLabel(['redis_key' => $aParam['article_id']]);
+var_dump($aQueue,$aParam);
         // //查询条件
         // $aWhere = ['article_id' => $aParam['article_id']];
         // $aArticle = Db::name('article')->field('article_id,accept_sn')->where($aWhere)->limit(1)->select();