From 409853c31f70e3b1b41d9ff39edea1da932500d9 Mon Sep 17 00:00:00 2001 From: chengxl Date: Wed, 23 Jul 2025 22:14:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E5=85=AC=E4=BC=97=E5=8F=B7?= =?UTF-8?q?=E5=86=85=E5=AE=B9=E7=94=9F=E6=88=90=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/api/controller/Aiarticle.php | 170 +++++++++-------------- 1 file changed, 69 insertions(+), 101 deletions(-) diff --git a/application/api/controller/Aiarticle.php b/application/api/controller/Aiarticle.php index b6926a6..8a3a86d 100644 --- a/application/api/controller/Aiarticle.php +++ b/application/api/controller/Aiarticle.php @@ -50,59 +50,10 @@ class Aiarticle extends Base } /** - * @title 请求OPENAI + * @title 生成公微 * @param aSearch array 模版内容 * @param iTemplateId 模版ID */ - private function _createContentForOpenAI($aSearch = [],$sArticleType = 'default',$isTranslate = 2){ - - if(empty($aSearch) || empty($sArticleType)){ - return json_encode(array('status' => 2,'msg' => 'Please select a template or enter the content you want to consult')); - } - //组织参数 - if(in_array($sArticleType, ['Mini Review','Review'])){ - $sArticleType = 'Review'; - }else{ - $sArticleType = 'default'; - } - if($isTranslate == 1){ - $sArticleType = 1; - } - - //获取问答内容 - $oOpenAi = new OpenAi; - if($sArticleType == 'default'){ - $aMessage = $oOpenAi->buildDefaultPrompt($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'],'redis_id' => empty($aSearch['article_id']) ? 0 : $aSearch['article_id']]; - $aResult = json_decode($oOpenAi->createWechatContent($aParam),true); - } - if($sArticleType == 'Review'){ - $aMessage = $oOpenAi->buildReviewPrompt($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'],'redis_id' => empty($aSearch['article_id']) ? 0 : $aSearch['article_id']]; - $aResult = json_decode($oOpenAi->createWechatContent($aParam),true); - } - if($sArticleType == 1){ - $aMessage = $oOpenAi->buildTranslatePrompt($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); - } - - //请求OPENAI接口 - $iStatus = empty($aResult['status']) ? 0 : $aResult['status']; - if($iStatus != 1){ - return json_encode($aResult); - } - return json_encode(array('status' => 1,'msg' => 'Content is being generated, please wait')); - } public function create($aParam = []){ //获取参数 @@ -154,8 +105,7 @@ class Aiarticle extends Base return json_encode(['status' => 5,'msg' => 'The data has been generated, please proceed with the next steps']); } //请求OPENAI - $aResult = json_decode($this->createForOpenAi($aArticleContent,$aAiArticle),true); - return json_encode($aResult); + return $this->createForOpenAi($aArticleContent,$aAiArticle); } @@ -195,12 +145,17 @@ class Aiarticle extends Base $aSearch['{#title_chinese#}'] = $this->basic_html_filter($aArticle['title']); $aSearch['{#journal_content#}'] = $this->basic_html_filter($aJournal['journal_content'] ?? ''); $aSearch['{#journal_content#}'] = trim(trim($aSearch['{#journal_content#}'],'.'),'。'); - - //写入Redis 标识已经开始请求OPENAI处理 - $sArticleType = empty($aArticle['type']) ? 'default' : $aArticle['type']; $aSearch['article_id'] = $iArticleId; - $aResult = $this->_createContentForOpenAI($aSearch,$sArticleType); - return $aResult; + $aSearch['prompt_article_type'] = empty($aArticle['type']) ? 'default' : $aArticle['type']; + //获取问答内容 + $oOpenAi = new OpenAi; + $aMessage = $oOpenAi->buildWechatPrompt($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'],'redis_id' => empty($aSearch['article_id']) ? 0 : $aSearch['article_id']]; + return $oOpenAi->createWechatContent($aParam); } /** @@ -648,11 +603,11 @@ class Aiarticle extends Base //处理通讯作者信息数据 $aAuthorInfo = json_decode($this->dealTemplateAuthor($aAuthor,$sTemplatePath),true); + $aAuthorInfo = empty($aAuthorInfo['data']) ? [] : $aAuthorInfo['data']; $aSearch['{###author_summary###}'] = empty($aAuthorInfo['author_info']) ? '' : $aAuthorInfo['author_info']; $aSearch['{###author_string###}'] = empty($aAuthorInfo['author_string']) ? '' : ',通讯作者为'.$aAuthorInfo['author_string']; - //处理往期推荐数据 $aPreviousRecommend = json_decode($this->dealTemplatePreviousRecommend($aArticle,$sTemplatePath),true); $aSearch['{###previous_recommend_summary###}'] = empty($aPreviousRecommend['data']) ? '' : $aPreviousRecommend['data']; @@ -708,9 +663,10 @@ class Aiarticle extends Base $aSearch['{###journal_icon_details###}'] = str_replace('{###journal_icon###}', $aAiArticle['journal_icon']??'', $sTemplateJournalInfo); } } - + $aSearch['{###covered###}'] = empty($aSearch['{###covered###}']) ? '' : '内容涵盖'.$aSearch['{###covered###}']; + $sTemplate = str_replace(array_keys($aSearch), array_values($aSearch), $sTemplate); - // $this->writeFile($sTemplate,ROOT_PATH.'public/template_info.html'); + $this->writeFile($sTemplate,ROOT_PATH.'public/template_info.html'); return json_encode(['status' => 1, 'msg' => '模版生成成功','data' => ['template' => $sTemplate]]); } @@ -735,58 +691,70 @@ class Aiarticle extends Base } $sAuthorTemplate = file_get_contents($sAuthorTemplatePathAuthor); - //处理模版变量 - $aAuthorSerachInfo = []; - $sAuthorInfo = ''; + //默认头像 $aLogo = $this->aLogo; - - //请求AI翻译 - $sContent = ''; - $aSearch['{#content#}'] = json_encode($aAuthor); - $aResult = json_decode($this->_createContentForOpenAI($aSearch,1,1),true); - $aContent = empty($aResult['data']) ? [] : array_column($aResult['data'], null,'email'); + //数据处理 + $aTranslate = $aAuthorSerachInfo = $aTranslateAuthorList = []; + //通讯作者信息 + $sAuthorInfo = ''; foreach ($aAuthor as $key => $value) { - - //请求AI翻译 - $aInfo = empty($aContent[$value['email']]) ? [] : $aContent[$value['email']]; - - //替换模版 - $aAuthorSerach = []; //所属单位处理 - $value['company'] = empty($aInfo['company']) ? $value['company'] : $aInfo['company']; if(!empty($value['company'])){ $value['company'] =str_replace(',', ',', $value['company']); $aCompany = explode(',', $value['company']); } $value['company'] = empty($aCompany[0]) ? $value['company'] : trim($aCompany[0],','); - //简介处理 - $value['introduction'] = empty($aInfo['introduction']) ? $value['introduction'] : $aInfo['introduction']; - //职称处理 - $value['technical'] = empty($aInfo['technical']) ? $value['technical'] : $aInfo['technical']; + $sAuthorInfo .= $value['company'].$value['author_name'].$value['technical'].','; if(empty($value['introduction'])){//简介为空不处理 - $sTechnical = empty($value['technical']) ? '老师' : $value['technical']; - $sAuthorInfo .= $value['company'].$value['author_name'].$sTechnical.','; continue; } - - //缓缓处理进行模版替换 - foreach ($value as $k => $val) { - if($k == 'icon'){//头像拼接处理 - $sIconInfo = empty($val) ? $aLogo['url'] : $val; - $aParsedUrl = parse_url($sIconInfo); - if(empty($aParsedUrl['scheme'])){ - $aAuthorSerach['{###icon_path###}'] = trim($this->sSubmissionUrl,'/').$this->sUserIcon.$val; - }else{ - $aAuthorSerach['{###icon_path###}'] = $sIconInfo; - } - $aAuthorSerach['{###icon_path###}'] = trim($aAuthorSerach['{###icon_path###}'],'/'); - }else{ - $aAuthorSerach['{###'.$k.'###}'] = trim($val,'/'); - } - } - $aAuthorSerachInfo[] = str_replace(array_keys($aAuthorSerach), array_values($aAuthorSerach), $sAuthorTemplate); + $aTranslateAuthorList[$value['email']] = [ + 'author_name' => empty($value['author_name']) ? '' : $value['author_name'], + 'technical' => empty($value['technical']) ? '' : $value['technical'], + 'introduction' => empty($value['introduction']) ? '' : $value['introduction'], + 'company' => empty($value['company']) ? '' : $value['company'] + ]; + $aAuthorSerachInfo[] = $value; + } + //请求AI翻译 + if(!empty($sAuthorInfo)){ + $aTranslate = array_merge(['author_info' => trim($sAuthorInfo,',')],$aTranslate); + } + if(!empty($aTranslateAuthorList)){ + $aTranslate = array_merge($aTranslateAuthorList,$aTranslate); + } + if(!empty($aTranslate)){ + $aSearch['{#content#}'] = json_encode($aTranslate); + $oOpenAi = new OpenAi; + $aTranslate = $oOpenAi->translate($aSearch); + $aTranslate = empty($aTranslate['data']) ? [] : $aTranslate['data']; + } + //翻译内容处理 + if(!empty($aAuthorSerachInfo)){ + foreach ($aAuthorSerachInfo as $key => $value) { + $aInfo = empty($aTranslate[$value['email']]) ? [] : $aTranslate[$value['email']]; + $value['author_name'] = empty($aInfo['author_name']) ? $value['author_name'] : $aInfo['author_name']; + $value['technical'] = empty($aInfo['technical']) ? $value['technical'] : $aInfo['technical']; + $value['introduction'] = empty($aInfo['introduction']) ? $value['introduction'] : $aInfo['introduction']; + $value['company'] = empty($aInfo['company']) ? $value['company'] : $aInfo['company']; + $aAuthorSerach = []; + foreach ($value as $k => $val) { + if($k == 'icon'){//头像拼接处理 + $sIconInfo = empty($val) ? $aLogo['url'] : $val; + $aParsedUrl = parse_url($sIconInfo); + if(empty($aParsedUrl['scheme'])){ + $aAuthorSerach['{###icon_path###}'] = trim($this->sSubmissionUrl,'/').$this->sUserIcon.$val; + }else{ + $aAuthorSerach['{###icon_path###}'] = $sIconInfo; + } + $aAuthorSerach['{###icon_path###}'] = trim($aAuthorSerach['{###icon_path###}'],'/'); + }else{ + $aAuthorSerach['{###'.$k.'###}'] = trim($val,'/'); + } + } + $aAuthorSerachInfo[$key] = str_replace(array_keys($aAuthorSerach), array_values($aAuthorSerach), $sAuthorTemplate); + } } - //通讯作者列表 //获取通讯作者汇总模版 $sAuthorTemplatePath = $sTemplatePath.'/author_summary.html'; @@ -802,7 +770,7 @@ class Aiarticle extends Base $sAuthorTemplate = empty($sInfo) ? '' : str_replace('{###author_info###}', $sInfo, $sAuthorTemplate); //通讯作者字符串处理 - $sAuthorInfo = trim($sAuthorInfo,','); + $sAuthorInfo = empty($aTranslate['author_info']) ? '' : $aTranslate['author_info']; return json_encode(['status' => 1,'msg' => 'success','data' => ['author_info' => $sAuthorTemplate,'author_string' => trim($sAuthorInfo,';')]]); }