AiCommandLogic.php 4.0 KB
<?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\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('is_batch', $is_batch)->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, '{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_keywords = $this->getDeployOptimize('main_keywords');
            if ($main_keywords) {
                $main_keywords = explode("\r\n", $main_keywords);
                //随机取
                shuffle($main_keywords);
                $main_keywords = array_slice($main_keywords, 0, 8);
                $main_keywords = implode(", ", $main_keywords);
                $prompt = str_replace('{core keywords 8}', $main_keywords, $prompt);
            }else{
                $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', 'main_keywords']);
            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);
    }
}