Using Context

Overview

Flask-assitant supports API.AI’s concept of contexts.

Contexts help to store and persist accessible information over multiple requests and define the “state” of the current session. You can create different contexts based on the actions your assistant performs and use the generated contexts to determine which intents may be triggered (and thus which actions may take place) in future requests.

The use of contexts allows for a more dynamic dialogue and is helpful for differentiating phrases which may be vague or have different meanings depending on the user’s preferences or geographic location, the current page in an app, or the topic of conversation.

Intents may require input contexts, and their actions may set output contexts.

Context Objects

Input Contexts

  • Input contexts limit intents to be matched only when certain contexts are set.
  • They essentially act as requirements for a particular intent’s action function to be called.
  • They are received in every request from API.AI under a “contexts” element, and consist of any previously declared output contexts

Output Contexts

  • Output contexts are set by actions to share information across requests within a session and are received as Input Contexts for future intents.
  • If an input context is modified within in action, the changes will persist via a new output context.

In a REST-like fashion, all declared contexts are received in every request from API.AI and included in every response from your assistant. Flask-assistant provides the context_manager to automatically handle this exchange and preserve the state of the conversation.

Context Manager

The context_manager is used to declare, access, and modify context objects. It contains the input contexts recieved from the API.AI request and appends any new or modified contexts to the flask-assistant response.

from flask_assistant import context_manager

Add a new context:

context_manager.add('context-name')

Retrieve a declared context:

my_context = context_manager.get('context-name')

Set a parameter value:

context_manager.set('context-name', 'param_name', value)

context decorator

The context() decorator restricts a wrapped action function to be matched only if the given contexts are active.

While the context_manager() is used create and access context objects, the context() decorator is responsible for mapping an intent to one of possibly many context-dependent action functions.

The basic action() intent-mapping in conjuction with context() action filtering allows a single intent to invoke an action appropriate to the current conversation.

For example:

@assist.action('give-diet')
def set_user_diet(diet):
    speech = 'Are you trying to make food or get food?'
    context_manager.add(diet)
    return ask(speech)

@assist.context('vegetarian')
@assist.action('get-food')
def suggest_food():
    return tell("There's a farmers market tonight.")

@assist.context('carnivore')
@assist.action('get-food')
def suggest_food():
    return tell("Bob's BBQ has some great tri tip")

@assist.context('broke')
@assist.action('get-food')
def suggest_food():
    return tell("Del Taco is open late")