对接OPENAI调整
This commit is contained in:
@@ -32,20 +32,24 @@ class Aitest extends Base
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//查询文章
|
//查询文章及AI审稿
|
||||||
$aWhere = ['article_id' => $aParam['article_id']];
|
$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)){
|
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)){
|
if(!empty($aAiReview)){
|
||||||
return json_encode(array('status' => 1,'msg' => 'AI has been reviewed','data' => $aAiReview));
|
return json_encode(array('status' => 1,'msg' => 'AI has been reviewed','data' => $aAiReview));
|
||||||
}
|
}
|
||||||
|
|
||||||
//根据期刊ID查询期刊信息
|
//根据期刊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)){
|
if(empty($aJournal)){
|
||||||
return json_encode(array('status' => 4,'msg' => 'This article is not associated with a journal' ));
|
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'];//简介
|
$title = empty($aArticle['title']) ? '' : $aArticle['title'];//简介
|
||||||
$abstrart = empty($aArticle['abstrart']) ? '' : $aArticle['abstrart'];//简介
|
$abstrart = empty($aArticle['abstrart']) ? '' : $aArticle['abstrart'];//简介
|
||||||
$keywords = empty($aArticle['keywords']) ? '' : $aArticle['keywords'];//关键词
|
$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('/[\r\n]+/', '', $sContent);
|
||||||
$sContent = preg_replace('/ +/', ' ', $sContent); // 合并连续空格
|
$sContent = preg_replace('/ +/', ' ', $sContent); // 合并连续空格
|
||||||
$aSearch['{content}'] = $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){
|
if($aJournal['journal_id'] == 1){
|
||||||
$aSearch['{journal_name}'] = '传统医学研究';
|
$aSearch['{journal_name}'] = '传统医学研究';
|
||||||
}
|
}
|
||||||
@@ -92,17 +98,12 @@ class Aitest extends Base
|
|||||||
$sJournalContent = empty($aJournalPaperArt['data']) ? '' : implode('', $aJournalPaperArt['data']);
|
$sJournalContent = empty($aJournalPaperArt['data']) ? '' : implode('', $aJournalPaperArt['data']);
|
||||||
$sJournalContent = empty($sJournalContent) ? $aJournal['scope'] : $sJournalContent;
|
$sJournalContent = empty($sJournalContent) ? $aJournal['scope'] : $sJournalContent;
|
||||||
$aSearch['{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']];
|
$oOpenAi = new OpenAi;
|
||||||
$aResult = json_decode($oOpenAi->curlOpenAI($aParam),true);
|
//请求OPENAI接口-重要维度单独请求获取答案
|
||||||
echo '<pre>';var_dump($aResult);exit;
|
$aSearch['question'] = 'aArticleImportantPrompt';
|
||||||
|
$aResult = json_decode($oOpenAi->curlMultiOpenAIImportant($aSearch),true);
|
||||||
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
|
$iStatus = empty($aResult['status']) ? 0 : $aResult['status'];
|
||||||
if($iStatus != 1){
|
if($iStatus != 1){
|
||||||
return json_encode($aResult);
|
return json_encode($aResult);
|
||||||
@@ -112,42 +113,33 @@ echo '<pre>';var_dump($aResult);exit;
|
|||||||
if(empty($aData)){
|
if(empty($aData)){
|
||||||
return json_encode(['status' => 6,'msg' => 'OPENAI returns empty content']);
|
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)){
|
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['article_id'] = $iArticleId;
|
||||||
|
$aData['journal_id'] = $aArticle['journal_id'];
|
||||||
$aResult = $this->addAiReview($aData);
|
$aResult = $this->addAiReview($aData);
|
||||||
return json_encode($aResult);
|
return json_encode($aResult);
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 保留内容中的文字和图片(移除其他HTML标签)
|
|
||||||
* @param string $content 原始富文本内容
|
|
||||||
* @return string 处理后的内容
|
|
||||||
*/
|
|
||||||
private function keepTextAndImages($content)
|
|
||||||
{
|
|
||||||
if (empty($content)) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
// 步骤1:保留<img>标签和文本,移除其他所有HTML标签
|
|
||||||
// 使用strip_tags保留<img>标签,其他标签全部移除
|
|
||||||
// $filtered = strip_tags($content, '<img>');
|
|
||||||
|
|
||||||
// 步骤2:处理特殊字符和冗余空白
|
|
||||||
$filtered = html_entity_decode($content); // 解码HTML实体(如 )
|
|
||||||
$filtered = preg_replace('/\s+/', ' ', $filtered); // 合并连续空白为单个空格
|
|
||||||
$filtered = trim($filtered); // 去除首尾空格
|
|
||||||
|
|
||||||
// 步骤3:修复可能被破坏的<img>标签格式(如属性缺失引号)
|
|
||||||
$filtered = preg_replace('/<img ([^>]+)>/i', '<img $1>', $filtered);
|
|
||||||
$filtered = preg_replace('/src=([^\s>]+)/i', 'src="$1"', $filtered); // 确保src属性带引号
|
|
||||||
|
|
||||||
return $filtered;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -155,58 +147,89 @@ echo '<pre>';var_dump($aResult);exit;
|
|||||||
* @param article_id 文章ID
|
* @param article_id 文章ID
|
||||||
* @param content 内容
|
* @param content 内容
|
||||||
*/
|
*/
|
||||||
protected function addAiReview($aParam = array()){
|
private function addAiReview($aParam = array()){
|
||||||
|
|
||||||
//返回数组
|
$iArticleId = empty($aParam['article_id']) ? 0 : $aParam['article_id'];
|
||||||
$aResult = ['status' => 1,'msg' => 'AI review successful'];
|
if(empty($iArticleId)){
|
||||||
//必填参数验证
|
|
||||||
$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'];
|
return ['status' => 2,'msg' => 'Please select the article to be reviewed'];
|
||||||
}
|
}
|
||||||
if(!Db::name('article_ai_review')->insert($aInsert)){
|
$iJournalId = empty($aParam['journal_id']) ? 0 : $aParam['journal_id'];
|
||||||
$aResult = ['status' => 2,'msg' => 'Failed to add AI audit content'];
|
if(empty($iJournalId)){
|
||||||
|
return ['status' => 2,'msg' => 'The journal to which the article belongs cannot be empty'];
|
||||||
}
|
}
|
||||||
$aResult['data'] = $aInsert;
|
//返回数组
|
||||||
|
$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;
|
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审核内容查询
|
* @title 文章AI审核内容查询
|
||||||
* @param article_id 文章ID
|
* @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'])){
|
if(empty($aParam['article_id'])){
|
||||||
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
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)){
|
if(empty($aArticle)){
|
||||||
return json_encode(array('status' => 3,'msg' => 'No articles requiring review were found' ));
|
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();
|
$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 '<pre>';var_dump($aResult);exit;
|
|||||||
*/
|
*/
|
||||||
public function recommendedReviewer(){
|
public function recommendedReviewer(){
|
||||||
|
|
||||||
|
$oQueueJob = new QueueJob;
|
||||||
//获取参数
|
//获取参数
|
||||||
$aParam = $this->request->post();
|
$aParam = $this->request->post();
|
||||||
if(empty($aParam['article_id'])){
|
if(empty($aParam['article_id'])){
|
||||||
return json_encode(array('status' => 2,'msg' => 'Please select an article' ));
|
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']];
|
// $aWhere = ['article_id' => $aParam['article_id']];
|
||||||
// $aArticle = Db::name('article')->field('article_id,accept_sn')->where($aWhere)->limit(1)->select();
|
// $aArticle = Db::name('article')->field('article_id,accept_sn')->where($aWhere)->limit(1)->select();
|
||||||
|
|||||||
Reference in New Issue
Block a user