diff --git a/plugins/talk-plugin-toxic-comments/README.md b/plugins/talk-plugin-toxic-comments/README.md index f158a5eb3..e649f5832 100644 --- a/plugins/talk-plugin-toxic-comments/README.md +++ b/plugins/talk-plugin-toxic-comments/README.md @@ -28,3 +28,4 @@ Configuration: [ms](https://www.npmjs.com/package/ms). (Default `300ms`) - `TALK_PERSPECTIVE_DO_NOT_STORE` - Whether the API stores or deletes the comment text and context from this request after it has been evaluated. Stored comments will be used for future research and community model building purposes to improve the API over time. (Default `true`) [Perspective API - Analyze Comment Request](https://github.com/conversationai/perspectiveapi/blob/master/api_reference.md#analyzecomment-request) - `TALK_PERSPECTIVE_SEND_FEEDBACK` - If set to `TRUE`, this plugin will send back moderation actions as feedback to [Perspective](http://perspectiveapi.com/) to improve their model. (Default `FALSE`) +- `TALK_PERSPECTIVE_MODEL` - Determines the Perspective API toxicity model that should be used, i.e. `TOXICITY` vs `SEVERE_TOXICITY`. A list of available models provided by the Perspective API can be found [here](https://github.com/conversationai/perspectiveapi/blob/master/api_reference.md#models). When displaying the toxicity score, this model will be used by default. If this model isn't available on the comment metadata (such as when the model has been changed), it will fall back to the stored `TOXICITY` model as Talk will always fetch that. (Default `SEVERE_TOXICITY`) diff --git a/plugins/talk-plugin-toxic-comments/server/config.js b/plugins/talk-plugin-toxic-comments/server/config.js index cc0c2daa0..1987049cb 100644 --- a/plugins/talk-plugin-toxic-comments/server/config.js +++ b/plugins/talk-plugin-toxic-comments/server/config.js @@ -9,6 +9,7 @@ const config = { API_TIMEOUT: ms(process.env.TALK_PERSPECTIVE_TIMEOUT || '300ms'), DO_NOT_STORE: process.env.TALK_PERSPECTIVE_DO_NOT_STORE || true, SEND_FEEDBACK: process.env.TALK_PERSPECTIVE_SEND_FEEDBACK === 'TRUE', + API_MODEL: process.env.TALK_PERSPECTIVE_MODEL || 'SEVERE_TOXICITY', }; if (process.env.NODE_ENV !== 'test' && !config.API_KEY) { diff --git a/plugins/talk-plugin-toxic-comments/server/perspective.js b/plugins/talk-plugin-toxic-comments/server/perspective.js index db6d0c8de..773c097fb 100644 --- a/plugins/talk-plugin-toxic-comments/server/perspective.js +++ b/plugins/talk-plugin-toxic-comments/server/perspective.js @@ -5,8 +5,10 @@ const { THRESHOLD, API_TIMEOUT, DO_NOT_STORE, + API_MODEL, } = require('./config'); const debug = require('debug')('talk:plugin:toxic-comments'); +const get = require('lodash/get'); // Load the global Talk configuration, we want to grab some variables.. const { ROOT_URL } = require('config'); @@ -55,7 +57,7 @@ async function getScores(text) { doNotStore: DO_NOT_STORE, requestedAttributes: { TOXICITY: {}, - SEVERE_TOXICITY: {}, + [API_MODEL]: {}, }, }); if (!data || data.error) { @@ -64,7 +66,7 @@ async function getScores(text) { TOXICITY: { summaryScore: null, }, - SEVERE_TOXICITY: { + [API_MODEL]: { summaryScore: null, }, }; @@ -74,20 +76,21 @@ async function getScores(text) { TOXICITY: { summaryScore: data.attributeScores.TOXICITY.summaryScore.value, }, - SEVERE_TOXICITY: { - summaryScore: data.attributeScores.SEVERE_TOXICITY.summaryScore.value, + [API_MODEL]: { + summaryScore: data.attributeScores[API_MODEL].summaryScore.value, }, }; } /** - * Get toxicity probability + * Get toxicity probability from the scores, trying first the selection model, + * but falling back to the `TOXICITY` model when the selected model isn't found. * * @param {object} scores scores as returned by `getScores` * @return {number} toxicity probability from 0 - 1.0 */ function getProbability(scores) { - return scores.SEVERE_TOXICITY.summaryScore; + return get(scores, API_MODEL, scores.TOXICITY).summaryScore; } /** diff --git a/plugins/talk-plugin-toxic-comments/server/resolvers.js b/plugins/talk-plugin-toxic-comments/server/resolvers.js index 8143b446e..5108c07bc 100644 --- a/plugins/talk-plugin-toxic-comments/server/resolvers.js +++ b/plugins/talk-plugin-toxic-comments/server/resolvers.js @@ -1,8 +1,15 @@ const get = require('lodash/get'); +const { API_MODEL } = require('./config'); module.exports = { Comment: { toxicity: comment => - get(comment, 'metadata.perspective.SEVERE_TOXICITY.summaryScore'), + // Try to get the score from the custom model first, but fall back to the + // TOXICITY score if it isn't provided. + get( + comment, + `metadata.perspective.${API_MODEL}.summaryScore`, + get(comment, 'metadata.perspective.TOXICITY.summaryScore') + ), }, };