1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-11-27 08:06:03 +03:00

feat(server): resolve slots from slot filling

This commit is contained in:
louistiti 2022-03-26 19:51:50 +08:00
parent 3bbc2f8a25
commit 960a6dc71c
No known key found for this signature in database
GPG Key ID: 7ECA3DD523793FE6
2 changed files with 50 additions and 7 deletions

View File

@ -103,7 +103,7 @@ export default () => new Promise(async (resolve, reject) => {
*/
if (slotObj.source.type === 'entity') {
nlp.slotManager
.addSlot(intent, slotObj.source.name, true, { [lang]: slotObj.questions })
.addSlot(intent, `${slotObj.name}#${slotObj.source.name}`, true, { [lang]: slotObj.questions })
}
/* nlp.slotManager
.addSlot(intent, 'boolean', true, { [lang]: 'How many players?' }) */

View File

@ -113,7 +113,7 @@ class Nlu {
const result = await this.nlp.process(utterance)
const {
locale, domain, intent, score, answers, slotFill, srcAnswer
locale, domain, intent, score, answers
} = result
const [skillName, actionName] = intent.split('.')
let obj = {
@ -223,16 +223,59 @@ class Nlu {
}
}
// TODO: slot filling
console.log('result', result)
if (slotFill && srcAnswer) {
const answer = srcAnswer[Math.floor(Math.random() * srcAnswer.length)]
// TODO: create specific method for that
const getSlots = async (intent, entities) => {
const slots = await this.nlp.slotManager.getMandatorySlots(intent)
const keys = Object.keys(slots)
const newSlots = []
this.brain.talk(answer)
for (let i = 0; i < keys.length; i += 1) {
const key = keys[i]
const slotObj = slots[key]
const [slotName, slotEntity] = key.split('#')
const [foundEntity] = entities.filter(({ entity }) => entity === slotEntity)
const questions = slotObj.locales[this.brain.lang]
const question = questions[Math.floor(Math.random() * questions.length)]
newSlots.push({
name: slotName,
entity: slotEntity,
value: foundEntity,
isFilled: !!foundEntity,
question
})
}
return newSlots
}
const slots = await getSlots(intent, obj.entities)
console.log('slots', slots)
const notFilledSlots = slots.filter((slot) => !slot.isFilled)
if (notFilledSlots.length > 0) {
this.brain.talk(notFilledSlots[0].question)
this.brain.socket.emit('is-typing', false)
return resolve()
}
// TODO: fill with contexts?
obj.slots = slots
// console.log('getIntentEntityNames',
// await this.nlp.slotManager.getIntentEntityNames(intent))
// ['number']
// console.log('getMandatorySlots', await this.nlp.slotManager.getMandatorySlots(intent))
/*
number: {
intent: 'guess_the_number.start',
entity: 'number',
mandatory: true,
locales: { en: [Array] }
}
*/
try {
// Inject action entities with the others if there is
const data = await this.brain.execute(obj, { mute: opts.mute })