Skip to content

Commit 7ee1975

Browse files
feat : walledai plugin
1 parent 2b229d4 commit 7ee1975

File tree

5 files changed

+365
-135
lines changed

5 files changed

+365
-135
lines changed

conf.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"patronus",
99
"pangea",
1010
"promptsecurity",
11-
"panw-prisma-airs"
11+
"panw-prisma-airs",
12+
"walledai"
1213
],
1314
"credentials": {
1415
"portkey": {

plugins/index.ts

Lines changed: 104 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -1,137 +1,107 @@
1-
import { handler as defaultregexMatch } from './default/regexMatch';
2-
import { handler as defaultsentenceCount } from './default/sentenceCount';
3-
import { handler as defaultwordCount } from './default/wordCount';
4-
import { handler as defaultcharacterCount } from './default/characterCount';
5-
import { handler as defaultjsonSchema } from './default/jsonSchema';
6-
import { handler as defaultjsonKeys } from './default/jsonKeys';
7-
import { handler as defaultcontains } from './default/contains';
8-
import { handler as defaultvalidUrls } from './default/validUrls';
9-
import { handler as defaultwebhook } from './default/webhook';
10-
import { handler as defaultlog } from './default/log';
11-
import { handler as defaultcontainsCode } from './default/containsCode';
12-
import { handler as defaultalluppercase } from './default/alluppercase';
13-
import { handler as defaultalllowercase } from './default/alllowercase';
14-
import { handler as defaultendsWith } from './default/endsWith';
15-
import { handler as defaultmodelWhitelist } from './default/modelWhitelist';
16-
import { handler as portkeymoderateContent } from './portkey/moderateContent';
17-
import { handler as portkeylanguage } from './portkey/language';
18-
import { handler as portkeypii } from './portkey/pii';
19-
import { handler as portkeygibberish } from './portkey/gibberish';
20-
import { handler as aporiavalidateProject } from './aporia/validateProject';
21-
import { handler as sydelabssydeguard } from './sydelabs/sydeguard';
22-
import { handler as pillarscanPrompt } from './pillar/scanPrompt';
23-
import { handler as pillarscanResponse } from './pillar/scanResponse';
24-
import { handler as patronusphi } from './patronus/phi';
25-
import { handler as patronuspii } from './patronus/pii';
26-
import { handler as patronusisConcise } from './patronus/isConcise';
27-
import { handler as patronusisHelpful } from './patronus/isHelpful';
28-
import { handler as patronusisPolite } from './patronus/isPolite';
29-
import { handler as patronusnoApologies } from './patronus/noApologies';
30-
import { handler as patronusnoGenderBias } from './patronus/noGenderBias';
31-
import { handler as patronusnoRacialBias } from './patronus/noRacialBias';
32-
import { handler as patronusretrievalAnswerRelevance } from './patronus/retrievalAnswerRelevance';
33-
import { handler as patronustoxicity } from './patronus/toxicity';
34-
import { handler as patronuscustom } from './patronus/custom';
35-
import { mistralGuardrailHandler } from './mistral';
36-
import { handler as pangeatextGuard } from './pangea/textGuard';
37-
import { handler as promptfooPii } from './promptfoo/pii';
38-
import { handler as promptfooHarm } from './promptfoo/harm';
39-
import { handler as promptfooGuard } from './promptfoo/guard';
40-
import { handler as pangeapii } from './pangea/pii';
41-
import { pluginHandler as bedrockHandler } from './bedrock/index';
42-
import { handler as acuvityScan } from './acuvity/scan';
43-
import { handler as lassoclassify } from './lasso/classify';
44-
import { handler as exaonline } from './exa/online';
45-
import { handler as azurePii } from './azure/pii';
46-
import { handler as azureContentSafety } from './azure/contentSafety';
47-
import { handler as promptSecurityProtectPrompt } from './promptsecurity/protectPrompt';
48-
import { handler as promptSecurityProtectResponse } from './promptsecurity/protectResponse';
49-
import { handler as panwPrismaAirsintercept } from './panw-prisma-airs/intercept';
50-
import { handler as defaultjwt } from './default/jwt';
51-
import { handler as defaultrequiredMetadataKeys } from './default/requiredMetadataKeys';
1+
import { handler as defaultregexMatch } from "./default/regexMatch"
2+
import { handler as defaultsentenceCount } from "./default/sentenceCount"
3+
import { handler as defaultwordCount } from "./default/wordCount"
4+
import { handler as defaultcharacterCount } from "./default/characterCount"
5+
import { handler as defaultjsonSchema } from "./default/jsonSchema"
6+
import { handler as defaultjsonKeys } from "./default/jsonKeys"
7+
import { handler as defaultcontains } from "./default/contains"
8+
import { handler as defaultvalidUrls } from "./default/validUrls"
9+
import { handler as defaultwebhook } from "./default/webhook"
10+
import { handler as defaultlog } from "./default/log"
11+
import { handler as defaultcontainsCode } from "./default/containsCode"
12+
import { handler as defaultalluppercase } from "./default/alluppercase"
13+
import { handler as defaultendsWith } from "./default/endsWith"
14+
import { handler as defaultalllowercase } from "./default/alllowercase"
15+
import { handler as defaultmodelwhitelist } from "./default/modelwhitelist"
16+
import { handler as defaultjwt } from "./default/jwt"
17+
import { handler as defaultrequiredMetadataKeys } from "./default/requiredMetadataKeys"
18+
import { handler as portkeymoderateContent } from "./portkey/moderateContent"
19+
import { handler as portkeylanguage } from "./portkey/language"
20+
import { handler as portkeypii } from "./portkey/pii"
21+
import { handler as portkeygibberish } from "./portkey/gibberish"
22+
import { handler as aporiavalidateProject } from "./aporia/validateProject"
23+
import { handler as sydelabssydeguard } from "./sydelabs/sydeguard"
24+
import { handler as pillarscanPrompt } from "./pillar/scanPrompt"
25+
import { handler as pillarscanResponse } from "./pillar/scanResponse"
26+
import { handler as patronusphi } from "./patronus/phi"
27+
import { handler as patronuspii } from "./patronus/pii"
28+
import { handler as patronusisConcise } from "./patronus/isConcise"
29+
import { handler as patronusisHelpful } from "./patronus/isHelpful"
30+
import { handler as patronusisPolite } from "./patronus/isPolite"
31+
import { handler as patronusnoApologies } from "./patronus/noApologies"
32+
import { handler as patronusnoGenderBias } from "./patronus/noGenderBias"
33+
import { handler as patronusnoRacialBias } from "./patronus/noRacialBias"
34+
import { handler as patronusretrievalAnswerRelevance } from "./patronus/retrievalAnswerRelevance"
35+
import { handler as patronustoxicity } from "./patronus/toxicity"
36+
import { handler as patronuscustom } from "./patronus/custom"
37+
import { handler as pangeatextGuard } from "./pangea/textGuard"
38+
import { handler as pangeapii } from "./pangea/pii"
39+
import { handler as promptsecurityprotectPrompt } from "./promptsecurity/protectPrompt"
40+
import { handler as promptsecurityprotectResponse } from "./promptsecurity/protectResponse"
41+
import { handler as panwPrismaAirsintercept } from "./panw-prisma-airs/intercept"
42+
import { handler as walledaiguardrails } from "./walledai/guardrails"
5243

5344
export const plugins = {
54-
default: {
55-
regexMatch: defaultregexMatch,
56-
sentenceCount: defaultsentenceCount,
57-
wordCount: defaultwordCount,
58-
characterCount: defaultcharacterCount,
59-
jsonSchema: defaultjsonSchema,
60-
jsonKeys: defaultjsonKeys,
61-
contains: defaultcontains,
62-
validUrls: defaultvalidUrls,
63-
webhook: defaultwebhook,
64-
log: defaultlog,
65-
containsCode: defaultcontainsCode,
66-
alluppercase: defaultalluppercase,
67-
alllowercase: defaultalllowercase,
68-
endsWith: defaultendsWith,
69-
modelWhitelist: defaultmodelWhitelist,
70-
jwt: defaultjwt,
71-
requiredMetadataKeys: defaultrequiredMetadataKeys,
72-
},
73-
portkey: {
74-
moderateContent: portkeymoderateContent,
75-
language: portkeylanguage,
76-
pii: portkeypii,
77-
gibberish: portkeygibberish,
78-
},
79-
aporia: {
80-
validateProject: aporiavalidateProject,
81-
},
82-
sydelabs: {
83-
sydeguard: sydelabssydeguard,
84-
},
85-
pillar: {
86-
scanPrompt: pillarscanPrompt,
87-
scanResponse: pillarscanResponse,
88-
},
89-
patronus: {
90-
phi: patronusphi,
91-
pii: patronuspii,
92-
isConcise: patronusisConcise,
93-
isHelpful: patronusisHelpful,
94-
isPolite: patronusisPolite,
95-
noApologies: patronusnoApologies,
96-
noGenderBias: patronusnoGenderBias,
97-
noRacialBias: patronusnoRacialBias,
98-
retrievalAnswerRelevance: patronusretrievalAnswerRelevance,
99-
toxicity: patronustoxicity,
100-
custom: patronuscustom,
101-
},
102-
mistral: {
103-
moderateContent: mistralGuardrailHandler,
104-
},
105-
pangea: {
106-
textGuard: pangeatextGuard,
107-
pii: pangeapii,
108-
},
109-
promptfoo: {
110-
pii: promptfooPii,
111-
harm: promptfooHarm,
112-
guard: promptfooGuard,
113-
},
114-
bedrock: {
115-
guard: bedrockHandler,
116-
},
117-
acuvity: {
118-
scan: acuvityScan,
119-
},
120-
lasso: {
121-
classify: lassoclassify,
122-
},
123-
exa: {
124-
online: exaonline,
125-
},
126-
azure: {
127-
pii: azurePii,
128-
contentSafety: azureContentSafety,
129-
},
130-
promptsecurity: {
131-
protectPrompt: promptSecurityProtectPrompt,
132-
protectResponse: promptSecurityProtectResponse,
133-
},
134-
'panw-prisma-airs': {
135-
intercept: panwPrismaAirsintercept,
136-
},
45+
"default": {
46+
"regexMatch": defaultregexMatch,
47+
"sentenceCount": defaultsentenceCount,
48+
"wordCount": defaultwordCount,
49+
"characterCount": defaultcharacterCount,
50+
"jsonSchema": defaultjsonSchema,
51+
"jsonKeys": defaultjsonKeys,
52+
"contains": defaultcontains,
53+
"validUrls": defaultvalidUrls,
54+
"webhook": defaultwebhook,
55+
"log": defaultlog,
56+
"containsCode": defaultcontainsCode,
57+
"alluppercase": defaultalluppercase,
58+
"endsWith": defaultendsWith,
59+
"alllowercase": defaultalllowercase,
60+
"modelwhitelist": defaultmodelwhitelist,
61+
"jwt": defaultjwt,
62+
"requiredMetadataKeys": defaultrequiredMetadataKeys
63+
},
64+
"portkey": {
65+
"moderateContent": portkeymoderateContent,
66+
"language": portkeylanguage,
67+
"pii": portkeypii,
68+
"gibberish": portkeygibberish
69+
},
70+
"aporia": {
71+
"validateProject": aporiavalidateProject
72+
},
73+
"sydelabs": {
74+
"sydeguard": sydelabssydeguard
75+
},
76+
"pillar": {
77+
"scanPrompt": pillarscanPrompt,
78+
"scanResponse": pillarscanResponse
79+
},
80+
"patronus": {
81+
"phi": patronusphi,
82+
"pii": patronuspii,
83+
"isConcise": patronusisConcise,
84+
"isHelpful": patronusisHelpful,
85+
"isPolite": patronusisPolite,
86+
"noApologies": patronusnoApologies,
87+
"noGenderBias": patronusnoGenderBias,
88+
"noRacialBias": patronusnoRacialBias,
89+
"retrievalAnswerRelevance": patronusretrievalAnswerRelevance,
90+
"toxicity": patronustoxicity,
91+
"custom": patronuscustom
92+
},
93+
"pangea": {
94+
"textGuard": pangeatextGuard,
95+
"pii": pangeapii
96+
},
97+
"promptsecurity": {
98+
"protectPrompt": promptsecurityprotectPrompt,
99+
"protectResponse": promptsecurityprotectResponse
100+
},
101+
"panw-prisma-airs": {
102+
"intercept": panwPrismaAirsintercept
103+
},
104+
"walledai": {
105+
"guardrails": walledaiguardrails
106+
}
137107
};

plugins/walledai/guardrails.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import {
2+
HookEventType,
3+
PluginContext,
4+
PluginHandler,
5+
PluginParameters,
6+
} from '../types';
7+
import { post, getText } from '../utils';
8+
9+
const API_URL =
10+
'https://services.walled.ai/v1/guardrail/moderate';
11+
12+
export const handler: PluginHandler = async (
13+
context: PluginContext,
14+
parameters: PluginParameters,
15+
eventType: HookEventType
16+
) => {
17+
let error = null;
18+
let verdict = true;
19+
let data = null;
20+
21+
if (!parameters.credentials?.apiKey) {
22+
return {
23+
error: `'parameters.credentials.apiKey' must be set`,
24+
verdict: true,
25+
data,
26+
};
27+
}
28+
29+
const text = getText(context, eventType);
30+
if (!text) {
31+
return {
32+
error: 'request or response text is empty',
33+
verdict: true,
34+
data,
35+
};
36+
}
37+
38+
// Prepare request body
39+
const requestBody = {
40+
text: text,
41+
text_type: parameters.text_type || 'prompt',
42+
generic_safety_check: parameters.generic_safety_check ?? true,
43+
greetings_list: parameters.greetings_list || ['generalgreetings'],
44+
};
45+
46+
// Prepare headers
47+
const requestOptions = {
48+
headers: {
49+
'Content-Type': 'application/json',
50+
'Authorization': `Bearer ${parameters.credentials.apiKey}`, // Uncomment if API key is required
51+
},
52+
};
53+
54+
try {
55+
const response = await post(API_URL, requestBody, requestOptions, parameters.timeout);
56+
data = response.data;
57+
if (
58+
data.safety[0]?.isSafe==false
59+
) {
60+
verdict = false;
61+
}
62+
} catch (e) {
63+
console.log(e)
64+
error = e instanceof Error ? e.message : String(e);
65+
verdict = true;
66+
data = null;
67+
}
68+
return {
69+
error,
70+
verdict,
71+
data,
72+
};
73+
};

0 commit comments

Comments
 (0)