DeepPavlov

DeepPavlov is a comprehensive open source framework for building chatbots and other conversational agents for a variety of purposes and tasks. While this bot is designed for more goal-oriented bots, it will suit us well, as it is full-featured and includes several sequence-to-sequence model variations. Let's take a look at how to build a simple pattern (sequence-to-sequence) recognition model in the following steps:

  1. Up until now, we have kept our Python environment loose, but that has to change. We now want to isolate our development environment so that we can easily replicate it to other systems later. The best way to do this is working with Python virtual environments. Create a new environment and then activate it with the following commands at an Anaconda window:
#Anaconda virtual environment
conda create --name dlgames
#when prompted choose yes
activate dlgames
  1. If you don't use Anaconda, the process is a bit more involved, as follows:
#Python virtual environment
pip install virtualenv
virutalenv dlgames

#on Mac
source dlgames/bin/activate

#on Windows
dlgamesScriptsactivate
  1. Then we need to install DeepPavlov with the following command at a shell or an Anaconda window:
pip install deeppavlov
  1.  This framework will attempt to install several libraries and may disrupt any existing Python environments. This is the other reason we are now using virtual environments.
  1. For our purposes, we are just going to look at the basic Hello World sample that is very simple to follow now that we have covered the background. We first do our imports as per standard as follows:
from deeppavlov.skills.pattern_matching_skill import PatternMatchingSkill
from deeppavlov.agents.default_agent.default_agent import DefaultAgent
from deeppavlov.agents.processors.highest_confidence_selector import HighestConfidenceSelector
  1. Now, DeepPavlov is based on Keras, but as you can see, the types we are using here wrap the functionality of a sequence-to-sequence pattern-matching model. The PatternMatchingSkill represents the sequence-to-sequence model we want to give our chatbot agent. Next, we import the DefaultAgent type, which is just the basic agent. After that, we introduce a confidence selector called HighestConfidenceSelector. Remember that the thought vector we generate is a vector of probabilities. The HighestConfidenceSelector selector always chooses the highest value relation or context that matches the corresponding word. 
  2. Next, we generate three sets of patterns with corresponding responses, shown in the following code:
hello = PatternMatchingSkill(responses=['Hello world!'], patterns=["hi", "hello", "good day"])
bye = PatternMatchingSkill(['Goodbye world!', 'See you around'], patterns=["bye", "ciao", "see you"])
fallback = PatternMatchingSkill(["I don't understand, sorry", 'I can say "Hello world!"'])
  1. Each PatternMatchingSkill represents a set of pattern/response-contextual pairs. Note how there may be multiple responses and patterns for each. The other great thing about this framework is the ability to interchange and add skills. In this case, we are using just pattern matching, but there are plenty of other skills the reader is encouraged to explore.
  2. Finally, we build the agent and run it by simply printing the results with the final bit of code:
HelloBot = DefaultAgent([hello, bye, fallback], skills_selector=HighestConfidenceSelector())

print(HelloBot(['Hello!', 'Boo...', 'Bye.']))

  1. This last section of code creates a DefaultAgent with the three skills (hello, bye, and fallback) using the HighestConfidenceSelector. Then it runs the agent by feeding a set of three inputs nested inside the print statement.
  2. Run the code as you normally would and look at the output. Is it what you expected?

The simplicity of DeepPavlov makes it an excellent tool to build up various conversational chatbots for your games or other purposes if you so choose. The framework itself is very broad-featured and provides multiple natural language processing tools for a variety of tasks, including goal-oriented chatbots. Whole books could and probably should be written about Pavlov; if you have an interest in this, look more for NLP and DeepPavlov.

With our new tool in hand, we now need a platform in which to serve up our bots with great conversational abilities. In the next section, we explore how to build a server for our bot.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset