Lets Chat!

Knowledge Graph-based ChatBot

Mustaffa Hussain
Published in
5 min readOct 30, 2020

--

Let's design a Knowledge Graph-Based Chat Bot ourselves!

Knowledge Graphs have been around for quite some time now and they are used extensively. Google search is powered by Knowledge graphs. Damn right! If you don't know knowledge graphs, it's pretty simple. This will get you started.

Problem statement

The problem specified “Imagine a scenario of a family discussing a holiday plan. In the past, they have discussed going to Port Blair. Knowledge Graphs should have captured this dialog and should be able to recommend the same when a new discussion is happening months later on deciding on a holiday destination.

Let's make an approach note on the strategy, components of NLU — to build a Knowledge Graph and retrieval.

Overview

The use case scenario depicted here and tackled in the flow diagram is that of families using the chatbot for planning out holidays as well as other needs. The main complexity lies in the fact that Knowledge graphs work well with facts, but here we need to attach personalizations and remember the info by understanding the context and use it for recommendations when accessed months later.

Parts:

The approach has 3 parts.

  1. The generic workflow of the bot.
  2. The internal workflow of the bot.
  3. Internal subgraph mechanism.

Solution

1. The generic workflow of the bot

The bot is to function like any other chatbot from the front end. Here in the bot is triggered with user input and then reply from the bot.

2. The internal workflow of the bot

  1. The user input is passed to the intend identification layer for identification of intend, Herein we can use any Multi-class classification model for identification of existing intends in the Knowledge Graph. The intends are the relationships in the graph. It is common to run into cases where training data is scarce in the problem domain. We can work around that using Siamese Network.
  2. If any of the existing intends is not found, extract the keywords from the input using TFIDF from previous interactions+ NER + POS. We can use pre-trained spacy, Stanford NLP, fair NLP, etc models. Have look at flair as it offers pre-trained models for different domains. we can train one ourselves if needed.
  1. If Key words are found,[Found==True] and also a node, display the data in the node. Else return “can’t understand input/invalid input”. Show online search results if possible.
  2. If keywords are not found,[Found==False] then display “can’t understand input” and send it to check DB, which is considered for Knowledge graph updations and internal analysis.

Coming to the branch of Intend is found; There are two possibilities: direct statements and normal queries.

  1. Direct statement: Example “I want to go to goa”, “ I like Spain”. Here we use the special relationship of the person {vertex}— [TF] →place {vertex} and update the affinity relationship (like a term frequency) used as TF in the flow diagram. To determine if a given input is a statement we can use the structure (SVO, SOV) of language.
  2. Normal queries: example “tourist attractions in GOA”, “hotels in Spain”. Pass to the previous question context layer. This is important as when planning a trip, or any other activity, we tend to do it in parts by searching for related things.

Example: 1. cheapest places to travel, 2. tourist attractions in GOA, 3. Hotels near WAGA beach, 4. flight tickets from IGI to GOA

Here check if subsequent queries are in any of the relationships of the vertex. If the no of queries > threshold, update the affinity relationship(TF) of the person to place(person {vertex} — [TF] →place {vertex}).

Pass the input to NER/POS layer to get the Knowledge Graph query structure. The output of the NER/POS layer is used to generate the cypher query for results extraction from the knowledge graph. If TF/ affinity exists in a location as parsed from the NER/POS layer above, pass the recommendation query along to show the recommendations too along with the normal results.

3. The internal subgraph mechanism

Along with the internal workflow to remember info, it is also important to forget the recommendation data and prevent recommending older stuff over time. For this, we can use the decay function on the affinity relationship of the existing knowledge graph. The rate of decay can be set such that a TF/affinity becomes 0 over a determined span of months (3 months). So when new affinities are made their values are increasing and simultaneously the old affinities are dying in magnitude.

Moreover, it does not affect normal relationships.

An example set of questions with dummy flow

  1. I am and the: no intend, no important words comes to check DB for further internal analysis,
  2. Srilanka: no intend, imp word, Srilanka data shown
  3. cheapest places to travel: intend — cheap travel, NER/POS — Locations{vertice}
  4. tourist attractions in GOA: intend — attractions, NER/POS — Locations {name: GOA}
  5. Hotels near WAGA beach: intend — nearby, NER/POS — Locations {name: GOA}
  6. flight tickets from IGI to GOA- intend — flight, NER/POS — Airport {name: IGI} — [flight] — >Airport {name: GOA}
  7. I want to go to GOA: intend — visit, statement= TRUE, NER/POS- Locations {name: GOA}

Some PROs of using KG BOTS:

  1. Knowledge Graphs are Databases on steroids. They are blazingly fast and inherently handle interactions between different types of nodes.
  2. Moreover, the ability to keep data at the relationship (edge) and node (vertex) bring a lot of flexibility. This helps get rid of hierarchical templates which are often used in making bots.
  3. The ability to modify Knowledge Graphs at fly is a closing deal. It's very simple to add and alter the graph.

Wanna make one, let's collaborate. Hit me up on LinkedIn and drop in a Hi.

References

  1. https://ieeexplore.ieee.org/document/9165716
  2. https://onlim.com/en/using-knowledge-graphs-to-optimize-chatbot-conversations/
  3. https://chatbotslife.com/knowledge-graphs-and-chatbots-an-analytical-approach-a8dcc8649100
  4. https://www.slideshare.net/christophewillemsen/knowledge-graphs-chatbots-with-neo4j
  5. https://www.greenshootlabs.com/articles/chatbot-knowledge-graphs
  6. https://onlim.com/en/knowledge-graphs-and-conversational-ai/
  7. https://developer.kore.ai/docs/bots/bot-builder-tool/knowledge-task/creating-a-knowledge-graph/

--

--

Mustaffa Hussain

M.Sc Computer Science from South Asian University. I write to understand. Portfolio link- mustaffa-hussain.github.io/Portfolio