File size: 759 Bytes
e4e0e54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import {
  CreateModerationResponseResultsInnerCategories,
  CreateModerationResponseResultsInnerCategoryScores,
} from "openai"

import { openai } from "./openai.mts"

export const runModerationCheck = async (
  input = ''
): Promise<{
  categories?: CreateModerationResponseResultsInnerCategories
  category_scores?: CreateModerationResponseResultsInnerCategoryScores
  flagged: boolean
}> => {
  if (!input || !input.length) {
    console.log(`skipping moderation check as input length is too shot`)
    return {
      flagged: false,
    }
  }

  const response = await openai.createModeration({ input })
  const { results } = response.data

  if (!results.length) {
    throw new Error(`failed to call the moderation endpoint`)
  }

  return results[0]
}