scrimba
AI Engineering
Intro to AI Engineering
Safety Best Practices
Go Pro!Bootcamp

Bootcamp

Study group

Collaborate with peers in your dedicated #study-group channel.

Code reviews

Submit projects for review using the /review command in your #code-reviews channel

To see this lesson you need to be logged in. Joining Scrimba is free and gives you access to 20+ courses.Sign up
AboutCommentsNotes
Safety Best Practices
Expand for more info
index.js
run
preview
console
import OpenAI from "openai";

const openai = new OpenAI({
dangerouslyAllowBrowser: true
});

async function main() {
// Relevant for both inputs and outputs
const completion = await openai.moderations.create({
input: "I hate you!"
});
const {flagged, categories} = completion.results[0];
console.log("flagged", flagged);
console.log("categories", categories);

// if (flagged) {
// renderWarning(categories);
// }
}

main();

function renderWarning(obj) {
const keys = Object.keys(obj);
const filtered = keys.filter((key) => obj[key]);
document.body.innerText =
`Your response has been flagged for the following reasons: ${filtered.join(", ")}.`
}

Console
"flagged"
,
true
,
"categories"
,
{sexual:
false
, hate:
false
, harassment:
true
, self-harm:
false
, sexual/minors:
false
, hate/threatening:
false
, violence/graphic:
false
, self-harm/intent:
false
, self-harm/instructions:
false
, harassment/threatening:
false
, violence:
false
}
,
true
,
/index.html
-3:22