Skip to content

Commit

Permalink
fix(skills): fixed slot extraction skill
Browse files Browse the repository at this point in the history
  • Loading branch information
slvnperron committed May 4, 2019
1 parent 22f9eb1 commit f5cd610
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 46 deletions.
34 changes: 23 additions & 11 deletions modules/basic-skills/src/actions/slot_fill.js
Expand Up @@ -2,28 +2,40 @@
* Extract entities that match a slot
* @hidden true
* @param slotName The name of the slot to extract. (e.g. destination_from)
* @param entityName The entity of the slot. (e.g. City)
* @param entitiesName The entities of the slot. (e.g. City)
*/
const slotFill = async (slotName, entityName) => {
if (event.nlu.entities && event.nlu.entities.length) {
const slotFill = async (slotName, entitiesName) => {
const entities = entitiesName.split(',')
if (entities && entities.length && event.nlu.entities && event.nlu.entities.length) {
for (const entity of event.nlu.entities) {
if (entity.name === entityName) {
setSlot(slotName, entity.data.value)
if (entities.includes(entity.name)) {
setSlot(slotName, entity)
}
}
} else if (entityName === 'any') {
setSlot(slotName, event.payload.text)
} else if (entities && entities.includes('any')) {
const value = event.payload.text || event.preview
setSlot(slotName, {
name: 'any',
type: 'any',
meta: { start: 0, end: value.length },
data: { extras: {}, value: value }
})
}
}

const setSlot = (slotName, value) => {
const setSlot = (slotName, entity) => {
if (!session.extractedSlots[slotName]) {
session.extractedSlots[slotName] = {
value,
timestamp: Date.now()
name: slotName,
value: entity.data.value,
entity: entity,
timestamp: Date.now(),
turns: 0,
overwritable: true,
expiresAfterTurns: false // BETA(11.8.4): Set this to a number to expire the slot after 'N' turns
}
session.extractedSlots.notFound = 0
}
}

return slotFill(args.slotName, args.entity)
return slotFill(args.slotName, args.entities)
3 changes: 2 additions & 1 deletion modules/basic-skills/src/backend/slot.ts
@@ -1,4 +1,5 @@
import * as sdk from 'botpress/sdk'

import { Transition } from './typings'

const generateFlow = async (data: any, metadata: sdk.FlowGeneratorMetadata): Promise<sdk.FlowGenerationResult> => {
Expand Down Expand Up @@ -26,7 +27,7 @@ const createNodes = data => {
const slotExtractOnReceive = [
{
type: sdk.NodeActionType.RunAction,
name: `basic-skills/slot_fill {"slotName":"${data.slotName}","entity":"${data.entity}"}`
name: `basic-skills/slot_fill {"slotName":"${data.slotName}","entities":"${data.entities}"}`
}
]

Expand Down

This file was deleted.

@@ -0,0 +1,54 @@
const _ = require('lodash')

// Make sure extractedSlots exists
event.state.session.extractedSlots = event.state.session.extractedSlots || {}
rotateTurns()
extractIntentSlots()
extractFromEntityResponse()

function extractIntentSlots() {
const slots = _.flatten(_.values(event.nlu.slots)).filter(x => !!x.value) // only non-null slots
for (let slot of slots) {
// BETA(11.8.4): Prevent overwrite of the slot if explicitely demanded
if (
event.state.session.extractedSlots[slot.name] &&
event.state.session.extractedSlots[slot.name].overwritable == false
) {
continue
}

// Slot is an array when the NLU is confused about the results
// The array is sorted by confidence so we take the first index
if (Array.isArray(slot)) {
slot = slot[0]
}

event.state.session.extractedSlots.notFound = 0
event.setFlag(bp.IO.WellKnownFlags.FORCE_PERSIST_STATE, true)
event.state.session.extractedSlots[slot.name] = {
...slot,
timestamp: Date.now(),
turns: 0,
overwritable: true,
expiresAfterTurns: false // BETA(11.8.4): Set this to a number to expire the slot after 'N' turns
}
}
}

function rotateTurns() {
for (let slot of _.values(event.state.session.extractedSlots)) {
if (typeof slot.turns === 'number') {
++slot.turns
}

// BETA(11.8.4): Automatically expire the slot after X dialog turns
if (typeof slot.expiresAfterTurns === 'number' && slot.turns >= slot.expiresAfterTurns) {
delete event.state.session.extractedSlots[slot.name]
}
}
}

function extractFromEntityResponse() {
if (event.state.session.extractedSlots.__currentSlot) {
}
}
2 changes: 1 addition & 1 deletion modules/basic-skills/src/views/full/slot.jsx
Expand Up @@ -56,7 +56,7 @@ export class Slot extends React.Component {
validationAction: this.state.selectedActionOption && this.state.selectedActionOption.value,
intent: intent && intent.name,
slotName: slot && slot.name,
entity: slot && slot.entity
entities: slot && slot.entities
}

this.props.onDataChanged && this.props.onDataChanged(data)
Expand Down

0 comments on commit f5cd610

Please sign in to comment.