Home > Enterprise >  How to see if openAI (node js) createModeration response "flagged" is true
How to see if openAI (node js) createModeration response "flagged" is true

Time:12-18

Using the OpenAI createModeration feature, I am trying to see if the string gets flagged or not.

I request the API by using this: `

const mod = await openai.createModeration({
      input: 'Some text to be flagged', // isn't actually this that would get flagged
});
console.log(mod.data.results)

When I log the resposne (and it's flagged) I get this:

[
  {
      hate: 0.057017914950847626,
      'hate/threatening': 0.0013999055372551084,
      'self-harm': 1.523021175842132e-8,
      sexual: 0.000011195417755516246,
      'sexual/minors': 4.2277463307982543e-8,
      violence: 0.8440001010894775,
      'violence/graphic': 1.2527605974810285e-7
    },
    flagged: true
  }
]

`

However, if I try to get the "flagged" option by doing mod.data.results.flagged it returns unidentified.

CodePudding user response:

mod.data.results is an array of objects. To get the first flagged value specify mod.data.results[0].flagged (or mod.data.results[0]?.flagged to account for an empty array)

UPDATE Actually something is not correct with your example object, it is missing a curly bracket.

  • Related