291 lines
13 KiB
PHP
291 lines
13 KiB
PHP
<?php
|
|
|
|
namespace app\api\controller;
|
|
|
|
use app\api\controller\Base;
|
|
use think\Db;
|
|
use app\common\OpenAi;
|
|
use app\common\QueueJob;
|
|
/**
|
|
* @title AI审核文章
|
|
* @description 对接OPENAI接口
|
|
*/
|
|
class Aitest extends Base
|
|
{
|
|
|
|
/**
|
|
* @title AI审核文章
|
|
* @param article_id 文章ID
|
|
* @param abstrart 摘要
|
|
* @param keywords 关键词
|
|
* @param model 接口模型
|
|
* @param stream 是否流式输出 true是false否
|
|
*/
|
|
public function review(){
|
|
|
|
|
|
//获取参数
|
|
$aParam = $this->request->post();
|
|
$iArticleId = empty($aParam['article_id']) ? '' : $aParam['article_id'];
|
|
if(empty($iArticleId)){
|
|
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
|
}
|
|
|
|
|
|
//查询文章及AI审稿
|
|
$aWhere = ['article_id' => $aParam['article_id']];
|
|
$aResult = json_decode($this->get($aWhere),true);
|
|
|
|
//文章信息
|
|
$aArticle = empty($aResult['article_data']) ? [] : $aResult['article_data'];
|
|
if(empty($aArticle)){
|
|
json_encode(array('status' => 3,'msg' => 'No articles requiring review were found' ));
|
|
}
|
|
|
|
//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('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' ));
|
|
}
|
|
|
|
//实例化公共方法
|
|
$oOpenAi = new OpenAi;
|
|
if($aArticle['state'] > 4 ){
|
|
//查询文章内容
|
|
$aWhere['type'] = 0;
|
|
$aWhere['content'] = ['<>',''];
|
|
$aWhere['state'] = 0;
|
|
$aArticleMain = Db::table('t_article_main')->where($aWhere)->column('content');
|
|
//查询参考文献
|
|
$aWhere = ['state' => ['in',[0,2]],'article_id' => $iArticleId];
|
|
$aProductionArticle = Db::name('production_article')->field('p_article_id')->where($aWhere)->find();
|
|
if(!empty($aProductionArticle)){
|
|
$aWhere = ['state' => 0,'p_article_id' => $aProductionArticle['p_article_id']];
|
|
$aRefer = Db::name('production_article_refer')->field('refer_content')->where($aWhere)->column('refer_content');
|
|
$aArticleMain = empty($aRefer) ? $aArticleMain : array_merge($aRefer,$aArticleMain);
|
|
}
|
|
}else{
|
|
|
|
$aFile = json_decode($oOpenAi->getFileContent(['article_id' => $iArticleId]),true);
|
|
$aFile = empty($aFile['data']) ? [] : $aFile['data'];
|
|
$aArticleMain = empty($aFile['mains']) ? [] : $aFile['mains'];
|
|
}
|
|
//获取提问AI的内容
|
|
$aSearch = [];
|
|
$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 = empty($aArticleMain) ? '' : implode('', array_unique($aArticleMain));
|
|
$sContent = preg_replace('/[\r\n]+/', '', $sContent);
|
|
$sContent = preg_replace('/ +/', ' ', $sContent); // 合并连续空格
|
|
$aSearch['{content}'] = $sContent;
|
|
$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 = empty($sJournalContent) ? $aJournal['scope'] : $sJournalContent;
|
|
$aSearch['{scope}'] = $sJournalContent;//期刊范围
|
|
|
|
//获取重要维度的问答信息
|
|
$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);
|
|
}
|
|
//处理返回信息
|
|
$aData = empty($aResult['data']) ? [] : $aResult['data'];
|
|
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(['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);
|
|
|
|
}
|
|
|
|
/**
|
|
* @title AI审核内容入库
|
|
* @param article_id 文章ID
|
|
* @param content 内容
|
|
*/
|
|
private function addAiReview($aParam = array()){
|
|
|
|
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
|
if(empty($iArticleId)){
|
|
return ['status' => 2,'msg' => 'Please select the article to be reviewed'];
|
|
}
|
|
$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'];
|
|
}
|
|
|
|
//查询文章审核内容-判断新增或修改
|
|
$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($aParam = []){
|
|
|
|
//获取参数
|
|
$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,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,'article_data' => $aArticle));
|
|
}
|
|
|
|
/**
|
|
* 批量处理待审核的文章自动推荐审稿人
|
|
*
|
|
* @return void
|
|
*/
|
|
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();
|
|
// if(empty($aArticle)){
|
|
// exit('未查询到需要处理的待审核的文章');
|
|
// }
|
|
//数据处理
|
|
$sMsg = '';
|
|
// foreach ($aArticle as $key => $value) {
|
|
// $iArticleId = empty($value['article_id']) ? 0 : $value['article_id'];
|
|
// if(empty($iArticleId)){
|
|
// continue;
|
|
// }
|
|
|
|
// $sQueueId = \think\Queue::push('app\api\job\ArticleAiCreateContent@fire',$aParam, 'ArticleAiCreateContent');
|
|
// $sQueueId = \think\Queue::push('app\api\job\RecommendReviewer@fire',$aParam, 'RecommendReviewer');
|
|
// $sQueueId = \think\Queue::push('app\api\job\RelatedArticle@fire',$aParam, 'RelatedArticle');
|
|
// $sQueueId = \think\Queue::push('app\api\job\ReviewerScore@fire',$aParam, 'ReviewerScore');
|
|
// $sQueueId = \think\Queue::push('app\api\job\RevisionReviewer@fire',$aParam, 'RevisionReviewer');
|
|
// $sQueueId = \think\Queue::push('app\api\job\SendRelatedArticleEmail@fire',$aParam, 'SendRelatedArticleEmail');
|
|
// $sQueueId = \think\Queue::push('app\api\job\SendReviewEmail@fire',$aParam, 'SendReviewEmail');
|
|
|
|
// $sQueueId = \think\Queue::push('app\api\job\WechatDraft@fire',$aParam, 'WechatDraft');
|
|
|
|
// $sQueueId = \think\Queue::push('app\api\job\WechatDraftPublish@fire',$aParam, 'WechatDraftPublish');
|
|
// $sQueueId = \think\Queue::push('app\api\job\WechatMaterial@fire',$aParam, 'WechatMaterial');
|
|
// $sQueueId = \think\Queue::push('app\api\job\WechatQueryStatus@fire',$aParam, 'WechatQueryStatus');
|
|
|
|
$sQueueId = \think\Queue::push('app\api\job\RecommendReviewer@fire',$aParam, 'RecommendReviewer');
|
|
var_dump($sQueueId);exit;
|
|
// if($sQueueId === false){
|
|
// $sMsg .= '文章入队失败,文章ID:'.$value['article_id'].'['.$value['accept_sn']."]\n";
|
|
// }else{
|
|
// $sMsg .= '文章入队成功,文章ID:'.$value['article_id'].'['.$value['accept_sn']."]\n";
|
|
// }
|
|
// }
|
|
exit($sMsg);
|
|
}
|
|
|
|
}
|