小程序使用 openaiwidget 做敏感词过滤
发布于 1 年前 作者 shaowei 1122 次浏览 来自 分享

环境

小程序
openaiwidget 1.2.23

参考

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

使用 openaiwidget 做敏感词过滤

引入插件

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

{
  "pages": [
    "pages/index/index"
  ],
  "plugins": {
    "chatbot": {
      "version": "1.2.23",
      "provider": "wx8c631f7e9f2465e1"
    }
  },
  "requiredBackgroundModes": [
    "audio"
  ],
  "sitemapLocation": "sitemap.json"
}

初始化

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

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

调用敏感内容检测接口

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

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;
}
回到顶部