Skip to content

Commit

Permalink
feat(server): prepare action loop feature
Browse files Browse the repository at this point in the history
  • Loading branch information
louistiti committed Apr 7, 2022
1 parent 8b56a18 commit 19e1aa2
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 40 deletions.
7 changes: 6 additions & 1 deletion core/skills-endpoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": []
},
{
Expand Down
1 change: 1 addition & 0 deletions server/src/core/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
15 changes: 7 additions & 8 deletions server/src/core/conversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand All @@ -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)
Expand Down
26 changes: 14 additions & 12 deletions server/src/core/nlu.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -411,23 +422,14 @@ 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
}
}

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()
Expand Down
35 changes: 17 additions & 18 deletions skills/games/guess_the_number/nlu/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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?"
]
}
}
25 changes: 25 additions & 0 deletions skills/games/guess_the_number/src/actions/guess.py
Original file line number Diff line number Diff line change
@@ -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')
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@

import utils

def pick_up(params):
def setup(params):
"""This is a test"""

entities, slots = params['entities'], params['slots']
# if "init" phase: pickup nb and set counter
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
Expand Down

0 comments on commit 19e1aa2

Please sign in to comment.