Contexts

Contexts represent the current context of a user's request. This is helpful for differentiating phrases which may be vague or have different meanings depending on the user’s preferences, geographic location, the current page in an app, or the topic of conversation.

For example, if a user is listening to music and finds a band that catches their interest, they might say something like: “I want to hear more of them”. As a developer, you can include the name of the band in the context with the request, so that the agent can use it in other intents.

Or let’s say you’re a manufacturer of smart home devices, and you have an app that remotely controls lights and household appliances. A user might say, "Turn on the front porch light", followed by “Turn it off”. By setting a context the app will understand that the second phrase is referring to the light from the first request. Later, if the user says, "Turn on the coffee machine", and then “Turn it off”, it will result in different action than before, because of the new context.

Adding Contexts

To define and add a context to an intent, click in the Add input context or Add output context field and type the desired name of the context and hit Enter to commit it.

Lifespan

By default, contexts expire after either five requests or ten minutes from the time they were activated. Intents that renew the context will reset the counter and clock to give an additional five requests and ten minutes.

You can change the lifespan of the context by clicking on the current lifespan and entering the desired number of requests. Setting the lifespan to 0 will reset the content when it is matched.

Output Contexts

Contexts are tied to user sessions (a session ID that you pass in API calls). If a user expression is matched to an intent, the intent can then set an output context to be shared by this expression in the future. You can also add a context when you send the user request to your API.AI agent.

In our appliance app example, saying "Turn on the front porch light" should set an output context to "front-porch-light".

There may be several intents that respond to "Turn it off", each with a different input context. Since the output context is set to "front-porch-light", the intent for "Turn it off" that has a matching input context of "front-porch-light" will be executed, and all others will not.

Input Contexts

Input contexts limit intents to be matched only when certain contexts are set.

For the music example, we would create two intents for requests like “I want to hear more of them”. The intent without context will match the user’s request when the app has no information about the artist, so it will need to get that info from the user. This is done by marking parameters as required and providing prompts.

The other intent that we create has an input context, which contains information about which artist or band is meant by the user’s request.

Extracting Parameter Values from Contexts

To refer to a parameter value that has been extracted in an intent with a defined output context, use the following format in the VALUE column: #context_name.parameter_name

Read more here.