Switch AI context mid-call
In this example, we will demonstrate how to use context_switch to shift the focus of the conversation.
The AI agent will switch between a StarWars and StarTrek context. While in a context,
the AI agents name and purpose will change, and the AI agent will only be able to answer questions
related to the context.
Initial prompt
Providing too much information in the initial prompt can cause the AI agent to get confused during a context switch. It is recommended to keep the initial prompt short and simple. Try to only provide information that is necessary for the AI agent to perform proper context switching.
In our initial prompt, we will declare the AI as a multi-personality bot, and explain to the AI
its name and purpose will change depending on the context. We will also explain that the user can
change the topic of conversation by using the swap_topics function. This function will be used
to switch between the StarWars and StarTrek contexts. Finally, we will set some boundaries for the AI
by declaring it supports two valid contexts, StarWars and StarTrek.
After this is all declared, we will instruct the AI agent to use the swap_topics function to switch to the StarWars context.
This will cause the AI agent to start the call in the StarWars context.
Context switching
In this example, we will use the swap_topics function to switch between the StarWars and StarTrek contexts.
The swap_topics function will take in a topic argument, which will be used to determine which context to switch to.
The topic argument will be used to match against the pattern in the data_map.expressions array. If a match is found,
the output.action will be executed. If no match is found, we will fall back to the ".*" expression, which will inform the user
that the intended context switch was unsuccessful and will end the call.
Example Function:
Context switching options
In the data_map.expressions.output.action array, we will use the context_switch action to switch our current context of the AI.
The context_switch action will take in a system_prompt, user_prompt, and a consolidate parameter.
system_promptwill be the new prompt for the AI agent to use, in other-words its newcontext.user_promptwill be used as user input. It will tell the AI agent that it wants to talk about the new context.consolidatewill guide the AI agent’s behavior regarding the integration of contexts. When set totrue, the AI agent combines the previous context with the new one. If false, only the new context is considered.
StarWars context
In the StarWars context, the AI agent will be named Luke. The AI agent will only be able to answer questions related to StarWars.
The user_prompt argument is set to I want to talk about StarWars. This will be used as user input.
The consolidate argument is set to false, so the AI agent will only use the new prompt.
Example
StarTrek context
In the StarTrek context, the AI agent will be named Leonard. The AI agent will only be able to answer questions related to StarTrek.
The user_prompt argument is set to I want to talk about StarTrek. This will be used as user input.
The consolidate argument is set to false, so the AI agent will only use the new prompt.
Example
Final example
It’s important to remember the boundaries of the AI agent that you have set in the initial prompt, and in the new context. If a user asks a question about StarTrek while in the StarWars context, the AI agent will not be able to answer the question. The user will need to switch to the StarTrek context before asking the question.