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

[New plugin] Simple plugin to allow whitelisting models as a guardrail #695

Merged
merged 2 commits into from
Nov 8, 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
34 changes: 34 additions & 0 deletions plugins/default/default.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { handler as logHandler } from './log';
import { handler as allUppercaseHandler } from './alluppercase';
import { handler as endsWithHandler } from './endsWith';
import { handler as allLowerCaseHandler } from './alllowercase';
import { handler as modelWhitelistHandler } from './modelWhitelist';

import { z } from 'zod';
import { PluginContext, PluginParameters } from '../types';
Expand Down Expand Up @@ -802,3 +803,36 @@ describe('allLowercase handler', () => {
expect(result.verdict).toBe(false);
});
});

describe('modelWhitelist handler', () => {
it('should return true verdict when the model requested is part of the whitelist', async () => {
const context: PluginContext = {
request: { json: { model: 'gemini-1.5-flash-001' } },
};

const parameters: PluginParameters = {
models: ['gemini-1.5-flash-001'],
};
const eventType = 'beforeRequestHook';

const result = await modelWhitelistHandler(context, parameters, eventType);

expect(result.error).toBe(null);
expect(result.verdict).toBe(true);
});
it('should return false verdict when the model requested is not part of the whitelist', async () => {
const context: PluginContext = {
request: { json: { model: 'gemini-1.5-pro-001' } },
};

const parameters: PluginParameters = {
models: ['gemini-1.5-flash-001'],
};
const eventType = 'beforeRequestHook';

const result = await modelWhitelistHandler(context, parameters, eventType);

expect(result.error).toBe(null);
expect(result.verdict).toBe(false);
});
});
31 changes: 31 additions & 0 deletions plugins/default/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,37 @@
}
],
"parameters": {}
},
{
"name": "Model whitelisting",
"id": "modelwhitelist",
"type": "guardrail",
"supportedHooks": ["beforeRequestHook"],
"description": [
{
"type": "subHeading",
"text": "Check if the model in the request is part of the allowed model list."
}
],
"parameters": {
"type": "object",
"properties": {
"models": {
"type": "array",
"label": "Model list",
"description": [
{
"type": "subHeading",
"text": "Enter the allowed models."
}
],
"items": {
"type": "string"
}
}
},
"required": ["models"]
}
}
]
}
25 changes: 25 additions & 0 deletions plugins/default/modelWhitelist.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import {
HookEventType,
PluginContext,
PluginHandler,
PluginParameters,
} from '../types';

export const handler: PluginHandler = async (
context: PluginContext,
parameters: PluginParameters,
eventType: HookEventType
) => {
let error = null;
let verdict = false;

try {
const modelList = parameters.models;
let requestModel = context.request?.json.model;
verdict = modelList.includes(requestModel);
} catch (e) {
error = e as Error;
}

return { error, verdict };
};
2 changes: 2 additions & 0 deletions plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { handler as defaultcontainsCode } from './default/containsCode';
import { handler as defaultalluppercase } from './default/alluppercase';
import { handler as defaultalllowercase } from './default/alllowercase';
import { handler as defaultendsWith } from './default/endsWith';
import { handler as defaultmodelWhitelist } from './default/modelWhitelist';
import { handler as portkeymoderateContent } from './portkey/moderateContent';
import { handler as portkeylanguage } from './portkey/language';
import { handler as portkeypii } from './portkey/pii';
Expand Down Expand Up @@ -48,6 +49,7 @@ export const plugins = {
alluppercase: defaultalluppercase,
alllowercase: defaultalllowercase,
endsWith: defaultendsWith,
modelWhitelist: defaultmodelWhitelist,
},
portkey: {
moderateContent: portkeymoderateContent,
Expand Down
Loading