A simple chatbot that uses markov chains to generate responses. It can be trained on a WhatsApp chat log. Note that your logs are not sent anywhere, everything happens locally.
- Clone the repository, or just download the
bot.py
file. - Get a WhatsApp chat log, how?
- Run
python3 bot.py -train <filename>.txt -b1
- Chat with your bot!
I've trained a bot on my own chat logs. You can chat with the bot here. Use with caution.
For web interface, see here.
import bot
my_bot = bot.Bot()
# Train the bot on a WhatsApp chat log
my_bot.create_model("chatlog.txt")
my_bot.train()
# Save the model to a file
my_bot.save_model("example.model")
# Generate 10 lines of sample chat
my_bot.sample_chat(10)
import bot
my_bot = bot.Bot()
# Load the model from a file
my_bot.load_model("example.model")
# Get a response to a message
my_bot.respond("hi") # output maybe "hello"
-
You can change number of previous words a word depends on, by changing the
states
parameter in thetrain
function. The default value is 3.bot.train(states=2) # train the bot with 2 previous words
-
Use custom text transformations to transform the chat log before training. The default transformation function is
query_transform
. You can modify the transformation function to your custom function as follows:def my_transform(raw: str) -> str: # do something with raw return raw bot.query_transform = my_transform # used to transform the query bot.reply_transform = my_transform # used to transform the reply bot.line_transform = my_transform # used in training to transform each line of the chat log
For example, this will make your bot SHOUT at you:
def shout(raw: str) -> str: return raw.upper() bot.reply_transform = shout
-
Use debug mode to print debug messages. Debug mode is disabled by default. To enable debug mode, set
debug
toTrue
in theBot
class, ormy_bot = bot.Bot(debug=True)
-
You can block certain words from being used in the chat log. To do this, set
blacklist
to a list of words in theBot
class, ormy_bot = bot.Bot(blacklist=["69"])
-
my_bot.stats()
prints some statistics about the model.
The bot.py
command line can be used to train the bot, generate sample chat, and interact with the bot. It can also be used to save and load the model.
Train the bot on chatlog.txt
and save the model to example.model
:
$ python3 bot.py -train chatlog.txt -save example.model
Generate 10 lines of sample chat using the saved example.model
:
$ python3 bot.py -load example.model -m 10
Interact with the bot using the saved example.model
:
$ python3 bot.py -load example.model -b1
usage: python3 bot.py [-h]
[-load LOAD]
[-train TRAIN]
[-save SAVE]
[-n N] [-m M]
[-b1] [-b2]
[-debug]
-load LOAD load model from file
-train TRAIN train on file/folder
-save SAVE save model to file
-n N N states in markov chain (3 is recommended)
-b1 chat with Bot1
-b2 chat with Bot2
-m M generate a sample chat log with M messages
-debug print debug messages
- The bot by default trains on a single chat log file. If you want to train it on multiple chat logs, you can pass the folder path to
create_model
function to train on all the files in the folder. - The bot doesn't have a database of responses. It uses the chat logs to generate responses. So, the more chat logs you feed it, the better it will be.
- Open WhatsApp on your phone.
- Go to the chat you want to export.
- Tap the menu button (3 dots on the top right corner).
- Tap
More
. - Tap
Export chat
. - Tap
Without Media
. - The chat log will be saved to your phone's storage.
For more information, see this.
- context based responses (remember the last few messages)
- training on multiple chat logs
- efficient storage of model
- web interface
- media support (images, stickers, etc.)
- easy way to train bots online (website?)
- you tell me
Soham Korade