Skip to content

Commit

Permalink
Merge pull request #656 from Portkey-AI/enhancement/guardrail-explana…
Browse files Browse the repository at this point in the history
…tions

Adding explanations to default guardrails
  • Loading branch information
VisargD authored Dec 16, 2024
2 parents dfd3871 + d773163 commit 83028e8
Show file tree
Hide file tree
Showing 24 changed files with 3,275 additions and 653 deletions.
125 changes: 108 additions & 17 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 @@ -41,6 +41,7 @@
},
"dependencies": {
"@aws-crypto/sha256-js": "^5.2.0",
"@cfworker/json-schema": "^4.0.3",
"@hono/node-server": "^1.3.3",
"@hono/node-ws": "^1.0.4",
"@portkey-ai/mustache": "^2.1.2",
Expand Down
46 changes: 40 additions & 6 deletions plugins/default/alllowercase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
import { getText } from '../utils';

function isAllLowerCase(str: string): boolean {
// Remove non-letter characters and compare the result to its lowercased version
return str === str.toLowerCase();
// Remove non-letter characters and check if any uppercase letters exist
return (
str.replace(/[^a-zA-Z]/g, '') ===
str.replace(/[^a-zA-Z]/g, '').toLowerCase()
);
}

export const handler: PluginHandler = async (
Expand All @@ -18,13 +21,44 @@ export const handler: PluginHandler = async (
) => {
let error = null;
let verdict = false;
let data = null;
let data: any = null;

try {
let text = getText(context, eventType);
verdict = isAllLowerCase(text);
} catch (e) {
error = e as Error;
const not = parameters.not || false;

if (!text) {
throw new Error('Missing text to analyze');
}

const isLower = isAllLowerCase(text);
verdict = not ? !isLower : isLower;

data = {
verdict,
not,
explanation: verdict
? not
? 'The text contains uppercase characters as expected.'
: 'All alphabetic characters in the text are lowercase.'
: not
? 'All alphabetic characters in the text are lowercase when they should not be.'
: 'The text contains uppercase characters.',
textExcerpt: text.length > 100 ? text.slice(0, 100) + '...' : text,
};
} catch (e: any) {
error = e;
let textExcerpt = getText(context, eventType);
textExcerpt =
textExcerpt?.length > 100
? textExcerpt.slice(0, 100) + '...'
: textExcerpt;

data = {
explanation: `An error occurred while checking lowercase: ${e.message}`,
not: parameters.not || false,
textExcerpt: textExcerpt || 'No text available',
};
}

return { error, verdict, data };
Expand Down
46 changes: 40 additions & 6 deletions plugins/default/alluppercase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import {
import { getText } from '../utils';

function isAllUpperCase(str: string): boolean {
// Remove non-letter characters and compare the result to its uppercased version
return str === str.toUpperCase();
// Remove non-letter characters and check if any lowercase letters exist
return (
str.replace(/[^a-zA-Z]/g, '') ===
str.replace(/[^a-zA-Z]/g, '').toUpperCase()
);
}

export const handler: PluginHandler = async (
Expand All @@ -18,13 +21,44 @@ export const handler: PluginHandler = async (
) => {
let error = null;
let verdict = false;
let data = null;
let data: any = null;

try {
let text = getText(context, eventType);
verdict = isAllUpperCase(text);
} catch (e) {
error = e as Error;
const not = parameters.not || false;

if (!text) {
throw new Error('Missing text to analyze');
}

const isUpper = isAllUpperCase(text);
verdict = not ? !isUpper : isUpper;

data = {
verdict,
not,
explanation: verdict
? not
? 'The text contains lowercase characters as expected.'
: 'All alphabetic characters in the text are uppercase.'
: not
? 'All alphabetic characters in the text are uppercase when they should not be.'
: 'The text contains lowercase characters.',
textExcerpt: text.length > 100 ? text.slice(0, 100) + '...' : text,
};
} catch (e: any) {
error = e;
let textExcerpt = getText(context, eventType);
textExcerpt =
textExcerpt?.length > 100
? textExcerpt.slice(0, 100) + '...'
: textExcerpt;

data = {
explanation: `An error occurred while checking uppercase: ${e.message}`,
not: parameters.not || false,
textExcerpt: textExcerpt || 'No text available',
};
}

return { error, verdict, data };
Expand Down
Loading

0 comments on commit 83028e8

Please sign in to comment.