diff --git a/application/api/controller/Aireview.php b/application/api/controller/Aireview.php index c5319f9..d1fd59f 100644 --- a/application/api/controller/Aireview.php +++ b/application/api/controller/Aireview.php @@ -20,7 +20,7 @@ class Aireview extends Base * @param model 接口模型 * @param stream 是否流式输出 true是false否 */ - public function review(){ + public function review(){ //获取参数 @@ -30,20 +30,24 @@ class Aireview extends Base return json_encode(array('status' => 2,'msg' => 'Please select an article' )); } - //查询文章 + //查询文章及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' )); } @@ -75,32 +79,30 @@ class Aireview 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}'] = '传统医学研究'; } //查询期刊内容 $aJournalPaperArt = json_decode($oOpenAi->getJournalPaperArt($aJournal),true); $sJournalContent = empty($aJournalPaperArt['data']) ? '' : implode('', $aJournalPaperArt['data']); - $sJournalContent = preg_replace('/[\r\n]+/', '', $sJournalContent); - $sJournalContent = preg_replace('/ +/', ' ', $sJournalContent); // 合并连续空格 - $sJournalContent = strip_tags($sJournalContent); $sJournalContent = empty($sJournalContent) ? $aJournal['scope'] : $sJournalContent; $aSearch['{scope}'] = $sJournalContent;//期刊范围 - $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); + //获取重要维度的问答信息 + $oOpenAi = new OpenAi; + //请求OPENAI接口-重要维度单独请求获取答案 + $aSearch['question'] = 'aArticleImportantPrompt'; + $aSearch['open_ai_id'] = $iArticleId; + $aResult = json_decode($oOpenAi->curlMultiOpenAIImportant($aSearch),true); $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; if($iStatus != 1){ return json_encode($aResult); @@ -110,15 +112,33 @@ class Aireview extends Base 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); - } /** @@ -126,65 +146,88 @@ class Aireview extends Base * @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','references_num']; - $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']; } - - //判断是否生成 - $aAiReview = Db::table('t_article_ai_review')->where('article_id',$aInsert['article_id'])->find(); - if(!empty($aAiReview)){ - return json_encode(array('status' => 1,'msg' => 'AI has been reviewed','data' => $aAiReview)); + $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 = ['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']; } - $aInsert['content'] = $aInsert['article_id']; - if(!Db::name('article_ai_review')->insert($aInsert)){ - $aResult = ['status' => 2,'msg' => 'Failed to add AI audit content']; + //查询文章审核内容-判断新增或修改 + $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; } - $aResult['data'] = $aInsert; - 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)); } }