Skip to content

Commit 08cd951

Browse files
committed
fix: mistral guardrails to moderations function
1 parent 88930e0 commit 08cd951

File tree

2 files changed

+65
-116
lines changed

2 files changed

+65
-116
lines changed

plugins/mistral/index.ts

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ export const mistralGuardrailHandler: PluginHandler = async (
4343
context: PluginContext,
4444
parameters: PluginParameters,
4545
eventType: HookEventType,
46-
_options,
47-
fn?: string
46+
_options
4847
) => {
4948
let error = null;
5049
let verdict = true;
@@ -66,7 +65,7 @@ export const mistralGuardrailHandler: PluginHandler = async (
6665
model = parameters.model;
6766
}
6867

69-
const guardrailFunction = fn as GuardrailFunction;
68+
const checks = parameters.categories as GuardrailFunction[];
7069

7170
const text = getText(context, eventType);
7271
const messages =
@@ -109,10 +108,27 @@ export const mistralGuardrailHandler: PluginHandler = async (
109108
}
110109
);
111110

112-
verdict = request.results?.[0]?.categories[guardrailFunction];
113-
} catch (error) {
114-
error = error;
115-
verdict = false;
111+
const categories: Record<GuardrailFunction, boolean> =
112+
request.results[0]?.categories ?? {};
113+
const categoriesFlagged = Object.keys(categories).filter((category) => {
114+
if (
115+
checks.includes(category as GuardrailFunction) &&
116+
!!categories[category as GuardrailFunction]
117+
) {
118+
return true;
119+
}
120+
return false;
121+
});
122+
123+
if (categoriesFlagged.length > 0) {
124+
verdict = false;
125+
data = { flagged_categories: categoriesFlagged };
126+
}
127+
// Success
128+
verdict = true;
129+
} catch (err) {
130+
error = err;
131+
verdict = true;
116132
}
117133

118134
return { error, verdict, data };

plugins/mistral/manifest.json

Lines changed: 42 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -15,121 +15,54 @@
1515
},
1616
"functions": [
1717
{
18-
"name": "Detect PII",
19-
"id": "pii",
20-
"supportedHooks": ["beforeRequestHook", "afterRequestHook"],
18+
"name": "Moderate Content",
19+
"id": "moderateContent",
2120
"type": "guardrail",
21+
"supportedHooks": ["beforeRequestHook"],
2222
"description": [
2323
{
2424
"type": "subHeading",
25-
"text": "Content that requests, shares, or attempts to elicit personal identifying information such as full names, addresses, phone numbers, social security numbers, or financial account details."
25+
"text": "Checks if the content passes the mentioned content moderation checks."
2626
}
2727
],
28-
"parameters": {}
29-
},
30-
{
31-
"name": "Detect Sexual Content",
32-
"id": "sexual",
33-
"supportedHooks": ["beforeRequestHook", "afterRequestHook"],
34-
"type": "guardrail",
35-
"description": [
36-
{
37-
"type": "subHeading",
38-
"text": "Material that explicitly depicts, describes, or promotes sexual activities, nudity, or sexual services. This includes pornographic content, graphic descriptions of sexual acts, and solicitation for sexual purposes. Educational or medical content about sexual health presented in a non-explicit, informational context is generally exempted."
39-
}
40-
],
41-
"parameters": {}
42-
},
43-
{
44-
"name": "Detect Hate & Discrimination",
45-
"id": "hate_and_discrimination",
46-
"supportedHooks": ["beforeRequestHook", "afterRequestHook"],
47-
"type": "guardrail",
48-
"description": [
49-
{
50-
"type": "subHeading",
51-
"text": "Content that expresses prejudice, hostility, or advocates discrimination against individuals or groups based on protected characteristics such as race, ethnicity, religion, gender, sexual orientation, or disability. This includes slurs, dehumanizing language, calls for exclusion or harm targeted at specific groups, and persistent harassment or bullying of individuals based on these characteristics."
52-
}
53-
],
54-
"parameters": {}
55-
},
56-
{
57-
"name": "Detect Violent & Thereat",
58-
"id": "violence_and_threats",
59-
"supportedHooks": ["beforeRequestHook", "afterRequestHook"],
60-
"type": "guardrail",
61-
"description": [
62-
{
63-
"type": "subHeading",
64-
"text": "Content that describes, glorifies, incites, or threatens physical violence against individuals or groups. This includes graphic depictions of injury or death, explicit threats of harm, and instructions for carrying out violent acts. This category covers both targeted threats and general promotion or glorification of violence."
65-
}
66-
],
67-
"parameters": {}
68-
},
69-
{
70-
"name": "Detect Dangerous & Criminal Content",
71-
"id": "dangerous_and_criminal_content",
72-
"supportedHooks": ["beforeRequestHook", "afterRequestHook"],
73-
"type": "guardrail",
74-
"description": [
75-
{
76-
"type": "subHeading",
77-
"text": "Content that promotes or provides instructions for illegal activities or extremely hazardous behaviors that pose a significant risk of physical harm, death, or legal consequences. This includes guidance on creating weapons or explosives, encouragement of extreme risk-taking behaviors, and promotion of non-violent crimes such as fraud, theft, or drug trafficking."
78-
}
79-
],
80-
"parameters": {}
81-
},
82-
{
83-
"name": "Detect Selfharm",
84-
"id": "selfharm",
85-
"supportedHooks": ["beforeRequestHook", "afterRequestHook"],
86-
"type": "guardrail",
87-
"description": [
88-
{
89-
"type": "subHeading",
90-
"text": "Content that promotes, instructs, plans, or encourages deliberate self-injury, suicide, eating disorders, or other self-destructive behaviors. This includes detailed methods, glorification, statements of intent, dangerous challenges, and related slang terms"
91-
}
92-
],
93-
"parameters": {}
94-
},
95-
{
96-
"name": "Detect Health",
97-
"id": "health",
98-
"supportedHooks": ["beforeRequestHook", "afterRequestHook"],
99-
"type": "guardrail",
100-
"description": [
101-
{
102-
"type": "subHeading",
103-
"text": "Content that contains or tries to elicit detailed or tailored medical advice."
104-
}
105-
],
106-
"parameters": {}
107-
},
108-
{
109-
"name": "Detect Finance",
110-
"id": "financial",
111-
"supportedHooks": ["beforeRequestHook", "afterRequestHook"],
112-
"type": "guardrail",
113-
"description": [
114-
{
115-
"type": "subHeading",
116-
"text": "Content that contains or tries to elicit detailed or tailored financial advice."
117-
}
118-
],
119-
"parameters": {}
120-
},
121-
{
122-
"name": "Detect Law",
123-
"id": "law",
124-
"supportedHooks": ["beforeRequestHook", "afterRequestHook"],
125-
"type": "guardrail",
126-
"description": [
127-
{
128-
"type": "subHeading",
129-
"text": "Content that contains or tries to elicit detailed or tailored legal advice."
130-
}
131-
],
132-
"parameters": {}
28+
"parameters": {
29+
"type": "object",
30+
"properties": {
31+
"categories": {
32+
"type": "array",
33+
"label": "Moderation Checks",
34+
"description": [
35+
{
36+
"type": "subHeading",
37+
"text": "Select the categories that should NOT be allowed in the content. (Checked via OpenAI moderation API)"
38+
}
39+
],
40+
"items": {
41+
"type": "string",
42+
"enum": [
43+
"sexual",
44+
"hate_and_discrimination",
45+
"violence_and_threats",
46+
"dangerous_and_criminal_content",
47+
"selfharm",
48+
"health",
49+
"financial",
50+
"law",
51+
"pii"
52+
],
53+
"default": [
54+
"hate/threatening",
55+
"harassment/threatening",
56+
"self-harm/intent",
57+
"self-harm/instructions",
58+
"sexual/minors",
59+
"violence/graphic"
60+
]
61+
}
62+
}
63+
},
64+
"required": ["categories"]
65+
}
13366
}
13467
]
13568
}

0 commit comments

Comments
 (0)