使用 GPT 对 waline 的评论进行审查

本文最后更新于:2024年10月12日 下午

前一阵子收到了这么一条来自 waline 的评论提醒。

New comment on 竹林里有冰的博客
【网站名称】:竹林里有冰的博客
【评论者昵称】:专业数据库
【评论者邮箱】:rakhiranijhhg@gmail.com
【内容】:总之,优化专业数据库对于保持数据准确性、提高系统性能和推动业务成功至关重要。通过遵循本文中概述的策略,您可以提高数据库操作的效率并释放新的增长机会。
【地址】:https://zhul.in/2021/04/04/yay-more/#66f7a8889ab78865d5f5ae19

评论的内容不仅透露着一股 AI 味,还和文章内容可谓是一点关系都没有,点开评论者的网站一看,一股塑料机翻味,怕是又是个来蹭 SEO 的广告哥。

广告哥留下的网站

根据 waline 的官方文档所言,waline 是使用了 Akismet 提供的垃圾内容检测服务的。可惜它似乎对 AI 生成的垃圾没有分辨能力。因此我计划使用 GPT 代替 Akismet 对 waline 的新评论进行审核。

walinejs/plugin 提供了一个 tencent-cms 的插件,功能是使用腾讯云的内容审查接口审查评论内容,这和我们需要的功能很像,主体部分和调用方法可以直接借鉴。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// index.js

const tencentcloud = require("tencentcloud-sdk-nodejs-tms");
const TmsClient = tencentcloud.tms.v20201229.Client;


module.exports = function({secretId, secretKey, region}) {
if (!secretId || !secretKey || !region) {
return {};
}

const clientConfig = {
credential: {
secretId,
secretKey,
},
region,
profile: {
httpProfile: {
endpoint: "tms.tencentcloudapi.com",
},
},
};

return {
hooks: {
async preSave(data) {
const { userInfo } = this.ctx.state;
const isAdmin = userInfo.type === 'administrator';
// ignore admin comment
if (isAdmin) {
return;
}

const client = new TmsClient(clientConfig);
try {
const resp = await client.TextModeration({ Content: data.comment });
if (!resp.Suggestion) {
throw new Error('Suggestion is empty. Tencent Cloud TMS info:', resp);
}

switch(resp.Suggestion) {
case 'Pass':
data.status = 'approved';
break;
case 'Block':
data.status = 'spam';
break;
case 'Review':
default:
data.status = 'waiting';
break;
}
} catch(e) {
console.log(e);
data.status = 'waiting';
}
},
},
};
}

可以看到,我们需要在这个被 module.exports 导出的函数中,return 一个对象,如果使用 hooks 编写的话可以调用一些生命周期 hook: 在 preSave 阶段,我们可以通过标注 data.status 参数来反馈评论类型。approved 为接受,spam 为垃圾邮件,waiting 为等待人工审核;除此之外,还可以基于 Koa 中间件制作插件,文档中有具体的描述。

index.js 顶部是需要引入的依赖。当然,如果需要引入外部的第三方包的话,需要在 packages.json 中加入需要的依赖(使用包管理器的命令进行安装)。

有了这些基础知识,就能手搓一个基于 GPT 的评论审查插件。

OpenAI 提供的是标准的 Restful API,本身的鉴权逻辑也不复杂,其实没必要调用 SDK,直接使用 fetch 调用就行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const doReview = async (comment) => {
const response = await fetch(openaiBaseUrl + '/v1/chat/completions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${openaiApiKey}`,
},
body: JSON.stringify({
model: openaiModel,
messages: [
{
role: 'system',
content: prompt
},
{
role: 'user',
content: comment,
},
],
}),
});
const data = await response.json();
if (data && data.choices && data.choices.length > 0) {
return data.choices[0].message.content;
} else {
return 'waiting';
}
}

再配合相应的封装,一款基于 GPT 的 waline 评论审核插件就完成了

如何安装

1
npm install waline-plugin-llm-reviewer

如何使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// index.js
const Waline = require('@waline/vercel');
const GPTReviewer = require('waline-plugin-llm-reviewer');

module.exports = Waline({
plugins: [
GptReviewer({
openaiBaseUrl: process.env.OPENAI_BASE_URL,
openaiModel: process.env.OPENAI_MODEL,
openaiApiKey: process.env.OPENAI_API_KEY,
openaiPrompt: process.env.OPENAI_PROMPT,
})
]
});

环境变量

  • ASISMET_KEY: Waline 使用的反垃圾评论服务,建议设置为 false 以禁用
  • OPENAI_BASE_URL: API 基础 URL。例如 https://api.openai.com
  • OPENAI_MODEL: 模型名称。例如 gpt-4o-mini
  • OPENAI_API_KEY: API 密钥。例如 ak-xxxxxx
  • OPENAI_PROMPT(可选): 模型的提示。例如 这是一个评论审查:

在 waline 中设置好对应的环境变量,使用 npm 安装好对应的包,就算大功告成了。


使用 GPT 对 waline 的评论进行审查
https://zhul.in/2024/10/12/use-gpt-to-review-waline-comments/
作者
竹林里有冰
发布于
2024年10月12日
更新于
2024年10月12日
许可协议