1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-10-05 21:58:40 +03:00

feat(server): action loop support

This commit is contained in:
louistiti 2024-04-29 11:25:18 +08:00
parent 57923f83ee
commit d9f0144e62
No known key found for this signature in database
GPG Key ID: 92CD6A2E497E1669
4 changed files with 53 additions and 2 deletions

View File

@ -61,6 +61,12 @@
"I can't fulfill this request because my large language model is not enabled",
"Sorry, I need my large language model to answer this request, but it's not enabled",
"My large language model is not enabled, so I can't answer this request"
],
"action_loop_stopped": [
"Sure, let me know if you need anything else",
"Alright, let me know what you need next",
"Okay, let me know what I can do for you next",
"Sure thing"
]
}
}

View File

@ -3,7 +3,8 @@
"en-US": {
"short": "en",
"min_confidence": 0.5,
"fallbacks": []
"fallbacks": [],
"action_loop_stop_words": ["stop", "break", "exit"]
},
"fr-FR": {
"short": "fr",
@ -15,7 +16,8 @@
"skill": "welcome",
"action": "run"
}
]
],
"action_loop_stop_words": ["stop", "break", "exit"]
}
}
}

View File

@ -62,6 +62,31 @@ export default class NLU {
}
}
/**
* Check if the utterance should break the action loop
* based on the active context and the utterance content
*/
private shouldBreakActionLoop(utterance: NLPUtterance): boolean {
const loopStopWords = LangHelper.getActionLoopStopWords(BRAIN.lang)
const hasActiveContext = this.conversation.hasActiveContext()
const hasOnlyOneWord = utterance.split(' ').length === 1
const hasLessThan5Words = utterance.split(' ').length < 5
const hasStopWords = loopStopWords.some((word) =>
utterance.toLowerCase().includes(word)
)
const hasLoopWord = utterance.toLowerCase().includes('loop')
if (
(hasActiveContext && hasStopWords && hasOnlyOneWord) ||
(hasLessThan5Words && hasStopWords && hasLoopWord)
) {
LogHelper.info('Should break action loop')
return true
}
return false
}
/**
* Set new language; recreate a new TCP server with new language; and reprocess understanding
*/
@ -115,6 +140,15 @@ export default class NLU {
return reject(msg)
}
if (this.shouldBreakActionLoop(utterance)) {
this.conversation.cleanActiveContext()
BRAIN.talk(`${BRAIN.wernicke('action_loop_stopped')}.`, true)
SOCKET_SERVER.socket?.emit('is-typing', false)
return resolve({})
}
// Add spaCy entities
await NER.mergeSpacyEntities(utterance)

View File

@ -38,4 +38,13 @@ export class LangHelper {
public static getShortCode(longCode: LongLanguageCode): ShortLanguageCode {
return langs[longCode].short
}
/**
* Get action loop stop words of the given long language code
* @param shortCode The short language code of the language
* @example getActionLoopStopWords('en-US') // ["stop", "break", "exit"]
*/
public static getActionLoopStopWords(shortCode: ShortLanguageCode): string[] {
return langs[LangHelper.getLongCode(shortCode)].action_loop_stop_words
}
}