-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from alexdeploy/moderation
Added prompt simple moderation for violent content.
- Loading branch information
Showing
4 changed files
with
110 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const { describe, it, expect } = require('@jest/globals'); | ||
const { moderationCheck } = require('../src/utils/security'); | ||
|
||
describe('Security check:', () => { | ||
|
||
const response = { | ||
data: { | ||
"id": "modr-XXXXX", | ||
"model": "text-moderation-001", | ||
"results": [ | ||
{ | ||
"categories": { | ||
"hate": false, | ||
"hate/threatening": false, | ||
"self-harm": false, | ||
"sexual": false, | ||
"sexual/minors": false, | ||
"violence": false, | ||
"violence/graphic": false | ||
}, | ||
"category_scores": { | ||
"hate": 0.18805529177188873, | ||
"hate/threatening": 0.0001250059431185946, | ||
"self-harm": 0.0003706029092427343, | ||
"sexual": 0.0008735615410842001, | ||
"sexual/minors": 0.0007470346172340214, | ||
"violence": 0.0041268812492489815, | ||
"violence/graphic": 0.00023186142789199948 | ||
}, | ||
"flagged": false | ||
} | ||
] | ||
} | ||
} | ||
|
||
it('should return true only if all categories are false', async () => { | ||
|
||
const result = await moderationCheck(response.data); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return false if any category is true', async () => { | ||
response.data.results[0].categories.hate = true; | ||
const result = await moderationCheck(response.data); | ||
expect(result).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* MODERATION RESPONSE CHECK | ||
* * Checks for any content policy violations in the response of OpenAI's Content Policy moderation. | ||
* @param {*} moderation is an response object of OpenAI's Content Policy moderation. | ||
* @see Moderation https://platform.openai.com/docs/guides/moderation/moderation | ||
* @returns true if the moderation is safe, false if it violates the Content Policy. | ||
*/ | ||
|
||
const moderationCheck = async (moderation) => { | ||
|
||
// moderation results | ||
const category_scores = moderation.results[0].category_scores; | ||
const categories = moderation.results[0].categories; | ||
|
||
const someIsTrue = Object.values(categories).some(valor => valor === true); | ||
|
||
if(someIsTrue) return false; | ||
|
||
return true; | ||
} | ||
|
||
module.exports = { | ||
moderationCheck | ||
} |