Input and output contexts are applied to intents. They work together to control conversation flow:
- Output contexts control active contexts. When an intent is matched, any configured output contexts for that intent become active.
- Input contexts control intent matching. While contexts are active, Dialogflow is more likely to match intents that are configured with input contexts that are a subset of currently active contexts.
With contexts, you can:
- Control the order of intent matching.
- Create context-specific intents with the same training phrases.
For example, consider the following intents:
|Intent name||Training phrases||Input contexts||Output contexts|
|dog-show||"Show me a picture"||dogs||-|
|cat-show||"Show me a picture"||cats||-|
pet-choose intent asks the end-user to select dogs or cats.
Once selected, it sets the selection to a
and uses the parameter as the output context.
cat-show intents show a picture of dogs or cats,
depending on the active context.
Multiple output contexts can be applied to an intent. When an intent is matched, any output contexts applied to the intent become active.
Parameter references for active contexts
When an intent with output context is matched, its collected parameter values can serve as temporary storage with parameter references for active contexts. These values can be accessed during future intent matching, for as long as the context remains active.
For example, consider the following variation of the pet example above:
|Intent name||Training phrases||Parameters||Input contexts||Output contexts||Response|
|pet-choose||"dogs", "cats"||$pet||-||pet-chosen||"You chose $pet"|
|pet-show||"Show me a picture"||-||pet-chosen||-||"Here is a picture of a #pet-chosen.pet"|
pet-choose intent sets the
to the value chosen by the end-user, and it activates the
pet-show intent is matched,
the intent response can access the
$pet parameter via the active context.
Each active context has a lifespan that defines the number of conversational turns for which the context remains active. The default lifespan is five conversational turns for normal intents and two conversational turns for follow-up intents. You can override the default lifespan for any contexts. In addition, all contexts expire after 20 minutes of becoming active. When an intent is matched with an output context that is already active, the lifespan and expiration timer are reset.
For example, consider the following dialog with a pet information agent.
The lifespan for the
cats context is two turns.
|1||Agent: What kind of pet do you want to know about?||The
|2||User: I like cats.
Agent: What do you want to know about cats?
|3||User: What do they look like?
Agent: Here's a cat picture. What else do you want to know?
|4||User: What do they sound like?
Agent: Here's a cat sound. What else do you want to know?
|5||User: How big are they?
Agent: What kind of pet do you want to know about?
While contexts are active, Dialogflow is more likely to match intents that are configured with input contexts that correspond to the currently active contexts. The detailed intent matching rules are as follows:
- An intent's input context list must be a subset of the currently active context list in order to be matched. This also applies to intents with no input contexts.
- For intents that satisfy rule #1, those with an input context list that most closely match the currently active context list are more likely to be matched.
- Intents with no input contexts can be matched at any time.
- When no contexts are active, only intents with no input contexts can be matched.
- If contexts "A" and "B" are active, an intent with "A" and "C" input contexts cannot be matched.
- If contexts "A", "B", and "C" are active, an intent with "A" and "B" input contexts can be matched.
- If contexts "A" and "B" are active, an intent with "A" and "B" input contexts is more likely to be matched than an intent with no input context or an intent with just "A" for an input context.