Skip to content

Instantly share code, notes, and snippets.

@slvnperron
Last active February 22, 2021 18:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slvnperron/275c72a673aae516905d3c4fce10c61e to your computer and use it in GitHub Desktop.
Save slvnperron/275c72a673aae516905d3c4fce10c61e to your computer and use it in GitHub Desktop.
Script to test none utterances (OOS)
async function action(bp: typeof sdk, event: sdk.IO.IncomingEvent, args: any, { user, temp, session } = event.state) {
/** Your code starts below */
const _ = require('lodash')
const axios = require('axios')
/**
* Tests multiple utterances from the bot's intent automatically
* @title NLU Tests
* @category NLU
* @author Botpress, Inc.
* @param {boolean} justNone - Only test none
*/
const myAction = async (justNone = true) => {
const config = await bp.http.getAxiosConfigForBot(event.botId, { localUrl: true })
const tests = [
"de l'autre de côté de la rue il y a de la crème glacée",
"je ne sais pas d'ou provient ce message",
"c'était une super soirée en ta présence",
"les caméléons sont la pluspart du temps vert",
"je n'hésiterai pas à te foutre une râclée",
"do you mind if aliens take control of your sofa?",
"would you rather be dead or a turtle?",
"my favorite cinema is located in Morocco",
"pistachio icecream is so tasteful! you should try it out",
"I don't mind when there's popcorn in my chocolat",
"generate a new license key",
"what's the title of this book?",
"please look elsewhere, I don't have anything to sell to you",
"motorbikes are dangerous because they go fast"
].map(t => ({ text: t, intent: 'none' }))
if (!justNone) {
const intents = await bp.ghost.forBot(event.botId).directoryListing('./intents', '*.json')
for (let intent of intents) {
const content = await bp.ghost.forBot(event.botId).readFileAsObject('./intents', intent)
const samples = _.sampleSize(_.flatten(Object.values(content.utterances)), 5)
samples.forEach(s => tests.push({ text: s, intent: content.name }))
}
}
let ok = 0
bp.logger.debug(`\n\n\n------------- START -------------------`)
for (const { text, intent } of tests) {
///api/v1/bots/{botId}/converse/{userId}/secured?include=nlu,state,suggestions,decision
const userId = event.target + '_' + Date.now()
const { data } = await axios.post(`/converse/${userId}/secured?include=nlu`, { text }, config)
const result = _.get(data, 'nlu.intent.name', 'N/A')
if (result === intent) {
bp.logger.debug(`✅ OK "${text}"`)
ok++
} else {
let message = `🚫 FAIL "${text}" Expected: ${intent} | But was: ${result}`
bp.logger.debug(message)
}
}
bp.logger.debug(`------------- END --------------------`)
bp.logger.debug(`Score: ${ok} / ${tests.length} (${(ok / tests.length * 100).toFixed(1)}%)`)
}
return myAction(args.justNone)
/** Your code ends here */
}
@chetan2914
Copy link

chetan2914 commented Jun 22, 2020

I am embedding a chatbot in my android app and getting response using converse api. i used botpress to create a chatbot.
how do i reset the conversation of the bot by a post method using converse api?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment