作者 刘锟

update

... ... @@ -8,9 +8,15 @@
namespace App\Console\Commands\Inquiry;
use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Helper\Translate;
use App\Helper\Validate;
use App\Http\Logic\Aside\Optimize\InquiryForwardLogic;
use App\Models\Ai\AiCommand;
use App\Models\Inquiry\InquiryInfo;
use App\Models\Inquiry\InquiryRelayAi;
use App\Models\Project\InquiryFilterConfig;
use App\Models\Project\Project;
use App\Services\InquiryRelayService;
... ... @@ -221,7 +227,59 @@ class SyncInquiryRelay extends Command
$remark = $this->inquiryFilter($unique_sign, $country, $data['ip'], $data['message'], $data['refer'], $data['email'], $data['phone'], $data['name']);
$status = $remark ? InquiryInfo::STATUS_INVALID : InquiryInfo::STATUS_INIT;
$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);
$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);
if ($form_id && $status == InquiryInfo::STATUS_INIT) {
$inquiry_ai_info = InquiryRelayAi::where('form_id', $form_id)->first();
if (!$inquiry_ai_info) {
//AI匹配询盘关键词
$ai_info = AiCommand::select(['ai'])->where('key', 'inquiry_keyword_extract')->first();
if (!$ai_info) {
$this->output('AI重写指令[inquiry_keyword_extract]未配置');
return true;
}
$ai_command = $ai_info['ai'];
$ai_command = str_replace('{title}', $data['title'], $ai_command);
$ai_command = str_replace('{keywords}', $keywords, $ai_command);
$ai_command = str_replace('{content}', $data['message'], $ai_command);
$ai_keyword = Common::deal_str(Gpt::instance()->openai_chat_qqs($ai_command));
if (!$ai_keyword) {
$this->output('询盘ID:' . $form_id . ',AI提取询盘关键词失败');
return true;
}
//关键词查询项目着陆页
$inquiryForwardLogic = new InquiryForwardLogic();
$forward_list = $inquiryForwardLogic->searchKeywords($ai_keyword, 2, 3);
if (empty($forward_list)) {
$this->output('询盘ID:' . $form_id . ',根据关键词[' . $ai_keyword . ']查询项目着陆页为空');
return true;
}
//重写询盘内容
$ai_message_list = [];
for ($i = 0; $i < count($forward_list); $i++) {
$message_re = $inquiryForwardLogic->aiRewrite($data['message']);
if (isset($message_re['ai_message']) && $message_re['ai_message']) {
$ai_message_list[] = $message_re['ai_message'];
}
}
if (empty($ai_message_list)) {
$this->output('询盘ID:' . $form_id . ',AI重写询盘内容失败');
return true;
}
$inquiry_ai_info = new InquiryRelayAi();
$inquiry_ai_info->form_id = $form_id;
$inquiry_ai_info->keywords = $ai_keyword;
$inquiry_ai_info->forward_url = Arr::a2s($forward_list);
$inquiry_ai_info->message_ai = Arr::a2s($ai_message_list);
$inquiry_ai_info->save();
}
}
return true;
}
... ...
... ... @@ -152,7 +152,10 @@ class InquiryForwardController extends BaseController
'type.required' => '搜索类型不能为空',
]);
$lists = $inquiryForwardLogic->searchKeywords();
$keywords = $this->param['keywords'];
$type = $this->param['type'];
$num = $this->param['num'] ?? 3;
$lists = $inquiryForwardLogic->searchKeywords($keywords, $type, $num);
$this->response('success', Code::SUCCESS, $lists);
}
... ... @@ -172,7 +175,8 @@ class InquiryForwardController extends BaseController
'message.required' => '需要重写的文案不能为空',
]);
$data = $inquiryForwardLogic->aiRewrite();
$message = $this->param['message'];
$data = $inquiryForwardLogic->aiRewrite($message);
$this->response('success', Code::SUCCESS, $data);
}
... ...
... ... @@ -240,21 +240,23 @@ class InquiryForwardLogic extends BaseLogic
/**
* 关键词查询项目着陆页
* @param $keywords
* @param $type
* @param $num
* @return int|mixed
* @author Akun
* @date 2025/02/26 17:13
*/
public function searchKeywords()
public function searchKeywords($keywords, $type, $num)
{
$num = $this->param['num'] ?? 3;
$model = new InquiryProjectRoute();
// if ($this->param['type'] == 1) {
// if ($type == 1) {
// //使用全文索引搜索
// $routeQuery = $model->select(['project_id', 'route'])->whereRaw("MATCH(title) AGAINST(? IN BOOLEAN MODE)", [$this->param['keywords']]);
// $routeQuery = $model->select(['project_id', 'route'])->whereRaw("MATCH(title) AGAINST(? IN BOOLEAN MODE)", [$keywords]);
// } else {
//使用like查询
$routeQuery = $model->select(['project_id', 'route'])->where('title', 'like', '%' . $this->param['keywords'] . '%');
$routeQuery = $model->select(['project_id', 'route'])->where('title', 'like', '%' . $keywords . '%');
// }
$re_route = $routeQuery->inRandomOrder()->take(100)->get()->toArray();
... ... @@ -309,13 +311,14 @@ class InquiryForwardLogic extends BaseLogic
/**
* AI重写询盘文案
* @param $in_content
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2025/02/27 10:40
*/
public function aiRewrite()
public function aiRewrite($in_content)
{
$ai_info = AiCommand::select(['ai', 'not_use_probability'])->where('key', 'inquiry_text_rewrite')->first();
if (!$ai_info) {
... ... @@ -324,7 +327,6 @@ class InquiryForwardLogic extends BaseLogic
$ai_command = $ai_info['ai'];
$not_use_probability = $ai_info['not_use_probability'];
$in_content = $this->param['message'];
// 当原始询盘内容长度大于15个字符, 60%几率直接发送原始内容。
if (strlen($in_content) >= 15) {
... ...
... ... @@ -119,7 +119,7 @@ class InquiryInfo extends Base
$self->status = $status;
$self->remark = $remark;
$self->save();
return true;
return $self->id;
} catch (\Exception $e) {
return false;
}
... ...
<?php
namespace App\Models\Inquiry;
use App\Models\Base;
class InquiryRelayAi extends Base
{
protected $table = 'gl_inquiry_relay_ai';
}
... ...