作者 lyh
... ... @@ -7,18 +7,17 @@ use App\Helper\Common;
use App\Helper\Translate;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Controllers\Bside\:写入日志;
use App\Http\Logic\Bside\Ai\AiCommandLogic;
use App\Models\Ai\AiCommand;
use App\Models\Ai\AiLog;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
class AiCommandController extends BaseController
{
//获取文本内容
public $chat_url = 'v2/openai_chat_qqs';
/**
* @name :ai生成
* @author :liyuhang
* @method
* @author zbj
* @date 2023/11/22
*/
public function ai_http_post(){
$this->request->validate([
... ... @@ -28,39 +27,15 @@ class AiCommandController extends BaseController
'keywords.required' => '关键字不能为空',
'key.required' => '场景不能为空',
]);
#TODO 通过key获取到ai指令对象
$data = Common::send_openai_msg($this->chat_url,$this->param,$this->companyName($this->param['key'],$this->user['project_id']));
$data['text'] = Common::deal_keywords($data['text']);
$data['text'] = Common::deal_str($data['text']);
$text = AiCommandLogic::instance()->ai_send();
$param = [
'key'=>$this->param['key'],
'keywords'=>$this->param['keywords'],
'remark'=>json_encode($data)
'key' => $this->param['key'],
'keywords' => $this->param['keywords'],
'remark' => $text
];
$this->set_ai_log($param);
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :获取公司英文名称
* @name :companyName
* @author :lyh
* @method :post
* @time :2023/10/30 11:22
*/
public function companyName($key,$project_id){
$data = [
'news_remark',
'blog_remark',
'product_long_description'
];
$projectOptimizeModel = new DeployOptimize();
$info = $projectOptimizeModel->read(['project_id'=>$project_id],['id','company_en_name','company_en_description']);
if(in_array($key,$data)){
return $info['company_en_description'];
}else{
return $info['company_en_name'];
}
$this->response('success', Code::SUCCESS, ['text' => $text]);
}
/**
... ...
<?php
namespace App\Http\Logic\Bside\Ai;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Helper\Translate;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Ai\AiCommand;
use App\Models\Project\DeployOptimize;
use Illuminate\Support\Facades\Cache;
class AiCommandLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new AiCommand();
}
/**
* @author zbj
* @date 2023/11/22
*/
public function getPrompt($is_batch = 0){
$ai_command = $this->model->where('key', $this->param['key'])->where('is_batch', $is_batch)->first();
if(!$ai_command){
$this->fail('指令不存在');
}
$prompt = $ai_command->ai;
if(strpos($prompt, '{keyword}') !== false) {
$prompt = str_replace('{keyword}', $this->param['keywords'], $prompt);
}
if(strpos($prompt, '{company introduction}') !== false) {
$company_introduction = $this->getDeployOptimize('company_en_description');
$prompt = str_replace('{company introduction}', $company_introduction, $prompt);
}
if(strpos($prompt, '{company name}') !== false) {
$company_name = $this->getDeployOptimize('company_en_name');
$prompt = str_replace('{company name}', $company_name, $prompt);
}
if(strpos($prompt, '{core keywords 8}') !== false) {
$main_keywords = $this->getDeployOptimize('main_keywords');
if ($main_keywords) {
$main_keywords = explode("\r\n", $main_keywords);
//随机取
shuffle($main_keywords);
$main_keywords = array_slice($main_keywords, 0, 8);
$main_keywords = implode(",", $main_keywords);
$prompt = str_replace('{core keywords 8}', $main_keywords, $prompt);
}
}
if(trim($ai_command->ai) == '{core keywords 8}'){
$ai_send = false;
}else{
$lang = $this->getLang($this->param['keywords']);
$prompt .= '.Please answer in ' . ($lang ?: 'English');
$ai_send = true;
}
return [
'prompt' => $prompt,
'scene' => $ai_command->scene,
'ai_send' => $ai_send,
];
}
/**
* @param $content
* @return string
* @author zbj
* @date 2023/11/22
*/
public function getLang($content){
$result = Translate::translateSl($content);
if (isset($result['texts']['sl']) && isset(Translate::$tls_list[$result['texts']['sl']])) {
$lang = Translate::$tls_list[$result['texts']['sl']]['lang_en'];
} else {
$lang = 'English';
}
return $lang;
}
/**
* @param string $key
* @return false|mixed|string
* @author zbj
* @date 2023/11/22
*/
public function getDeployOptimize($key = ''){
$project_id = $this->project['id'];
$cache_key = 'project_deploy_optimize_info_' . $project_id;
$info = Cache::get($cache_key);
if(!$info){
$projectOptimizeModel = new DeployOptimize();
$info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'company_en_name', 'company_en_description', 'main_keywords']);
Cache::put($cache_key, $info, 600);
}
if($key){
return $info[$key] ??'';
}
return $info;
}
/**
* @param int $is_batch
* @return array|string|string[]
* @author zbj
* @date 2023/11/22
*/
public function ai_send($is_batch = 0){
$prompt = $this->getPrompt($is_batch);
if(!$prompt['ai_send']){
return $prompt['prompt'];
}
$text = Gpt::instance()->openai_chat_qqs($prompt['prompt'], $prompt['scene']);
$text = Common::deal_keywords($text);
return Common::deal_str($text);
}
}
... ...