Hacking external Content into your Conversational AI

Christoph Bullmann
Cognigy.AI
Published in
5 min readFeb 15, 2019
Photo by Franck V. on Unsplash

I am currently working with the Cognigy chatbot platform. It provides a vast playground of possibilities for the hacky mindset and therefore will be used in the following example.

Preamble

Getting the mindset

Let’s take the common box analogy, say that we want to enlarge it and won’t care about best practices such as enlarging it with the same material that it is made of. You have to think outside the box to get the target dimensions, but there might be materials inside the box that you can use. If the box has a lid that you do not need, you can use it as material as well.

Imagine that you want something to exist. You either do not have the resources or do not want to spend the resources to build it from scratch. However, you have a decent enough substitute for your needed resources, which you could form, mold and twist until you have created your desired outcome.

Hacking software is usually a lot more forgiving than hacking hardware, since creating backups costs less time and effort, it is harder to create new boxes than copying files on a hard drive. If something breaks, you simply go back and try something else. When trial and error is such a viable option, you do not necessarily need to have a clear path, just a clear goal.

The Goal

Defining the box

When working with chatbot tools such as Amazon Alexa, Cognigy or Dialogflow you define “intents” (definitions of what the user wants with corresponding example utterances) and the chatbot tries to map one best matching intent to a single user input. I wanted a way to be able to map multiple intents to a single user input and it to be easily configurable.

So what does that mean? Usually you have intents such as “Where is the pizza I’ve ordered” or “I want to know the status of my Amazon package”, with corresponding answers such as “Your pizza is being delivered”, “You can track your parcel on …”. The problem we want to fix here is that if a user says “Where is the pizza I’ve ordered and what is the status of my Amazon package” either the pizza or the package intent and answer get triggered, instead we want both the pizza and the package intent to trigger and the user should receive the answers for both.

Key-Phrase Intents

Keeping it simple

Most NLP tools use example sentences to train their intents and build up a machine learning model from them to map user input to one of the intents. However, the example sentences can often be reduced to certain keywords and keyword combinations that are unique to that specific intent. If you think about it, “I want to order a pizza” and “I want to buy a car” are very similar sentences and the actual intent difference is only expressed by “pizza” and “car”. If you group “order” with “buy” together and have an opposite key-phrase “sell”, you can already distinguish between four intents:

  1. buy/order pizza
  2. buy/order car
  3. sell pizza
  4. sell car

Key-phrase intents excel in simple solutions for simple problems. However, they are ill-suited for “fuzzy” conversations such as small-talk. They are still used here as they represent an easy-to-understand starting point if you are new to conversational AI. They also allow us to outsource the intents to a table.

Outsourcing the Intents

Setting up the Google Sheets

The multiple intent mapping that we want needs to map the key-phrase intents to answers/output phrases and therefore be a two-dimensional matrix/table. Excel was the first thing to come to my mind, but I needed it to be accessible by the chatbot platform, so the Google Sheets API was the cheapest thing that fulfilled the requirements.

The intents would be simple key-phrase triggers, this ensured that one column of a sheet was able to map one intent:

  • The first line would contain the “intent name” or tag
  • All following lines would contain trigger key-phrases
The intent names and key-phrase triggers in the Google Sheet

The mapping sheet contains all the answers in the first column, the other columns all map the intents/tags:

  • “t” means that tag needs to be triggered
  • “n” means that the tag is not allowed to be triggered
  • An empty cell means that it does not matter whether the tag is triggered or not

This approach allows a lot of possibilities, generally speaking you want each line to have a unique combination of tags. If you have multiple lines with the same combination, all of them will be triggered by the same user input, in the same order they are in the sheet.

The answers and the intent mapping

In the example given, the answers from line 4 and 5 can be triggered with one user input, as they are not excluding each other. If none of the tags is triggered the default answer from line 6 is presented.

Connecting the Chatbot

Implementing the Cognigy connection

Cognigy has HTTP-Nodes that are able to retrieve the necessary sheets from the Google Sheets API and allows its Code-Nodes to create Lexicons on the fly. With these tools it only required a bit of coding to get the logic into the project.

The final Flow was very tiny, since nearly all of the conversational logic had been outsourced to the sheets file:

The Cognigy Flow

All of the Nodes that are attached to the Once-Node import the Flow logic from the Google Sheets and store it in “cc.answers” at the first user interaction/the session start. The Think-Node allows the user input to be processed for Slots by the generated Lexicon.

You can find the contents of the Code-Nodes on GitHub, the HTTP-Nodes use the following structure:

https://sheets.googleapis.com/v4/spreadsheets/[spreadsheetID]/values/[sheetName]?key=[yourAPIKey]

The End

Thinking final thoughts

We have completely ignored Cognigy’s Intents, which represent a large part of its core functionality, but achieved our goal in doing so. One thing to keep in mind is that the principles proposed here could actually be used together with Cognigy’s Intents if necessary.

I hope this article has given you a new perspective on chatbot tools and how to manipulate their inner workings to fit your specific needs. The best way to get a grasp on what is possible is to tinker around yourself.

Feel free to try it out on Cognigy Community!

--

--