One of the main advantages Cognigy.AI provides is the conversational context. That context is used in order to, temporarily, remember information such as a given answer or any kind of custom data.
Why Session Storage?
Because of the fact that the transformer function is only executed once for every incoming and outgoing message, there is no context available. From default, everything is overwritten as soon as a new message arrives. Therefore, one has to use the Session Storage in the Endpoint Transformer Function if information should be stored temporarily.
How to use the Session Storage?
In most cases, the Flow sends some information to the endpoint so that the transformer function knows what to do next. The following example asks the user in which language the conversation should continue, while the language is used in the transformer in order to translate messages.
As can be seen in the image above, the user selects German as the target language so that the message "Good Day" is translated to "Guten Tag".
Inside the Flow
The Cognigy Flow uses a Question and a Say node for this process:
The Say node, in this case, called "Inform Transformer" look such as the following:
{
"language": "{{context.language}}"
}
While the text stays empty, the Data Field is used for sending the information about the previously selected language to the Output Transformer Function.
Inside the Endpoint Transformer Function
As soon as the, above shown, DATA ONLY message is sent, the Output Transformer Function can handle it and store it into the Session Storage:
handleOutput: async ({ processedOutput, output, endpoint, userId, sessionId }) => { // Create Session Storage const sessionStorage = await getSessionStorage(userId, sessionId); // Check if language information is provided if (processedOutput?.data?.language) { sessionStorage.language = processedOutput.data.language; } return processedOutput; },
Similar to the Add To Context Flow Node, the getSessionStorage() function in cooperation with the sessionStorage.language = ... can manipulate this type of memory. In this case, the function checks if there is a piece of information about the language provided and stores it.
Afterward, the Input Transformer Function extracts this information from the storage and uses it, for example, to translate the user input text:
handleInput: async ({ payload, endpoint }) => { // Get the Session Storage const sessionStorage = await getSessionStorage(payload?.userId, payload?.sessionId); // Get stored language const language = sessionStorage?.language; // Now the language could be used in order to tanslate the user's text, for example payload.text = await translate(payload.text, language) return { userId: payload.userId, sessionId: payload.sessionId, text: payload.text, data: payload.data }; },
Comments
0 comments