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

feat(server): prepare action loop feature

This commit is contained in:
louistiti 2022-04-07 22:09:01 +08:00
parent 8b56a1850c
commit 19e1aa22f6
No known key found for this signature in database
GPG Key ID: 0A1C3B043E70C77D
7 changed files with 73 additions and 40 deletions

View File

@ -7,7 +7,12 @@
},
{
"method": "GET",
"route": "/api/action/games/guess_the_number/pick_up",
"route": "/api/action/games/guess_the_number/setup",
"params": []
},
{
"method": "GET",
"route": "/api/action/games/guess_the_number/guess",
"params": []
},
{

View File

@ -158,6 +158,7 @@ class Brain {
executionTime
})
} else {
console.log('brain obj', obj)
const { nluDataFilePath, classification: { action: actionName } } = obj
const { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8'))
const { type: actionType } = actions[actionName]

View File

@ -58,25 +58,24 @@ class Conversation {
entities
} = contextObj
const slotKeys = Object.keys(slots)
const [skillName] = intent.split('.')
const newContextName = `${domain}.${skillName}`
// If slots are required to trigger next actions, then go through the context activation
if (slotKeys.length > 0) {
const { actions } = JSON.parse(fs.readFileSync(nluDataFilePath, 'utf8'))
const actionsKeys = Object.keys(actions)
// Grab output context from the NLU data file
const { output_context: outputContext } = actions[actionName]
// Define next action
const [nextAction] = actionsKeys.filter((key) => actions[key].input_context === outputContext)
// Grab next action from the NLU data file
const { next_action: nextAction } = actions[actionName]
/**
* If a new context is triggered
* then save the current active context to the contexts history
*/
if (this._activeContext.name !== outputContext) {
if (this._activeContext.name !== newContextName) {
this.pushToPreviousContextsStack()
// Activate new context
this._activeContext = {
name: outputContext,
name: newContextName,
domain,
intent,
currentEntities: [],
@ -88,7 +87,7 @@ class Conversation {
}
log.title('Conversation')
log.info(`New active context: ${outputContext}`)
log.info(`New active context: ${newContextName}`)
}
this.setSlots(lang, entities, slots)

View File

@ -92,6 +92,7 @@ class Nlu {
* TODO: split this method into several methods
*/
process (utterance, opts) {
console.log('this.conv.activeContext', this.conv.activeContext)
const processingTimeStart = Date.now()
return new Promise(async (resolve, reject) => {
@ -310,10 +311,20 @@ class Nlu {
// Pass context entities to the NLU result object
this.nluResultObj.entities = this.conv.activeContext.entities
console.log('this.conv.activeContext', this.conv.activeContext)
try {
// TODO: next action based on next_action
const data = await this.brain.execute(this.nluResultObj, { mute: opts.mute })
if (this.conv.activeContext.name === 'setup_game') {
// If it is a loop action
// TODO: remove this
data.loop = true
if (data.loop) {
this.conv.activeContext.nextAction = 'guess'
}
}
const processingTimeEnd = Date.now()
const processingTime = processingTimeEnd - processingTimeStart
@ -411,10 +422,6 @@ class Nlu {
classification: {
domain,
skill: skillName,
/**
* Use the next action if it is defined via the skill NLU config
* or take the classified action
*/
action: this.conv.activeContext.nextAction,
confidence: 1
}
@ -422,12 +429,7 @@ class Nlu {
this.conv.cleanActiveContext()
const data = await this.brain.execute(this.nluResultObj, { mute: opts.mute })
// If it is a loop action
// TODO: remove this
data.loop = true
return data
return this.brain.execute(this.nluResultObj, { mute: opts.mute })
}
this.conv.cleanActiveContext()

View File

@ -31,29 +31,17 @@
]
}
],
"output_context": "setup_game"
"next_action": "setup"
},
"pick_up": {
"setup": {
"type": "logic",
"input_context": "setup_game",
"answers": {
"bigger": [
"The number is bigger.",
"Try with a bigger number."
],
"smaller": [
"It is smaller.",
"Try a smaller number."
],
"guessed": [
"Congrats! The number was %nb% and you guessed in %attempts_nb% attempts. Ready for another round?"
]
},
"output_context": "set_restart"
"next_action": "guess"
},
"guess": {
"type": "logic"
},
"decide": {
"type": "dialog",
"input_context": "set_restart",
"slots": [
{
"name": "decision",
@ -71,6 +59,17 @@
"answers": {
"ready": [
"Alright, I have set %players_nb% players with the %email_test% email. Go ahead and guess the number between 0 and 100!"
],
"bigger": [
"The number is bigger.",
"Try with a bigger number."
],
"smaller": [
"It is smaller.",
"Try a smaller number."
],
"guessed": [
"Congrats! The number was %nb% and you guessed in %attempts_nb% attempts. Ready for another round?"
]
}
}

View File

@ -0,0 +1,25 @@
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import utils
def guess(params):
"""This is a test"""
entities, slots = params['entities'], params['slots']
given_nb = -1
nb_to_guess = 42 # TODO: pick up from DB
# Find entities
for item in params['entities']:
if item['entity'] == 'number':
given_nb = item['resolution']['value']
if given_nb == nb_to_guess:
return utils.output('end', 'guessed', '....CONGRATS....')
if nb_to_guess < given_nb:
# TODO: enable loop
return utils.output('end', 'smaller', utils.translate('smaller')
if nb_to_guess > given_nb:
# TODO: enable loop
return utils.output('end', 'bigger', utils.translate('bigger')

View File

@ -3,7 +3,7 @@
import utils
def pick_up(params):
def setup(params):
"""This is a test"""
entities, slots = params['entities'], params['slots']
@ -11,6 +11,8 @@ def pick_up(params):
counter = 0
nb = 42
# TODO: save these values in DB
# if "not init" phase: check nb + increment counter
# TODO: "loop" option to return to the core