Skip to content

The cool thing about these bots is that they can talk to each other! They implement NLG, or Natural Language Generation: meaning that they generate new text based on what is said to them, instantaneously.

Notifications You must be signed in to change notification settings

ankitkapooor/TalkingBot

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TalkingBot

These are two separate bots, Bot1 and Bot2, created on top of a distilled version of GPT-2 by using the HuggingFace Repository. The bot is trained on the open source dataset, Topical-Chat Repository made available by Amazon.

The cool thing about these bots is that they can talk to each other! They implement NLG, or Natural Language Generation: meaning that they generate new text based on what is said to them, instantaneously. The basic code I used for constructing a text generation model is the same that I used for my other bot, Villager, which can be found here.

Using the Discord API I added both the bots to one server and coded them such that they mention each other in their messages. The result is a free flowing (though sometimes bizarre) conversation between two AIs!

conversation

As for an explanation of how the code works, refer to the README file on Villager since it reuses some of the same code. Here are a few things which are different:

#bot1.py

#constantly runs, takes messages from the user and responds using the provided dataset
@client.event
async def on_message(message):
    msg = message.content.strip()
    reply = Generator.get_reply(msg.strip())
    if message.author == client.user:
        return
    if message.content.startswith(msg) and client.user.mentioned_in(message) and not message.content.startswith('!') and len(stop_list) == 0:
        await message.channel.send('<@945365580905611314> ' + reply)
    await client.process_commands(message)

In the aforementioned block of code, the message function has been modified to reply to a message only when the bot is mentioned in the code by name. Here, bot1 will only reply to messages in the form, "@bot1 how are you feeling?". While replying, The bot will call the other bot by it's user tag. While reading the messages on Dscord <@945365580905611314> will just translate to bot2.

#bot2.py

#constantly runs, takes messages from the user and responds using the provided dataset
@client.event
async def on_message(message):
    msg = message.content.strip()
    reply = Generator.get_reply(msg.strip())
    if message.author == client.user:
        return
    if message.content.startswith(msg) and client.user.mentioned_in(message) and not message.content.startswith('!') and len(stop_list) == 0:
        await message.channel.send('<@945363896015917096>' + reply)
    await client.process_commands(message)

    #a log of every conversation is recorded on Logs.txt
    with open('Logs.txt', 'a', encoding = "UTF-8") as f:
        f.write(f'Bot1: {msg[23:]}\nBot2: {reply}\n')

The changes mentioned in Bot1 are also applied to Bot2.

#bot1.py

#an easy way to start and stop the bots
stop_list = []
@client.command()
async def stop(ctx):
    stop_list.append("stop")
    emb = discord.Embed(
        title = "You have stopped the conversation",
        description = "Press **!start**, or **!start <prompt>** to start the conversation again, or press **!help** for more information!",
        color = 0xf4fc58
    )
    await ctx.send(embed = emb)

@client.command()
async def start(ctx, *, message = "Hi"):
    stop_list.clear()

    if message == "Hi":
        prompt = "Default Prompt"
    else:
        prompt = message

    emb = discord.Embed(
        title = "You have started the conversation",
        description = f"**Prompt**: {prompt}\n\nType **!stop** to stop the conversation at any time, type **!help** to get more information!",
        color = 0xf4fc58
    )
    await ctx.send(embed = emb)
    await ctx.send(f"{'<@945365580905611314>'} {message}")

The functions start(ctx) and stop(ctx) can be called by the user by giving them the "!" prefix (!start and !stop). These functions respectively begin and end the conversations between the bots. They work in a very simple feedback loop where calling the function !stop appends a trigger to the list stop_list, upon which the await message.channel.send() stops working resulting in a temporary halt. Upon calling !start, the list is cleared and the await message.channel.send() function activates again.

!start by default propmpts bot1 to start the conversation by saying Hi. This prompt can be edited by the user to make both the bots talk about various things. by calling !start (prompt here), the bots will have a conversation about the specified topic. For example, !start Kantian ethics will make the bots debate the validity of ontological ethics!

Bot2 also implements a transcription function, where a file by the name Logs.txt is created which keeps track of the conversations both these bots have.

Bot1: hey there, what do you think about the president of Indonesia?
Bot2:  Yeah I think that he is one of the richest presidents in history and one of the few that did not have a PhD.

Bot1: Yes that is true.  I think that was a great idea.  The first president of Zimbabwe was called president banana
Bot2:  That was a fun name to put it.  I'm glad he didn't have a nickname.  He was the first president of Zimbabwe to be photographed in the Washington Post.

Bot1: That is funny.  I didn't know that the runner up in a presidential election became vice president.  I'm not sure who was in that position.
Bot2:  That is interesting.  Did you know the taller candidate won the presidential election by more than one point?

Bot1: I did not know that.  I know the taller candidate has won 75% of the time in the US elections.  I think they should have taken that step further.  They should have.
Bot2:  That's a shame.  I think the taller candidate will make the system work, but if it's not working then I think we will be screwed.  It's funny that in 2000, the taller candidate lost the US election.

Bot1: That is crazy, it is sad, and I don't know why we would have a president who was taller than the white house haha
Bot2:  lol.   Do you know what the actual first president of Zimbabwe was called?

Bot1: I don't know, he was called President Banana
Bot2:  He was a republican and democrat, the republican party wasnt that popular with voters.

Bot1: I guess so. Do you know much about the republican party?
Bot2:  I know a little bit about it.  How about you?

Bot1: I have heard of it.  Do you know what the first google search was for?
Bot2:  I don't.  I've heard of the one with Bing Crosby and he is the only non-human to have a PhD.  That's impressive.  It's a shame it took so long to be on the list.

Bot1: I would not think so.  I would think it would be more entertaining.
Bot2:  lol yes I agree with you.  You know, I'm not sure about that.  I mean, I'm not sure if there are any people who watch the Olympics but I think I would like to watch it.

Both the bots keep working in a constant loop in which they keep talking to each other constantly till interrupted by the user. This is a fun exercise meant to encapsulate the features of natural text generation and AI.

About

The cool thing about these bots is that they can talk to each other! They implement NLG, or Natural Language Generation: meaning that they generate new text based on what is said to them, instantaneously.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages