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

🚔 feat: OpenAI Moderation update #4827

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions api/cache/getLogStores.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ const namespaces = {
[ViolationTypes.ILLEGAL_MODEL_REQUEST]: createViolationInstance(
ViolationTypes.ILLEGAL_MODEL_REQUEST,
),
[ViolationTypes.MODERATION]: createViolationInstance(ViolationTypes.MODERATION),
logins: createViolationInstance('logins'),
[CacheKeys.ABORT_KEYS]: abortKeys,
[CacheKeys.TOKEN_CONFIG]: tokenConfig,
Expand Down
163 changes: 135 additions & 28 deletions api/server/middleware/moderateText.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,148 @@
const axios = require('axios');
const { ErrorTypes } = require('librechat-data-provider');
const OpenAI = require('openai');
const { ErrorTypes, ViolationTypes } = require('librechat-data-provider');
const { getCustomConfig } = require('~/server/services/Config');
const { isEnabled } = require('~/server/utils');
const denyRequest = require('./denyRequest');
const { logViolation } = require('~/cache');
const { logger } = require('~/config');

const DEFAULT_ACTIONS = Object.freeze({
violation: 2,
blockMessage: true,
log: true,
});

// Pre-compile threshold map for faster lookups
const DEFAULT_THRESHOLDS = new Map();

function formatViolation(violation) {
return {
category: violation.category,
score: Math.round(violation.score * 100) / 100,
threshold: violation.threshold,
severity: getSeverityLevel(violation.score),
};
}

function getSeverityLevel(score) {
if (score >= 0.9) {
return 'HIGH';
}
if (score >= 0.7) {
return 'MEDIUM';
}
return 'LOW';
}

function formatViolationsLog(violations, userId = 'unknown') {
const violationsStr = violations
.map((v) => `${v.category}:${v.score}>${v.threshold}`)
.join(' | ');

return `userId=${userId} violations=[${violationsStr}]`;
}

async function moderateText(req, res, next) {
if (process.env.OPENAI_MODERATION === 'true') {
try {
const { text } = req.body;

const response = await axios.post(
process.env.OPENAI_MODERATION_REVERSE_PROXY || 'https://api.openai.com/v1/moderations',
{
input: text,
},
{
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${process.env.OPENAI_MODERATION_API_KEY}`,
},
},
if (!isEnabled(process.env.OPENAI_MODERATION)) {
return next();
}

const moderationKey = process.env.OPENAI_MODERATION_API_KEY;
if (!moderationKey) {
logger.error('Missing OpenAI moderation API key');
return denyRequest(req, res, { message: 'Moderation configuration error' });
}

const { text } = req.body;
if (!text?.length || typeof text !== 'string') {
return denyRequest(req, res, { type: ErrorTypes.VALIDATION, message: 'Invalid text input' });
}

try {
const customConfig = await getCustomConfig();

if (!moderateText.openai) {
moderateText.openai = new OpenAI({ apiKey: moderationKey });
}

const response = await moderateText.openai.moderations.create({
model: 'omni-moderation-latest',
input: text,
});

if (!Array.isArray(response.results)) {
throw new Error('Invalid moderation API response format');
}

const violations = checkViolations(response.results, customConfig).map(formatViolation);

if (violations.length === 0) {
return next();
}

const actions = Object.assign({}, DEFAULT_ACTIONS, customConfig?.moderation?.actions);

if (actions.log) {
const userId = req.user?.id || 'anonymous';
logger.warn(
'[moderateText] Content moderation violations: ' + formatViolationsLog(violations, userId),
);
}

if (!actions.blockMessage) {
return next();
}

if (actions.violation > 0) {
logViolation(req, res, ViolationTypes.MODERATION, { violations }, actions.violation);
}

return denyRequest(req, res, {
type: ErrorTypes.MODERATION,
message: `Content violates moderation policies: ${violations
.map((v) => v.category)
.join(', ')}`,
violations: violations,
});
} catch (error) {
const errorDetails =
process.env.NODE_ENV === 'production'
? { message: error.message }
: {
error: error.message,
stack: error.stack,
status: error.response?.status,
};

logger.error('Moderation error:', errorDetails);

return denyRequest(req, res, {
type: ErrorTypes.MODERATION,
message: 'Content moderation check failed',
});
}
}

function checkViolations(results, customConfig) {
const violations = [];
const categories = customConfig?.moderation?.categories || {};

for (const result of results) {
for (const [category, score] of Object.entries(result.category_scores)) {
const categoryConfig = categories[category];

if (categoryConfig?.enabled === false) {
continue;
}

const results = response.data.results;
const flagged = results.some((result) => result.flagged);
const threshold = categoryConfig?.threshold || DEFAULT_THRESHOLDS.get(category) || 0.7;

if (flagged) {
const type = ErrorTypes.MODERATION;
const errorMessage = { type };
return await denyRequest(req, res, errorMessage);
if (score >= threshold) {
violations.push({ category, score, threshold });
}
} catch (error) {
logger.error('Error in moderateText:', error);
const errorMessage = 'error in moderation check';
return await denyRequest(req, res, errorMessage);
}
}
next();
return violations;
}

module.exports = moderateText;
2 changes: 1 addition & 1 deletion api/server/routes/ask/gptPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const { logger } = require('~/config');

const router = express.Router();

router.use(moderateText);
router.post('/abort', handleAbort());
router.use(moderateText);

router.post(
'/',
Expand Down
2 changes: 1 addition & 1 deletion api/server/routes/ask/openAI.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const {
} = require('~/server/middleware');

const router = express.Router();
router.use(moderateText);
router.post('/abort', handleAbort());
router.use(moderateText);

router.post(
'/',
Expand Down
2 changes: 1 addition & 1 deletion api/server/routes/edit/gptPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ const { logger } = require('~/config');

const router = express.Router();

router.use(moderateText);
router.post('/abort', handleAbort());
router.use(moderateText);

router.post(
'/',
Expand Down
2 changes: 1 addition & 1 deletion api/server/routes/edit/openAI.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ const {
} = require('~/server/middleware');

const router = express.Router();
router.use(moderateText);
router.post('/abort', handleAbort());
router.use(moderateText);

router.post(
'/',
Expand Down
Loading
Loading