小程序使用 openaiwidget 做敏感词检测

环境

小程序
openaiwidget 1.2.23

参考

1 敏感内容检测
2 openaiwidget:小程序后台->设置->第三方设置->插件管理->添加插件->搜索“openaiwidget”
3 文本内容安全识别

概述

同时使用小程序服务端 api(文本内容安全识别)和插件(openaiwidget)做敏感词检测。

api 的调用不记录。

使用 openaiwidget 做敏感词过滤

引入插件

修改小程序根目录的 app.json,添加 chatbot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"pages": [
"pages/index/index"
],
"plugins": {
"chatbot": {
"version": "1.2.23",
"provider": "wx8c631f7e9f2465e1"
}
},
"requiredBackgroundModes": [
"audio"
],
"sitemapLocation": "sitemap.json"
}

初始化

修改小程序根目录的 app.js

1
2
3
4
5
6
7
8
9
10
11
onLaunch() {
this.initPlugin();
},
initPlugin() {
plugin.init({
appid: "P5Ot9PHJDechCYqDFAW1AiK6OtG3Ja", //小程序示例账户,仅供学习和参考
openid: "oB6jg6ENstneouhXefbujwJl7v2n", //用户的openid,非必填,建议传递该参数
success: () => {}, //非必填
fail: (error) => {}, //非必填
});
},

调用敏感内容检测接口

判断是否敏感依据如何判断是否为敏感内容,我定 other 小于 0.9 即为敏感。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
var plugin = requirePlugin("chatbot");

function check(inputWord) {
let isSensitive = false;
plugin.api.nlp('sensitive', {q: inputWord, mode: 'cnn'}).then(res => {
isSensitive = this.checkIsSensitive(res);
if (isSensitive) {
console.debug('输入的内容'+inputWord+'敏感');
return;
}
});
}

function checkIsSensitive(res) {
let isSensitive = false;
for (let i = 0; i < res.result.length; i++) {
if (res.result[i][0] === 'other' && res.result[i][1] < 0.9) {
isSensitive = true;
break;
}
}
return isSensitive;
}

小程序使用 openaiwidget 做敏感词检测
https://hutaoren.cn/2022/11/11/小程序使用 openaiwidget 做敏感词检测/
作者
胡桃仁
发布于
2022年11月11日
许可协议