Skip to content

Commit ee09503

Browse files
authored
moderation fix so learners can practice with coding topics (#150)
1 parent c14d879 commit ee09503

File tree

1 file changed

+72
-4
lines changed

1 file changed

+72
-4
lines changed

apps/api/src/api/llm/core/services/moderation.service.ts

Lines changed: 72 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,32 @@ export class ModerationService implements IModerationService {
1919
async validateContent(content: string): Promise<boolean> {
2020
if (!content) return true;
2121

22+
// Check if this appears to be legitimate educational/technical content
23+
if (this.isEducationalContent(content)) {
24+
this.logger.debug(
25+
"Content appears to be educational/technical, allowing with less strict moderation",
26+
);
27+
return true;
28+
}
29+
2230
try {
2331
const moderation = new OpenAIModerationChain();
2432

2533
const { output: guardRailsResponse } = await moderation.invoke({
2634
input: content,
2735
});
2836

29-
return (
30-
guardRailsResponse !==
31-
"Text was found that violates OpenAI's content policy."
32-
);
37+
const isViolation =
38+
guardRailsResponse ===
39+
"Text was found that violates OpenAI's content policy.";
40+
41+
if (isViolation) {
42+
this.logger.warn(
43+
`Content flagged by moderation: ${content.slice(0, 200)}...`,
44+
);
45+
}
46+
47+
return !isViolation;
3348
} catch (error) {
3449
this.logger.error(
3550
`Error validating content: ${
@@ -41,6 +56,59 @@ export class ModerationService implements IModerationService {
4156
}
4257
}
4358

59+
private isEducationalContent(content: string): boolean {
60+
const educationalIndicators = [
61+
// Security education keywords
62+
"xss",
63+
"cross-site scripting",
64+
"security",
65+
"vulnerability",
66+
"penetration testing",
67+
"cybersecurity",
68+
"sql injection",
69+
"csrf",
70+
"owasp",
71+
"security report",
72+
"vulnerability assessment",
73+
"security analysis",
74+
"ethical hacking",
75+
76+
// Technical/programming education
77+
"algorithm",
78+
"data structure",
79+
"programming",
80+
"software development",
81+
"computer science",
82+
"technical documentation",
83+
"code example",
84+
"tutorial",
85+
"documentation",
86+
"technical report",
87+
"analysis",
88+
"implementation",
89+
90+
// Academic indicators
91+
"research",
92+
"study",
93+
"analysis",
94+
"conclusion",
95+
"methodology",
96+
"findings",
97+
"bibliography",
98+
"references",
99+
"abstract",
100+
"introduction",
101+
];
102+
103+
const lowerContent = content.toLowerCase();
104+
const matchCount = educationalIndicators.filter((indicator) =>
105+
lowerContent.includes(indicator),
106+
).length;
107+
108+
// If multiple educational indicators are present, likely educational content
109+
return matchCount >= 2;
110+
}
111+
44112
/**
45113
* Sanitize the content by removing any potentially harmful or unnecessary elements.
46114
* This method uses DOMPurify to remove scripts or other dangerous HTML content.

0 commit comments

Comments
 (0)