AiCommandLogic.php
5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?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 App\Models\Project\ProjectKeyword;
use App\Models\WebSetting\WebLanguage;
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('project_id', $this->user['project_id'] ?? $this->project['id'])->first();
if(!$ai_command){
$ai_command = $this->model->where('key', $this->param['key'])->where('project_id', 0)->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, '{topic}') !== false) {
$prompt = str_replace('{topic}', $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_keyword = $this->getDeployOptimize('main_keyword');
if ($main_keyword) {
$main_keyword = explode("\r\n", $main_keyword);
//随机取
shuffle($main_keyword);
$main_keyword = array_slice($main_keyword, 0, 8);
$main_keyword = implode(", ", $main_keyword);
$prompt = str_replace('{core keywords 8}', $main_keyword, $prompt);
}else{
$prompt = '';
}
}
// 前缀关键词
if(preg_match_all("/\{qz_(\d+)\}/", $prompt, $matches)) {
foreach ($matches[0] as $key=>$val) {
$keyword = getPrefixKeyword($this->user['project_id'], 'prefix', $matches[1][$key]);
if($keyword){
$prompt = str_replace($val, $keyword, $prompt);
}
}
}
// 后缀关键词
if(preg_match_all("/\{hz_(\d+)\}/", $prompt, $matches)) {
foreach ($matches[0] as $key=>$val) {
$keyword = getPrefixKeyword($this->user['project_id'], 'suffix', $matches[1][$key]);
if($keyword){
$prompt = str_replace($val, $keyword, $prompt);
}
}
}
if(strpos($prompt, '{company detail}') !== false) {
$company_introduction = $this->getDeployOptimize('company_en_description');
$prompt = str_replace('{company detail}', $company_introduction, $prompt);
}
if(trim($ai_command->ai) == '{core keywords 8}'){
$ai_send = false;
}else{
$lang = WebLanguage::getLangById($this->project['main_lang_id']??1)['english'] ?? 'English';
$prompt .= '.Please answer in ' . $lang;
$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', 'keyword_prefix', 'keyword_suffix']);
$projectKeywordModel = new ProjectKeyword();
$keywordInfo = $projectKeywordModel->read(['project_id'=>$project_id]);
$info['main_keyword'] = '';
if($keywordInfo === false){
$info['main_keyword'] = $keywordInfo['main_keyword'];
}
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);
}
}