作者 赵彬吉

update SeoTdk

@@ -6,6 +6,7 @@ use App\Helper\Arr; @@ -6,6 +6,7 @@ use App\Helper\Arr;
6 use App\Helper\Common; 6 use App\Helper\Common;
7 use App\Helper\Gpt; 7 use App\Helper\Gpt;
8 use App\Models\Ai\AiCommand; 8 use App\Models\Ai\AiCommand;
  9 +use App\Models\Ai\AiTdkErrorLog;
9 use App\Models\Com\UpdateNotify; 10 use App\Models\Com\UpdateNotify;
10 use App\Models\Domain\DomainInfo; 11 use App\Models\Domain\DomainInfo;
11 use App\Models\Mail\Mail; 12 use App\Models\Mail\Mail;
@@ -271,6 +272,14 @@ class UpdateSeoTdk extends Command @@ -271,6 +272,14 @@ class UpdateSeoTdk extends Command
271 } 272 }
272 Redis::expire($cache_key, 120); 273 Redis::expire($cache_key, 120);
273 274
  275 +
  276 + //连续3次失败的 不再处理
  277 + $error_log = AiTdkErrorLog::getLog($project_id, $table, $v['id']);
  278 + if($error_log){
  279 + continue;
  280 + }
  281 +
  282 +
274 echo date('Y-m-d H:i:s') . '更新--' . $table . ': 项目id' . $project_id . ':id' . $v['id'] . PHP_EOL; 283 echo date('Y-m-d H:i:s') . '更新--' . $table . ': 项目id' . $project_id . ':id' . $v['id'] . PHP_EOL;
275 $v = DB::connection('custom_mysql')->table($table)->where('id', $v['id'])->first(); 284 $v = DB::connection('custom_mysql')->table($table)->where('id', $v['id'])->first();
276 $v = (array)$v; 285 $v = (array)$v;
@@ -304,7 +313,7 @@ class UpdateSeoTdk extends Command @@ -304,7 +313,7 @@ class UpdateSeoTdk extends Command
304 }elseif ($field_arr[1] == 'description'){ 313 }elseif ($field_arr[1] == 'description'){
305 $update[$table]['des']++; 314 $update[$table]['des']++;
306 } 315 }
307 - $data[$field_arr[0]][$field_arr[1]] = $this->ai_send($prompt); 316 + $data[$field_arr[0]][$field_arr[1]] = $this->ai_send($prompt, $project_id, $table, $v['id']);
308 }else{ 317 }else{
309 if($field == 'title' || $field == 'seo_title'){ 318 if($field == 'title' || $field == 'seo_title'){
310 $update[$table]['title']++; 319 $update[$table]['title']++;
@@ -321,7 +330,7 @@ class UpdateSeoTdk extends Command @@ -321,7 +330,7 @@ class UpdateSeoTdk extends Command
321 if($field == 'keyword_content'){ 330 if($field == 'keyword_content'){
322 $update[$table]['keyword_content']++; 331 $update[$table]['keyword_content']++;
323 } 332 }
324 - $data[$field] = $this->ai_send($prompt); 333 + $data[$field] = $this->ai_send($prompt, $project_id, $table, $v['id']);
325 } 334 }
326 } else { 335 } else {
327 //直接使用topic 336 //直接使用topic
@@ -499,15 +508,29 @@ class UpdateSeoTdk extends Command @@ -499,15 +508,29 @@ class UpdateSeoTdk extends Command
499 * @method :post 508 * @method :post
500 * @time :2023/8/19 10:40 509 * @time :2023/8/19 10:40
501 */ 510 */
502 - public function ai_send($prompt) 511 + public function ai_send($prompt, $project_id, $table, $id)
503 { 512 {
504 $text = Gpt::instance()->openai_chat_qqs($prompt); 513 $text = Gpt::instance()->openai_chat_qqs($prompt);
  514 +
  515 + if (!$text) {
  516 + $cache_key = "ai_error_times_{$project_id}_{$table}_{$id}";
  517 + if (!Cache::get($cache_key)) {
  518 + Cache::put($cache_key, 0, 7 * 24 * 3600);
  519 + }
  520 + $times = Cache::increment($cache_key);
  521 + //3次错误 记录下来 后面不处理了
  522 + if ($times > 2) {
  523 + AiTdkErrorLog::insertLog($project_id, $table, $id, $prompt);
  524 + Cache::forget($cache_key);
  525 + }
  526 + }
  527 +
505 $text = Common::deal_keywords($text); 528 $text = Common::deal_keywords($text);
506 $text = Common::deal_str($text); 529 $text = Common::deal_str($text);
507 530
508 //包含这写字 重新生成 531 //包含这写字 重新生成
509 if(Str::contains(Str::lower($text), ['[your brand]', '[brand name]'])){ 532 if(Str::contains(Str::lower($text), ['[your brand]', '[brand name]'])){
510 - return $this->ai_send($prompt); 533 + return $this->ai_send($prompt, $project_id, $table, $id);
511 } 534 }
512 535
513 return $text; 536 return $text;
  1 +<?php
  2 +
  3 +namespace App\Models\Ai;
  4 +
  5 +use App\Models\Base;
  6 +
  7 +class AiTdkErrorLog extends Base
  8 +{
  9 + //设置关联表名
  10 + protected $table = 'gl_ai_tdk_error_log';
  11 + //自动维护create_at创建时间 updated_at修改时间
  12 + public $timestamps = true;
  13 +
  14 + public static function insertLog($project_id, $table, $table_id, $prompt){
  15 + $model = new self();
  16 + $model->project_id = $project_id;
  17 + $model->table_name = $table;
  18 + $model->table_id = $table_id;
  19 + $model->prompt = $prompt;
  20 + $model->save();
  21 + }
  22 +
  23 + public static function getLog($project_id, $table, $table_id){
  24 + return self::where('project_id', $project_id)->where('table_name', $table)->where('table_id', $table_id)->first();
  25 + }
  26 +}