作者 赵彬吉

update

@@ -7,18 +7,17 @@ use App\Helper\Common; @@ -7,18 +7,17 @@ use App\Helper\Common;
7 use App\Helper\Translate; 7 use App\Helper\Translate;
8 use App\Http\Controllers\Bside\BaseController; 8 use App\Http\Controllers\Bside\BaseController;
9 use App\Http\Controllers\Bside\:写入日志; 9 use App\Http\Controllers\Bside\:写入日志;
  10 +use App\Http\Logic\Bside\Ai\AiCommandLogic;
  11 +use App\Models\Ai\AiCommand;
10 use App\Models\Ai\AiLog; 12 use App\Models\Ai\AiLog;
11 use App\Models\Project\DeployOptimize; 13 use App\Models\Project\DeployOptimize;
12 use App\Models\Project\Project; 14 use App\Models\Project\Project;
13 15
14 class AiCommandController extends BaseController 16 class AiCommandController extends BaseController
15 { 17 {
16 - //获取文本内容  
17 - public $chat_url = 'v2/openai_chat_qqs';  
18 /** 18 /**
19 - * @name :ai生成  
20 - * @author :liyuhang  
21 - * @method 19 + * @author zbj
  20 + * @date 2023/11/22
22 */ 21 */
23 public function ai_http_post(){ 22 public function ai_http_post(){
24 $this->request->validate([ 23 $this->request->validate([
@@ -28,39 +27,15 @@ class AiCommandController extends BaseController @@ -28,39 +27,15 @@ class AiCommandController extends BaseController
28 'keywords.required' => '关键字不能为空', 27 'keywords.required' => '关键字不能为空',
29 'key.required' => '场景不能为空', 28 'key.required' => '场景不能为空',
30 ]); 29 ]);
31 - #TODO 通过key获取到ai指令对象  
32 - $data = Common::send_openai_msg($this->chat_url,$this->param,$this->companyName($this->param['key'],$this->user['project_id']));  
33 - $data['text'] = Common::deal_keywords($data['text']);  
34 - $data['text'] = Common::deal_str($data['text']); 30 +
  31 + $text = AiCommandLogic::instance()->ai_send();
35 $param = [ 32 $param = [
36 - 'key'=>$this->param['key'],  
37 - 'keywords'=>$this->param['keywords'],  
38 - 'remark'=>json_encode($data) 33 + 'key' => $this->param['key'],
  34 + 'keywords' => $this->param['keywords'],
  35 + 'remark' => $text
39 ]; 36 ];
40 $this->set_ai_log($param); 37 $this->set_ai_log($param);
41 - $this->response('success',Code::SUCCESS,$data);  
42 - }  
43 -  
44 - /**  
45 - * @remark :获取公司英文名称  
46 - * @name :companyName  
47 - * @author :lyh  
48 - * @method :post  
49 - * @time :2023/10/30 11:22  
50 - */  
51 - public function companyName($key,$project_id){  
52 - $data = [  
53 - 'news_remark',  
54 - 'blog_remark',  
55 - 'product_long_description'  
56 - ];  
57 - $projectOptimizeModel = new DeployOptimize();  
58 - $info = $projectOptimizeModel->read(['project_id'=>$project_id],['id','company_en_name','company_en_description']);  
59 - if(in_array($key,$data)){  
60 - return $info['company_en_description'];  
61 - }else{  
62 - return $info['company_en_name'];  
63 - } 38 + $this->response('success', Code::SUCCESS, $text);
64 } 39 }
65 40
66 /** 41 /**
  1 +<?php
  2 +
  3 +namespace App\Http\Logic\Bside\Ai;
  4 +
  5 +use App\Helper\Common;
  6 +use App\Helper\Gpt;
  7 +use App\Helper\Translate;
  8 +use App\Http\Logic\Bside\BaseLogic;
  9 +use App\Models\Ai\AiCommand;
  10 +use App\Models\Project\DeployOptimize;
  11 +use Illuminate\Support\Facades\Cache;
  12 +
  13 +class AiCommandLogic extends BaseLogic
  14 +{
  15 + public function __construct()
  16 + {
  17 + parent::__construct();
  18 + $this->param = $this->requestAll;
  19 + $this->model = new AiCommand();
  20 + }
  21 +
  22 + /**
  23 + * @author zbj
  24 + * @date 2023/11/22
  25 + */
  26 + public function getPrompt($is_batch = 0){
  27 + $ai_command = $this->model->where('key', $this->param['key'])->where('is_batch', $is_batch)->first();
  28 + if(!$ai_command){
  29 + $this->fail('指令不存在');
  30 + }
  31 + $prompt = $ai_command->ai;
  32 +
  33 + if(strpos($prompt, '{keyword}') !== false) {
  34 + $prompt = str_replace('{keyword}', $this->param['keywords'], $prompt);
  35 + }
  36 + if(strpos($prompt, '{company introduction}') !== false) {
  37 + $company_introduction = $this->getDeployOptimize('company_en_description');
  38 + $prompt = str_replace('{company introduction}', $company_introduction, $prompt);
  39 + }
  40 + if(strpos($prompt, '{company name}') !== false) {
  41 + $company_name = $this->getDeployOptimize('company_en_name');
  42 + $prompt = str_replace('{company name}', $company_name, $prompt);
  43 + }
  44 + if(strpos($prompt, '{core keywords 8}') !== false) {
  45 + $main_keywords = $this->getDeployOptimize('main_keywords');
  46 + if ($main_keywords) {
  47 + $main_keywords = explode("\r\n", $main_keywords);
  48 + //随机取
  49 + shuffle($main_keywords);
  50 + $main_keywords = array_slice($main_keywords, 0, 8);
  51 + $main_keywords = implode(",", $main_keywords);
  52 + $prompt = str_replace('{core keywords 8}', $main_keywords, $prompt);
  53 + }
  54 + }
  55 +
  56 + if(trim($ai_command->ai) == '{core keywords 8}'){
  57 + $ai_send = false;
  58 + }else{
  59 + $lang = $this->getLang($this->param['keywords']);
  60 + $prompt .= '.Please answer in ' . ($lang ?: 'English');
  61 + $ai_send = true;
  62 + }
  63 +
  64 + return [
  65 + 'prompt' => $prompt,
  66 + 'scene' => $ai_command->scene,
  67 + 'ai_send' => $ai_send,
  68 + ];
  69 + }
  70 +
  71 + /**
  72 + * @param $content
  73 + * @return string
  74 + * @author zbj
  75 + * @date 2023/11/22
  76 + */
  77 + public function getLang($content){
  78 + $result = Translate::translateSl($content);
  79 + if (isset($result['texts']['sl']) && isset(Translate::$tls_list[$result['texts']['sl']])) {
  80 + $lang = Translate::$tls_list[$result['texts']['sl']]['lang_en'];
  81 + } else {
  82 + $lang = 'English';
  83 + }
  84 + return $lang;
  85 + }
  86 +
  87 + /**
  88 + * @param string $key
  89 + * @return false|mixed|string
  90 + * @author zbj
  91 + * @date 2023/11/22
  92 + */
  93 + public function getDeployOptimize($key = ''){
  94 + $project_id = $this->project['id'];
  95 + $cache_key = 'project_deploy_optimize_info_' . $project_id;
  96 + $info = Cache::get($cache_key);
  97 + if(!$info){
  98 + $projectOptimizeModel = new DeployOptimize();
  99 + $info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'company_en_name', 'company_en_description', 'main_keywords']);
  100 + Cache::put($cache_key, $info, 600);
  101 + }
  102 + if($key){
  103 + return $info[$key] ??'';
  104 + }
  105 + return $info;
  106 + }
  107 +
  108 + /**
  109 + * @param int $is_batch
  110 + * @return array|string|string[]
  111 + * @author zbj
  112 + * @date 2023/11/22
  113 + */
  114 + public function ai_send($is_batch = 0){
  115 + $prompt = $this->getPrompt($is_batch);
  116 + if(!$prompt['ai_send']){
  117 + return $prompt['prompt'];
  118 + }
  119 + $text = Gpt::instance()->openai_chat_qqs($prompt['prompt'], $prompt['scene']);
  120 + $text = Common::deal_keywords($text);
  121 + return Common::deal_str($text);
  122 + }
  123 +}