request->post(); $iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id']; if(empty($iArticleId)){ return json_encode(array('status' => 2,'msg' => 'Please select an article' )); } //查询文章 $aWhere = ['article_id' => $aParam['article_id']]; $aArticle = Db::name('article')->field('article_id,abstrart,keywords,journal_id')->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(); 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(); if(empty($aJournal)){ return json_encode(array('status' => 4,'msg' => 'This article is not associated with a journal' )); } //实例化公共方法 $oOpenAi = new OpenAi; //查询文章内容 $aWhere['type'] = 0; $aWhere['content'] = ['<>','']; $aWhere['state'] = 0; $aArticleMain = Db::table('t_article_main')->where($aWhere)->column('content'); if(empty($aArticleMain)){//读取文件内容 $aFile = json_decode($oOpenAi->getFileContent(['article_id' => $iArticleId]),true); $aFile = empty($aFile['data']) ? [] : $aFile['data']; $aArticleMain = empty($aFile['mains']) ? [] : $aFile['mains']; } //获取提问AI的内容 $aSearch = []; $abstrart = empty($aArticle['abstrart']) ? '' : $aArticle['abstrart'];//简介 $keywords = empty($aArticle['keywords']) ? '' : $aArticle['keywords'];//关键词 $sContent = empty($aArticleMain) ? '' : strip_tags(implode('', array_unique($aArticleMain)),'');//文章内容 if(empty(trim($sContent))){ $sContent = '摘要:'.$abstrart.'关键词:'.$keywords; } $aSearch['{content}'] = $sContent; $aSearch['{journal_name}'] = empty($aJournal['zname']) ? '' : $aJournal['zname'];//期刊名 if($aJournal['journal_id'] == 1){ $aSearch['{journal_name}'] = '传统医学研究'; } //查询期刊内容 $aJournalPaperArt = json_decode($oOpenAi->getJournalPaperArt($aJournal),true); $sJournalContent = empty($aJournalPaperArt['data']) ? '' : strip_tags(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); $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; if($iStatus != 1){ return json_encode($aResult); } //处理返回信息 $aData = empty($aResult['data']) ? [] : $aResult['data']; if(empty($aData)){ return json_encode(['status' => 6,'msg' => 'OPENAI returns empty content']); } if(empty($aData)){ return json_encode(array('status' => 10,'msg' => 'OPENAI did not return data')); } //执行数据入库 $aData['article_id'] = $iArticleId; $aResult = $this->addAiReview($aData); return json_encode($aResult); } /** * @title AI审核内容入库 * @param article_id 文章ID * @param content 内容 */ protected 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'])){ 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)); } $aInsert['content'] = $aInsert['article_id']; if(!Db::name('article_ai_review')->insert($aInsert)){ $aResult = ['status' => 2,'msg' => 'Failed to add AI audit content']; } $aResult['data'] = $aInsert; return $aResult; } /** * @title 文章AI审核内容查询 * @param article_id 文章ID */ public function get(){ //获取参数 $aParam = $this->request->post(); 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(); 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)); } }