Skip to content

Commit

Permalink
👮feat: moderation text (danny-avila#1388)
Browse files Browse the repository at this point in the history
* fixed some bugs and handling errors better

* feat: plugins support

* fix: prettier error message

* moved circular-json-es6 in /api

* docs: added openai moderation text

* fix(gptPlugins): incorrect merge

* discarding changes

* removed circular-json-es6
  • Loading branch information
berry-13 authored Jan 1, 2024
1 parent 98017a8 commit 68a57ff
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 2 deletions.
2 changes: 2 additions & 0 deletions server/middleware/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const concurrentLimiter = require('./concurrentLimiter');
const validateMessageReq = require('./validateMessageReq');
const buildEndpointOption = require('./buildEndpointOption');
const validateRegistration = require('./validateRegistration');
const moderateText = require('./moderateText');
const noIndex = require('./noIndex');

module.exports = {
Expand All @@ -29,5 +30,6 @@ module.exports = {
validateMessageReq,
buildEndpointOption,
validateRegistration,
moderateText,
noIndex,
};
39 changes: 39 additions & 0 deletions server/middleware/moderateText.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const axios = require('axios');
const denyRequest = require('./denyRequest');

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}`,
},
},
);

const results = response.data.results;
const flagged = results.some((result) => result.flagged);

if (flagged) {
const type = 'moderation';
const errorMessage = { type };
return await denyRequest(req, res, errorMessage);
}
} catch (error) {
console.error('Error in moderateText:', error);
const errorMessage = 'error in moderation check';
return await denyRequest(req, res, errorMessage);
}
}
next();
}

module.exports = moderateText;
2 changes: 2 additions & 0 deletions server/routes/ask/gptPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ const {
setHeaders,
validateEndpoint,
buildEndpointOption,
moderateText,
} = require('~/server/middleware');
const { logger } = require('~/config');

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

router.post('/', validateEndpoint, buildEndpointOption, setHeaders, async (req, res) => {
Expand Down
3 changes: 2 additions & 1 deletion server/routes/ask/openAI.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const {
setHeaders,
validateEndpoint,
buildEndpointOption,
moderateText,
} = require('~/server/middleware');

const router = express.Router();

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

router.post('/', validateEndpoint, buildEndpointOption, setHeaders, async (req, res, next) => {
Expand Down
2 changes: 2 additions & 0 deletions server/routes/edit/gptPlugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ const {
setHeaders,
validateEndpoint,
buildEndpointOption,
moderateText,
} = require('~/server/middleware');
const { logger } = require('~/config');

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

router.post('/', validateEndpoint, buildEndpointOption, setHeaders, async (req, res) => {
Expand Down
3 changes: 2 additions & 1 deletion server/routes/edit/openAI.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ const {
setHeaders,
validateEndpoint,
buildEndpointOption,
moderateText,
} = require('~/server/middleware');

const router = express.Router();

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

router.post('/', validateEndpoint, buildEndpointOption, setHeaders, async (req, res, next) => {
Expand Down

0 comments on commit 68a57ff

Please sign in to comment.