Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/nsfw text check plugin #1

Merged
merged 6 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'washyourmouthoutwithsoap';
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@types/mustache": "^4.2.5",
"async-retry": "^1.3.3",
"hono": "^3.12.0",
"washyourmouthoutwithsoap": "^1.0.2",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions plugins/.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'washyourmouthoutwithsoap';
14 changes: 14 additions & 0 deletions plugins/default/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,20 @@
}
},
{
"name": "NSFW Checker",
"id": "nsfwChecker",
"type": "guardrail",
"supportedHooks": ["afterRequestHook"],
"description": [
{
"type": "subHeading",
"text": "Checks if the content contains NSFW words."
}
],
"parameters": {
"type": "object",
"properties": {}
},
"name": "Lowercase check",
"id": "alllowercase",
"type": "guardrail",
Expand Down
40 changes: 3 additions & 37 deletions plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,7 @@ import { handler as portkeymoderateContent } from './portkey/moderateContent';
import { handler as portkeylanguage } from './portkey/language';
import { handler as portkeypii } from './portkey/pii';
import { handler as portkeygibberish } from './portkey/gibberish';
import { handler as aporiavalidateProject } from './aporia/validateProject';
import { handler as sydelabssydeguard } from './sydelabs/sydeguard';
import { handler as pillarscanPrompt } from './pillar/scanPrompt';
import { handler as pillarscanResponse } from './pillar/scanResponse';
import { handler as patronusphi } from './patronus/phi';
import { handler as patronuspii } from './patronus/pii';
import { handler as patronusisConcise } from './patronus/isConcise';
import { handler as patronusisHelpful } from './patronus/isHelpful';
import { handler as patronusisPolite } from './patronus/isPolite';
import { handler as patronusnoApologies } from './patronus/noApologies';
import { handler as patronusnoGenderBias } from './patronus/noGenderBias';
import { handler as patronusnoRacialBias } from './patronus/noRacialBias';
import { handler as patronusretrievalAnswerRelevance } from './patronus/retrievalAnswerRelevance';
import { handler as patronustoxicity } from './patronus/toxicity';
import { handler as patronuscustom } from './patronus/custom';
import { handler as nsfwCheckernsfwChecker } from './nsfw/nsfwChecker';

export const plugins = {
default: {
Expand All @@ -55,27 +41,7 @@ export const plugins = {
pii: portkeypii,
gibberish: portkeygibberish,
},
aporia: {
validateProject: aporiavalidateProject,
},
sydelabs: {
sydeguard: sydelabssydeguard,
},
pillar: {
scanPrompt: pillarscanPrompt,
scanResponse: pillarscanResponse,
},
patronus: {
phi: patronusphi,
pii: patronuspii,
isConcise: patronusisConcise,
isHelpful: patronusisHelpful,
isPolite: patronusisPolite,
noApologies: patronusnoApologies,
noGenderBias: patronusnoGenderBias,
noRacialBias: patronusnoRacialBias,
retrievalAnswerRelevance: patronusretrievalAnswerRelevance,
toxicity: patronustoxicity,
custom: patronuscustom,
nsfw: {
nsfwChecker: nsfwCheckernsfwChecker,
},
};
18 changes: 18 additions & 0 deletions plugins/nsfw/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "nsfw",
"id": "nsfwChecker",
"functions": [
{
"name": "nsfw",
"id": "nsfwChecker",
"supportedHooks": ["afterRequestHook"],
"type": "guardrail",
"description": [
{
"type": "subHeading",
"text": "Checks the response for NSFW content."
}
]
}
]
}
67 changes: 67 additions & 0 deletions plugins/nsfw/nsfw.tests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// plugins/nsfw/nsfw.tests.ts
import { handler as nsfwHandler } from './nsfwChecker';
import { HookEventType, PluginContext, PluginParameters } from '../types';

describe('NSFW Handler', () => {
it('should return true for NSFW content', async () => {
const context: PluginContext = {
response: {
text: 'They are such a boob at this, they suck ass!',
},
};
const eventType: HookEventType = 'afterRequestHook';
const parameters: PluginParameters = {};

const result = await nsfwHandler(context, parameters, eventType);
expect(result).toBeDefined();
expect(result.verdict).toBe(true);
expect(result.error).toBeNull();
expect(result.data).toBeDefined();
});

it('should return false for non-NSFW content', async () => {
const context: PluginContext = {
response: {
text: 'This is a test string with no NSFW content',
},
};
const eventType: HookEventType = 'afterRequestHook';
const parameters: PluginParameters = {};

const result = await nsfwHandler(context, parameters, eventType);
expect(result).toBeDefined();
expect(result.verdict).toBe(false);
expect(result.error).toBeNull();
expect(result.data).toBeDefined();
});

it('should return true for non-NSFW content', async () => {
const context: PluginContext = {
response: {
text: 'They are such a boob at this, they suck ass!',
},
};
const eventType: HookEventType = 'afterRequestHook';
const parameters: PluginParameters = {};

const result = await nsfwHandler(context, parameters, eventType);
expect(result).toBeDefined();
expect(result.verdict).toBe(true);
expect(result.error).toBeNull();
expect(result.data).toBeDefined();
});

it('should return an error for missing response text', async () => {
const context: PluginContext = {
response: {},
};
const eventType: HookEventType = 'afterRequestHook';
const parameters: PluginParameters = {};

const result = await nsfwHandler(context, parameters, eventType);
expect(result).toBeDefined();
expect(result.error).toBeDefined();
expect(result.verdict).toBeUndefined();
expect(result.data).toBeNull();
});
});
24 changes: 24 additions & 0 deletions plugins/nsfw/nsfwChecker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {
HookEventType,
PluginContext,
PluginHandler,
PluginParameters,
} from '../types';
import { getText } from '../utils';
import * as wash from 'washyourmouthoutwithsoap';

export const handler: PluginHandler = async (
context: PluginContext,
parameters: PluginParameters,
eventType: HookEventType,
options: any
) => {
const responseText = getText(context.response, eventType);
const containsNsfw = wash.check('en', responseText);

return {
verdict: containsNsfw,
data: null,
error: null,
};
};