Skip to content

Commit e968bef

Browse files
authored
Merge pull request #1253 from Apollon77/slotfixerr
fix: Fix crash on initial slot fill without a former question asked
2 parents b5adedb + 014798d commit e968bef

File tree

3 files changed

+152
-1
lines changed

3 files changed

+152
-1
lines changed

packages/nlp/test/nlp.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,77 @@ describe('NLP', () => {
11651165
'When do you want to travel from {{fromCity}} to {{toCity}}?'
11661166
);
11671167
});
1168+
test('On initial processing slotFill.latestSlot is not set in response (because no asked slot filled now)', async () => {
1169+
const corpus = {
1170+
name: 'basic conversations',
1171+
locale: 'en-us',
1172+
entities: {
1173+
clientName: {
1174+
trim: [
1175+
{
1176+
position: 'betweenLast',
1177+
leftWords: ['is', 'am'],
1178+
rightWords: ['.'],
1179+
},
1180+
{
1181+
position: 'afterLast',
1182+
words: ['is', 'am'],
1183+
},
1184+
],
1185+
},
1186+
location: {
1187+
trim: [
1188+
{
1189+
position: 'betweenLast',
1190+
leftWords: ['in', 'around'],
1191+
rightWords: ['today', 'currently', 'at'],
1192+
},
1193+
{
1194+
position: 'afterLast',
1195+
words: ['in', 'around', 'to', 'at', 'from'],
1196+
},
1197+
],
1198+
},
1199+
},
1200+
data: [
1201+
{
1202+
intent: 'user.introduce',
1203+
utterances: ['i am @clientName', 'my name is @clientName'],
1204+
answer: [
1205+
'Nice to meet you @clientName.',
1206+
"It's a pleasure to meet you @clientName.",
1207+
],
1208+
slotFilling: {
1209+
clientName: "I'm sorry but i didn't get your name",
1210+
location: 'Where are you from @clientName?',
1211+
},
1212+
},
1213+
],
1214+
};
1215+
const nlp = new Nlp({
1216+
languages: ['en'],
1217+
autoSave: false,
1218+
});
1219+
await nlp.addCorpus(corpus);
1220+
expect(nlp.ner.rules.en).toBeDefined();
1221+
expect(nlp.ner.rules.en.clientName).toBeDefined();
1222+
expect(nlp.ner.rules.en.location).toBeDefined();
1223+
expect(nlp.slotManager.intents['user.introduce']).toBeDefined();
1224+
1225+
await nlp.train();
1226+
const input = {
1227+
locale: 'en',
1228+
text: 'my name is John',
1229+
};
1230+
const actual = await nlp.process(input);
1231+
expect(actual.intent).toEqual('user.introduce');
1232+
expect(actual.entities).toBeDefined();
1233+
expect(actual.entities[0].entity).toEqual('clientName');
1234+
expect(actual.entities[0].sourceText).toEqual('John');
1235+
expect(actual.slotFill).toBeDefined();
1236+
expect(actual.slotFill.currentSlot).toEqual('location');
1237+
expect(actual.slotFill.latestSlot).toBeUndefined();
1238+
});
11681239
test('The corpus can contain entities with action details', async () => {
11691240
const corpus = {
11701241
name: 'Slot Filling Corpus',

packages/slot/src/slot-manager.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,10 @@ class SlotManager {
292292
entities: result.entities,
293293
answer: result.answer,
294294
srcAnswer: result.srcAnswer,
295-
latestSlot: context.slotFill.latestSlot,
296295
};
296+
if (context.slotFill && context.slotFill.latestSlot) {
297+
result.slotFill.latestSlot = context.slotFill.latestSlot;
298+
}
297299
const currentSlot = mandatorySlots[keys[0]];
298300
result.slotFill.currentSlot = currentSlot.entity;
299301
result.srcAnswer = currentSlot.locales[result.localeIso2];

packages/slot/test/slot-manager.test.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,84 @@ describe('Slot Manager', () => {
410410
entities: [],
411411
});
412412
});
413+
test('On initial slotfill, fill in but leave latestFilled empty', () => {
414+
const manager = new SlotManager();
415+
manager.addSlot('intent', 'entity1', true);
416+
const result = {
417+
intent: 'intent',
418+
utterance: 'hello John',
419+
score: 1,
420+
entities: [
421+
{
422+
sourceText: 'John',
423+
utteranceText: 'John',
424+
entity: 'entity1',
425+
},
426+
],
427+
};
428+
const context = {};
429+
const actual = manager.process(result, context);
430+
expect(actual).toBeTruthy();
431+
expect(result).toEqual({
432+
intent: 'intent',
433+
utterance: 'hello John',
434+
score: 1,
435+
entities: [
436+
{
437+
entity: 'entity1',
438+
utteranceText: 'John',
439+
sourceText: 'John',
440+
},
441+
],
442+
});
443+
});
444+
test('On initial slotfill, fill in but leave latestFilled empty, and ask for another entity', () => {
445+
const manager = new SlotManager();
446+
manager.addSlot('intent', 'entity1', true);
447+
manager.addSlot('intent', 'entity2', true, { en: 'answer' });
448+
const result = {
449+
intent: 'intent',
450+
utterance: 'hello John',
451+
score: 1,
452+
localeIso2: 'en',
453+
entities: [
454+
{
455+
sourceText: 'John',
456+
utteranceText: 'John',
457+
entity: 'entity1',
458+
},
459+
],
460+
};
461+
const context = {};
462+
const actual = manager.process(result, context);
463+
expect(actual).toBeTruthy();
464+
expect(result).toEqual({
465+
intent: 'intent',
466+
utterance: 'hello John',
467+
score: 1,
468+
localeIso2: 'en',
469+
entities: [
470+
{
471+
entity: 'entity1',
472+
utteranceText: 'John',
473+
sourceText: 'John',
474+
},
475+
],
476+
slotFill: {
477+
currentSlot: 'entity2',
478+
entities: [
479+
{
480+
entity: 'entity1',
481+
utteranceText: 'John',
482+
sourceText: 'John',
483+
},
484+
],
485+
intent: 'intent',
486+
localeIso2: 'en',
487+
},
488+
srcAnswer: 'answer',
489+
});
490+
});
413491
test('If slot fill is waiting for an entity, fill the entity', () => {
414492
const manager = new SlotManager();
415493
manager.addSlot('intent', 'entity1', true);

0 commit comments

Comments
 (0)