作者 刘锟

update

@@ -8,9 +8,15 @@ @@ -8,9 +8,15 @@
8 8
9 namespace App\Console\Commands\Inquiry; 9 namespace App\Console\Commands\Inquiry;
10 10
  11 +use App\Helper\Arr;
  12 +use App\Helper\Common;
  13 +use App\Helper\Gpt;
11 use App\Helper\Translate; 14 use App\Helper\Translate;
12 use App\Helper\Validate; 15 use App\Helper\Validate;
  16 +use App\Http\Logic\Aside\Optimize\InquiryForwardLogic;
  17 +use App\Models\Ai\AiCommand;
13 use App\Models\Inquiry\InquiryInfo; 18 use App\Models\Inquiry\InquiryInfo;
  19 +use App\Models\Inquiry\InquiryRelayAi;
14 use App\Models\Project\InquiryFilterConfig; 20 use App\Models\Project\InquiryFilterConfig;
15 use App\Models\Project\Project; 21 use App\Models\Project\Project;
16 use App\Services\InquiryRelayService; 22 use App\Services\InquiryRelayService;
@@ -221,7 +227,59 @@ class SyncInquiryRelay extends Command @@ -221,7 +227,59 @@ class SyncInquiryRelay extends Command
221 $remark = $this->inquiryFilter($unique_sign, $country, $data['ip'], $data['message'], $data['refer'], $data['email'], $data['phone'], $data['name']); 227 $remark = $this->inquiryFilter($unique_sign, $country, $data['ip'], $data['message'], $data['refer'], $data['email'], $data['phone'], $data['name']);
222 $status = $remark ? InquiryInfo::STATUS_INVALID : InquiryInfo::STATUS_INIT; 228 $status = $remark ? InquiryInfo::STATUS_INVALID : InquiryInfo::STATUS_INIT;
223 229
224 - $model->createInquiry($data['name'], $data['phone'], $data['email'], $data['ip'], $country, $data['message'], $message_cn, $type, $data['time'], $data['refer'], $data['title'], $keywords, $message_sign, $data['origin_key'], $data['image'], $email_status, $phone_status, $unique_sign, $status, $remark); 230 + $form_id = $model->createInquiry($data['name'], $data['phone'], $data['email'], $data['ip'], $country, $data['message'], $message_cn, $type, $data['time'], $data['refer'], $data['title'], $keywords, $message_sign, $data['origin_key'], $data['image'], $email_status, $phone_status, $unique_sign, $status, $remark);
  231 +
  232 + if ($form_id && $status == InquiryInfo::STATUS_INIT) {
  233 + $inquiry_ai_info = InquiryRelayAi::where('form_id', $form_id)->first();
  234 + if (!$inquiry_ai_info) {
  235 + //AI匹配询盘关键词
  236 + $ai_info = AiCommand::select(['ai'])->where('key', 'inquiry_keyword_extract')->first();
  237 + if (!$ai_info) {
  238 + $this->output('AI重写指令[inquiry_keyword_extract]未配置');
  239 + return true;
  240 + }
  241 +
  242 + $ai_command = $ai_info['ai'];
  243 + $ai_command = str_replace('{title}', $data['title'], $ai_command);
  244 + $ai_command = str_replace('{keywords}', $keywords, $ai_command);
  245 + $ai_command = str_replace('{content}', $data['message'], $ai_command);
  246 + $ai_keyword = Common::deal_str(Gpt::instance()->openai_chat_qqs($ai_command));
  247 + if (!$ai_keyword) {
  248 + $this->output('询盘ID:' . $form_id . ',AI提取询盘关键词失败');
  249 + return true;
  250 + }
  251 +
  252 + //关键词查询项目着陆页
  253 + $inquiryForwardLogic = new InquiryForwardLogic();
  254 + $forward_list = $inquiryForwardLogic->searchKeywords($ai_keyword, 2, 3);
  255 + if (empty($forward_list)) {
  256 + $this->output('询盘ID:' . $form_id . ',根据关键词[' . $ai_keyword . ']查询项目着陆页为空');
  257 + return true;
  258 + }
  259 +
  260 + //重写询盘内容
  261 + $ai_message_list = [];
  262 + for ($i = 0; $i < count($forward_list); $i++) {
  263 + $message_re = $inquiryForwardLogic->aiRewrite($data['message']);
  264 + if (isset($message_re['ai_message']) && $message_re['ai_message']) {
  265 + $ai_message_list[] = $message_re['ai_message'];
  266 + }
  267 + }
  268 + if (empty($ai_message_list)) {
  269 + $this->output('询盘ID:' . $form_id . ',AI重写询盘内容失败');
  270 + return true;
  271 + }
  272 +
  273 +
  274 + $inquiry_ai_info = new InquiryRelayAi();
  275 + $inquiry_ai_info->form_id = $form_id;
  276 + $inquiry_ai_info->keywords = $ai_keyword;
  277 + $inquiry_ai_info->forward_url = Arr::a2s($forward_list);
  278 + $inquiry_ai_info->message_ai = Arr::a2s($ai_message_list);
  279 + $inquiry_ai_info->save();
  280 + }
  281 + }
  282 +
225 return true; 283 return true;
226 } 284 }
227 285
@@ -152,7 +152,10 @@ class InquiryForwardController extends BaseController @@ -152,7 +152,10 @@ class InquiryForwardController extends BaseController
152 'type.required' => '搜索类型不能为空', 152 'type.required' => '搜索类型不能为空',
153 ]); 153 ]);
154 154
155 - $lists = $inquiryForwardLogic->searchKeywords(); 155 + $keywords = $this->param['keywords'];
  156 + $type = $this->param['type'];
  157 + $num = $this->param['num'] ?? 3;
  158 + $lists = $inquiryForwardLogic->searchKeywords($keywords, $type, $num);
156 $this->response('success', Code::SUCCESS, $lists); 159 $this->response('success', Code::SUCCESS, $lists);
157 } 160 }
158 161
@@ -172,7 +175,8 @@ class InquiryForwardController extends BaseController @@ -172,7 +175,8 @@ class InquiryForwardController extends BaseController
172 'message.required' => '需要重写的文案不能为空', 175 'message.required' => '需要重写的文案不能为空',
173 ]); 176 ]);
174 177
175 - $data = $inquiryForwardLogic->aiRewrite(); 178 + $message = $this->param['message'];
  179 + $data = $inquiryForwardLogic->aiRewrite($message);
176 $this->response('success', Code::SUCCESS, $data); 180 $this->response('success', Code::SUCCESS, $data);
177 } 181 }
178 182
@@ -240,21 +240,23 @@ class InquiryForwardLogic extends BaseLogic @@ -240,21 +240,23 @@ class InquiryForwardLogic extends BaseLogic
240 240
241 /** 241 /**
242 * 关键词查询项目着陆页 242 * 关键词查询项目着陆页
  243 + * @param $keywords
  244 + * @param $type
  245 + * @param $num
243 * @return int|mixed 246 * @return int|mixed
244 * @author Akun 247 * @author Akun
245 * @date 2025/02/26 17:13 248 * @date 2025/02/26 17:13
246 */ 249 */
247 - public function searchKeywords() 250 + public function searchKeywords($keywords, $type, $num)
248 { 251 {
249 - $num = $this->param['num'] ?? 3;  
250 252
251 $model = new InquiryProjectRoute(); 253 $model = new InquiryProjectRoute();
252 -// if ($this->param['type'] == 1) { 254 +// if ($type == 1) {
253 // //使用全文索引搜索 255 // //使用全文索引搜索
254 -// $routeQuery = $model->select(['project_id', 'route'])->whereRaw("MATCH(title) AGAINST(? IN BOOLEAN MODE)", [$this->param['keywords']]); 256 +// $routeQuery = $model->select(['project_id', 'route'])->whereRaw("MATCH(title) AGAINST(? IN BOOLEAN MODE)", [$keywords]);
255 // } else { 257 // } else {
256 //使用like查询 258 //使用like查询
257 - $routeQuery = $model->select(['project_id', 'route'])->where('title', 'like', '%' . $this->param['keywords'] . '%'); 259 + $routeQuery = $model->select(['project_id', 'route'])->where('title', 'like', '%' . $keywords . '%');
258 // } 260 // }
259 261
260 $re_route = $routeQuery->inRandomOrder()->take(100)->get()->toArray(); 262 $re_route = $routeQuery->inRandomOrder()->take(100)->get()->toArray();
@@ -309,13 +311,14 @@ class InquiryForwardLogic extends BaseLogic @@ -309,13 +311,14 @@ class InquiryForwardLogic extends BaseLogic
309 311
310 /** 312 /**
311 * AI重写询盘文案 313 * AI重写询盘文案
  314 + * @param $in_content
312 * @return array 315 * @return array
313 * @throws \App\Exceptions\AsideGlobalException 316 * @throws \App\Exceptions\AsideGlobalException
314 * @throws \App\Exceptions\BsideGlobalException 317 * @throws \App\Exceptions\BsideGlobalException
315 * @author Akun 318 * @author Akun
316 * @date 2025/02/27 10:40 319 * @date 2025/02/27 10:40
317 */ 320 */
318 - public function aiRewrite() 321 + public function aiRewrite($in_content)
319 { 322 {
320 $ai_info = AiCommand::select(['ai', 'not_use_probability'])->where('key', 'inquiry_text_rewrite')->first(); 323 $ai_info = AiCommand::select(['ai', 'not_use_probability'])->where('key', 'inquiry_text_rewrite')->first();
321 if (!$ai_info) { 324 if (!$ai_info) {
@@ -324,7 +327,6 @@ class InquiryForwardLogic extends BaseLogic @@ -324,7 +327,6 @@ class InquiryForwardLogic extends BaseLogic
324 327
325 $ai_command = $ai_info['ai']; 328 $ai_command = $ai_info['ai'];
326 $not_use_probability = $ai_info['not_use_probability']; 329 $not_use_probability = $ai_info['not_use_probability'];
327 - $in_content = $this->param['message'];  
328 330
329 // 当原始询盘内容长度大于15个字符, 60%几率直接发送原始内容。 331 // 当原始询盘内容长度大于15个字符, 60%几率直接发送原始内容。
330 if (strlen($in_content) >= 15) { 332 if (strlen($in_content) >= 15) {
@@ -119,7 +119,7 @@ class InquiryInfo extends Base @@ -119,7 +119,7 @@ class InquiryInfo extends Base
119 $self->status = $status; 119 $self->status = $status;
120 $self->remark = $remark; 120 $self->remark = $remark;
121 $self->save(); 121 $self->save();
122 - return true; 122 + return $self->id;
123 } catch (\Exception $e) { 123 } catch (\Exception $e) {
124 return false; 124 return false;
125 } 125 }
  1 +<?php
  2 +
  3 +namespace App\Models\Inquiry;
  4 +
  5 +use App\Models\Base;
  6 +
  7 +class InquiryRelayAi extends Base
  8 +{
  9 + protected $table = 'gl_inquiry_relay_ai';
  10 +}