这几天不知道谁无聊,拿我这1ip博客的评论来发广告,就1ip这广告发得一点用没有浪费感情嘛,只是把我弄烦了,要天天上来删广告,所以,简单的过滤一下。
用常规的关键字过滤什么太low了,用ai来识别了。

在var/Widget/Base/Comments.php最后面增加一个函数,让ai为输入的文字的广告味打分,0-9。
这个函数其实用在别的php的系统里也是可以的了,它并没有调用typecho的内部模块啥的。

private function checkAdv(string $inputWords): int {
    $apiKey = "xxxxxxxxx";
    $apiUrl = 'https://api.deepseek.com/v1/chat/completions';   //openai等等相应的改改了

    $systemPrompt = '你是一个广告检测助手。你的任务是对用户输入的文本进行广告嫌疑评分,评分范围为0-9,规则如下:0:无广告嫌疑 5:可能有广告嫌疑(临界值) 9:确定是广告。你只能回答一个0-9的整数,禁止包含任何其他文字或解释。';

    $requestData = [
        'model' => 'deepseek-chat',
        'messages' => [
            [
                'role' => 'system',
                'content' => $systemPrompt
            ],
            [
                'role' => 'user',
                'content' => $inputWords
            ]
        ],
        'max_tokens' => 1, 
        'temperature' => 0.1 
    ];

    $ch = curl_init($apiUrl);
    curl_setopt_array($ch, [
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => json_encode($requestData),
        CURLOPT_HTTPHEADER => [
            'Content-Type: application/json',
            'Authorization: Bearer ' . $apiKey
        ],
        CURLOPT_TIMEOUT => 5
    ]);
    //Author: https://blog.lostshit.com

    $response = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    if (curl_errno($ch)) {
        curl_close($ch);
        return 0; 
    }
    curl_close($ch);

    if ($httpCode === 200) {
        $result = json_decode($response, true);
        
        if (isset($result['choices'][0]['message']['content'])) {
            $score = trim($result['choices'][0]['message']['content']);
            if (is_numeric($score)) {
                return max(0, min(9, (int) $score)); 
            }
        }
    }
    return 0; 
}

然后,在public funtion insert这函数里面,比如 if (!empty($rows['coid'])) 这句前面加上:

        $score=$this->checkAdv($insertStruct['text']);
        if ($score>=5) {
          die("对不起,AI说这是广告。。。如果错了,还请你见谅。");
        }

搞定。

标签: none

仅有一条评论

  1. 稍微优化了一下prompt:
    你是一个广告检测助手。你的任务是对用户输入的文本进行广告嫌疑评分,评分范围为0-9,规则如下:0:无广告嫌疑 5:可能有广告嫌疑(临界值) 9:
    确定是广告。特例,如果是在说服务器和编程和网络相关的问题等打分为0。你只能回答一个0-9的整数,禁止包含任何其他文字或解释。
    因为有些评论会提到其它商家,比如cludflare,就会被判定为广告嫌疑。所以,加个特例。

添加新评论

*如果只是需要与我沟通联系,请telegram @ohyessure, 而不要用评论方式,因为没有你的个人资料,我无法回复及联络你。