How to build your own ChatGPT-style chat bot

In recent months, new inventions like ChatGPT have been said to be possibly the end of society, or the saving of society… but maybe this is something that is something very similar to what has been hyped before?

It is interesting that in Marc Andreesen’s recent post he claims numerous upcoming advantages based on AI, including “Every person will have an AI assistant/coach/mentor/trainer/advisor/therapist that is infinitely patient, infinitely compassionate, infinitely knowledgeable, and infinitely helpful. The AI assistant will be present through all of life’s opportunities and challenges, maximizing every person’s outcomes.”

There already was an AI chat psychotherapist back in the 1960s called Eliza, and that did not get anywhere in actual therapy use as far as I have heard. To say that AI will replace teachers is probably also going to be a no… And if it could be, would we?

The misinformation and double talk of ChatGPT has been noted elsewhere, and others such as Kuki and others are oddly fake-human, along with others noted here.

What about building your own? There are some Python projects that let you train a chatbot with your own questions and answers…

Installation of Chatterbot

First install Spacy, the language library, and Chatterbot along with its data:

pip3 install spacy
python3 -m spacy download en_core_web_sm
pip3 install chatterbot2
wget https://github.com/gunthercox/chatterbot-corpus/archive/refs/heads/master.zip && unzip master.zip && rm master.zip
cd chatterbot-corpus-master/

Note the use of “pip3” and “python3” on newer Ubuntu, and chatterbot2 is actually the latest working Chatterbot, despite the references in documentation. After setting up the environment folder as shown above, you can try a conversation with the chatbot as in their example:

~/chatterbot-corpus-master$ python3
Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from chatterbot import ChatBot
>>> from chatterbot.trainers import ChatterBotCorpusTrainer
>>> 
>>> chatbot = ChatBot('Ron Obvious')
>>> 
>>> # Create a new trainer for the chatbot
>>> trainer = ChatterBotCorpusTrainer(chatbot)
>>> 
>>> # Train the chatbot based on the english corpus
>>> trainer.train("chatterbot.corpus.english")
Training ai.yml: [####################] 100%
Training botprofile.yml: [####################] 100%
Training computers.yml: [####################] 100%
Training conversations.yml: [####################] 100%
Training emotion.yml: [####################] 100%
Training food.yml: [####################] 100%
Training gossip.yml: [####################] 100%
Training greetings.yml: [####################] 100%
Training health.yml: [####################] 100%
Training history.yml: [####################] 100%
Training humor.yml: [####################] 100%
Training literature.yml: [####################] 100%
Training money.yml: [####################] 100%
Training movies.yml: [####################] 100%
Training politics.yml: [####################] 100%
Training psychology.yml: [####################] 100%
Training science.yml: [####################] 100%
Training sports.yml: [####################] 100%
Training trivia.yml: [####################] 100%
>>> 
>>> # Get a response to an input statement
>>> chatbot.get_response("Hello, how are you today?")
<Statement text:What is cricket>
>>> print(chatbot.get_response("A cricket is a bug correct?"));
A secret society that has supposedly existed for centuries.
>>> print(chatbot.get_response("What is a secret society?"));
Try not to be too scared.
>>> print(chatbot.get_response("Scared of what?"));
Blue
>>> print(chatbot.get_response("Scared of what?"));
peter parker.
>>> print(chatbot.get_response("Scared of what?"));
Have I erred?
>>> print(chatbot.get_response("Who is the president of the USA?"));
Artificial Intelligence is the branch of engineering and science devoted to constructing machines that think.
>>> print(chatbot.get_response("Who is the president?"));
it changes every few years.
>>> a=chatbot.get_response("Who are you");
>>> a
<Statement text:I am just an artificial intelligence.>
>>> a.confidence
0.96

You can see that the responses here are not so useful and are based on natural language recognition and similarities to the input .yml files that you can find in that directory. The statement.confidence of a response will tell you if it actually has confidence in the response based on the train input, or as it may be, “pulled out of the hat”. This bot is also pretty useless with any math problems. You can see some examples of built bots in their examples folder and try them out. You can combine several different chatbot modes to make something that responds to math problems (“1 + 1”), unit questions (“how many inches in a yard”), and conversational questions it has been trained on:

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

unitbot = ChatBot(
    'Unit Converter',
    logic_adapters=[
        'chatterbot.logic.UnitConversion',
    ]
)
mathbot = ChatBot(
    'Math & Time Bot',
    logic_adapters=[
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter'
    ]
)
YMLreadingbot = ChatBot('Ron Obvious')
# Create a new trainer for the chatbot
trainer = ChatterBotCorpusTrainer(YMLreadingbot)
# Train the chatbot based on the english corpus
trainer.train("chatterbot.corpus.english")#chatterbot_corpus/data/english

print("Ready, enter your query:")
inputstr = ''
while inputstr != 'exit':
    inputstr = input()
    try:
        value = unitbot.get_response(inputstr)
        if value:
            print( "%s." % (value.text,))
            continue
    except AttributeError:
        pass
    
    trainresponse = YMLreadingbot.get_response(inputstr)
    if trainresponse.confidence > 0:
        print(trainresponse.text)
    else:
        try:
            value = mathbot.get_response(inputstr)
            if value and value.confidence >0:
                print( "%s." % (value.text,))
                continue
            else:
                print("I don't know can you rephrase??")
        except AttributeError:
            pass

More features

Another feature that is great about Chatterbot is you can start it and influence it rather than it shaping what you read and understand – as Noah was saying, it is really not something you want to give people to rely on or learn from– it is something that pulled in all sorts of internet garbage good and could be bad advice if you are using ChatGPT! For this chatbot you can use your own inputs. So let’s say you want to teach your bot some bible knowledge: Just add a new file and set some questions and answers in the similar yml format. For example:

categories:
- bible
conversations:
- - What is faith?
  - Now faith is the assurance of things hoped for, the conviction of things not seen. (Hebrews 11:1)
- - Who was Abel?
  - By faith Abel offered to God a more acceptable sacrifice than Cain, through which he was commended as righteous, God commending him by accepting his gifts. And through his faith, though he died, he still speaks. (Hebrews 11:4)
- - How important is faith?
  - 'Then what becomes of our boasting? It is excluded. By what kind of law? By a law of works? No, but by the law of faith. (Romans 3:27)'
- - What was the Jubilee?
  - "In this year of jubilee each of you shall return to his property. 14 And if you make a sale to your neighbor or buy from your neighbor, you shall not wrong one another. 15 You shall pay your neighbor according to the number of years after the jubilee, and he shall sell to you according to the number of years for crops. 16 If the years are many, you shall increase the price, and if the years are few, you shall reduce the price, for it is the number of the crops that he is selling to you. 17 You shall not wrong one another, but you shall fear your God, for I am the Lord your God. (Leviticus 25)"
- - Who is the son of God?
  - "And we know that the Son of God has come and has given us understanding, so that we may know him who is true; and we are in him who is true, in his Son Jesus Christ. He is the true God and eternal life. (1 John 5:20)" 
- - Who is God?
  - "Hear, O Israel: The Lord our God, the Lord is one. (Deutoronomy 6:4)"
- - Who knows God?
  - "Anyone who does not love does not know God, because God is love. (1 John 4:8)"
- - Who is Jesus?
  - "And the Word became flesh, and dwelt among us, and we saw His glory, glory as of the only begotten from the Father, full of grace and truth. (John 1:14)"
- - What is the value of Truth?
  - "Buy the truth and do not sell it— wisdom, instruction and insight as well. (Proverbs 23:23)"
- - What is truth?
  - "An honest witness tells the truth, but a false witness tells lies. (Proverbs 12:17)"

Now the bot can give an answer to “What is faith for?” and other questions very similar to those that were programmed in to it. Now this isn’t going to win any Bible trivia but it is an interesting experiment.

More changes

Let’s say you don’t like some of the humor and answers that the bot example. You can edit its humor.yml before running the bot again – remove some, maybe add some of your own like so:

- - Does a mathematician plant a garden?
  - Yes with plants with square roots
- - Why is a moon rock tastier than an earth rock?
  - it's a little meteor (meatier).
- - Where do mathematicians live?
  - the city of Tangent
- - How do you keep warm in a room?
  - Go to a corner that is 90 degrees.

Now you can ask questions very similar to these to get the defined answers. For example:

Does mathematician have a garden?
Yes with plants with square roots
is a moon rock better than an earth rock?
it's a little meteor (meatier).

Hopefully this gives you some examples of what you can do with machine learning models! It is constructed completely different from ChatGPT large language learning models, so it can be run on a computer without a massive processor and processing card 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

thirty two ÷ = 16