mirror of
https://github.com/hanzoai/chat.git
synced 2026-09-30 20:51:17 +00:00
librechat-data-provider → @hanzochat/data-provider @librechat/data-schemas → @hanzochat/data-schemas @librechat/client → @hanzochat/client (@hanzochat/api already forked; external @librechat/agents fork follows) All imports, package.json names/deps, tsconfig path aliases, jest moduleNameMapper, and rollup/vite build aliases updated. The data-provider react-query subpath resolves via the exports map. Lockfiles regenerated in a follow-on once the canonical package manager is confirmed.
44 lines
1.2 KiB
JavaScript
44 lines
1.2 KiB
JavaScript
const axios = require('axios');
|
|
const { isEnabled } = require('@hanzochat/api');
|
|
const { logger } = require('@hanzochat/data-schemas');
|
|
const { ErrorTypes } = require('@hanzochat/data-provider');
|
|
const denyRequest = require('./denyRequest');
|
|
|
|
async function moderateText(req, res, next) {
|
|
if (!isEnabled(process.env.OPENAI_MODERATION)) {
|
|
return next();
|
|
}
|
|
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}`,
|
|
},
|
|
},
|
|
);
|
|
|
|
const results = response.data.results;
|
|
const flagged = results.some((result) => result.flagged);
|
|
|
|
if (flagged) {
|
|
const type = ErrorTypes.MODERATION;
|
|
const errorMessage = { type };
|
|
return await denyRequest(req, res, errorMessage);
|
|
}
|
|
} catch (error) {
|
|
logger.error('Error in moderateText:', error);
|
|
const errorMessage = 'error in moderation check';
|
|
return await denyRequest(req, res, errorMessage);
|
|
}
|
|
next();
|
|
}
|
|
|
|
module.exports = moderateText;
|