So, here is part 2 friends! Yesterday, I posted about some of the work I had done in Chris H, Dona S, and Will D’s South Coast Summit workshop. In that blog post we looked at building a Copilot bot from scratch with OpenAI’s API and JavaScript.
So now, in this post, we’ll take that bot further with some moderation techniques, because as always its important when working with AI that we work with it responsibly. We’ll ensure we do various checks on the bot’s response so users can then later filter out anything not 100% right. We’ll do that filtering in a next post though.
Recap…
So, before we continue, make sure you’ve read the following post and you’ve built the bot to that point first.
Adding moderation
So now, we’ll work on the part where we’ll do a call to the moderation endpoint from OpenAI and pass it the response the create chat endpoint already gave us back to then see if it’s worth showing to the user.
So let’s look at my code…
// Select from outputs and log to console
var reply = completion.data.choices[0].message.content;
messages.push({"role": "assistant", "content": reply});
inclusiveReply = reply + "\n Note this response is from a bot powered by AI. Receive responses and handle with care."
console.log(inclusiveReply);
// Run moderation on the previous response
const moderation = await openai.createModeration({
input: inclusiveReply
})
In the code above, in the first block before the line break I’m logging my reply from the API with some additional hard-coded text to the console.
Then in the next block, this is where I make the call to the create moderation endpoint with the reply the previous call gave us. You can see I’ve used ‘await’ because I’m working in an asynchronous function here where I need to wait for the response before I set the variable with the value.
Now lets look at what I do with my constant, ‘moderation’.
// Return the moderation output to the user
console.log("To ensure we gave you a safe response, we did a double check and here's the moderation results for the response we gave:");
console.table(moderation.data.results[0].categories)
Now with my output from the create moderation endpoint, I’m going to select the first record back in an array held in moderation.data.results in the outputs. Then I’ll select the categories property from that record and put the array in a table in the console.
Check it out…
That’s great! So now the next steps we’ll be able to look at in a future post now that we’ve got this data is processing the moderation first, then deciding whether we actually send the user the response or not depending on the results of the response moderation. If anything returns true, I don’t want the user to receive the response in the terminal.
So friends, stay tuned and subscribe for the next part of this mini-series on building your own Copilot with JavaScript and OpenAI! 👨🏻💻
Did you like this content? 💖
Did you like this content? Check out some of the other posts on my blog, and if you like those too, be sure to subscribe to get my posts directly in your inbox for free!
Subscribe