作者 赵彬吉

update SeoTdk

... ... @@ -6,6 +6,7 @@ use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Models\Ai\AiCommand;
use App\Models\Ai\AiTdkErrorLog;
use App\Models\Com\UpdateNotify;
use App\Models\Domain\DomainInfo;
use App\Models\Mail\Mail;
... ... @@ -271,6 +272,14 @@ class UpdateSeoTdk extends Command
}
Redis::expire($cache_key, 120);
//连续3次失败的 不再处理
$error_log = AiTdkErrorLog::getLog($project_id, $table, $v['id']);
if($error_log){
continue;
}
echo date('Y-m-d H:i:s') . '更新--' . $table . ': 项目id' . $project_id . ':id' . $v['id'] . PHP_EOL;
$v = DB::connection('custom_mysql')->table($table)->where('id', $v['id'])->first();
$v = (array)$v;
... ... @@ -304,7 +313,7 @@ class UpdateSeoTdk extends Command
}elseif ($field_arr[1] == 'description'){
$update[$table]['des']++;
}
$data[$field_arr[0]][$field_arr[1]] = $this->ai_send($prompt);
$data[$field_arr[0]][$field_arr[1]] = $this->ai_send($prompt, $project_id, $table, $v['id']);
}else{
if($field == 'title' || $field == 'seo_title'){
$update[$table]['title']++;
... ... @@ -321,7 +330,7 @@ class UpdateSeoTdk extends Command
if($field == 'keyword_content'){
$update[$table]['keyword_content']++;
}
$data[$field] = $this->ai_send($prompt);
$data[$field] = $this->ai_send($prompt, $project_id, $table, $v['id']);
}
} else {
//直接使用topic
... ... @@ -499,15 +508,29 @@ class UpdateSeoTdk extends Command
* @method :post
* @time :2023/8/19 10:40
*/
public function ai_send($prompt)
public function ai_send($prompt, $project_id, $table, $id)
{
$text = Gpt::instance()->openai_chat_qqs($prompt);
if (!$text) {
$cache_key = "ai_error_times_{$project_id}_{$table}_{$id}";
if (!Cache::get($cache_key)) {
Cache::put($cache_key, 0, 7 * 24 * 3600);
}
$times = Cache::increment($cache_key);
//3次错误 记录下来 后面不处理了
if ($times > 2) {
AiTdkErrorLog::insertLog($project_id, $table, $id, $prompt);
Cache::forget($cache_key);
}
}
$text = Common::deal_keywords($text);
$text = Common::deal_str($text);
//包含这写字 重新生成
if(Str::contains(Str::lower($text), ['[your brand]', '[brand name]'])){
return $this->ai_send($prompt);
return $this->ai_send($prompt, $project_id, $table, $id);
}
return $text;
... ...
<?php
namespace App\Models\Ai;
use App\Models\Base;
class AiTdkErrorLog extends Base
{
//设置关联表名
protected $table = 'gl_ai_tdk_error_log';
//自动维护create_at创建时间 updated_at修改时间
public $timestamps = true;
public static function insertLog($project_id, $table, $table_id, $prompt){
$model = new self();
$model->project_id = $project_id;
$model->table_name = $table;
$model->table_id = $table_id;
$model->prompt = $prompt;
$model->save();
}
public static function getLog($project_id, $table, $table_id){
return self::where('project_id', $project_id)->where('table_name', $table)->where('table_id', $table_id)->first();
}
}
... ...