Conversational Artifical intelligence
in python

Step by step process and explanation of code

Before we can start coding, all dependices must be installed. Make sure python is added to the PATH. In the program directory use this command to download the packages.

pip install transformers numpy

These packages will be imported in the begining of the python program.

import transformers
import os
import numpy as np

Now that the packages are installed, we can setup the nltk and transformers modules with microsofts conversation trained AI. We will be adding this code into a function.

def RUN_CORTANA():
RunLoop = True
print("RUNNING MAIN FUNCTION")
nlp = transformers.pipeline("conversational", model="microsoft/DialoGPT-medium")
os.environ["TOKENIZERS_PARALLELISM"] = "true"

A while loop will be used to keep the chat bot runnning until the user tells it to exit. In this command, a user input variable will send information to the chatbot.

while RunLoop:
text = input("enter text: ")
print("----- Closing down Cortana -----")

Now that we have the user text, we can set conditional statements or modify the text before the AI gets it. Once the string is entered, the chatbot will search through its databases and find the most accurate response to the string. In the code below, the if statement tells the computer that when the user is greeting the Chatbot. A Constant reponse must be outputted.

if any(i in text for i in
["exit", "close", "bye", "cya", "have a nice day", "see you", "talk to you later"]):
res = np.random.choice(["Have a good day", "Bye", "Goodbye", "Hope to meet soon", "peace out!"])
RunLoop = False
elif text == "ERROR":
res = ""
pass
else:
chat = nlp(transformers.Conversation(text), pad_token_id=50256)
res = str(chat)
res = res[res.find("bot >> ") + 6:].strip()
print(res)

Once this code has been written, we can call the function and run the program

if __name__ == "__main__":
print("----- Starting up -----")
RUN_CORTANA()

Get full code here