Build Your Own Chatbot With ChatGPT 2023 | A Step By Step Tutorial

1. Install Python:-

i) First, you need to install Python on your computer. Open this link and download the setup file for your platform.
ii) Next, run the setup file and make sure to enable the checkbox for “Add Python.exe to PATH.” This is an extremely important step. After that, click on “Install Now” and follow the usual steps to install Python.
iii) To check if Python is properly installed, open Cmd on your computer. Run the python --version command and it will output the Python version. On Linux or other platforms, you may have to use python3 --version instead of python --version.
iv) Now, run python -m pip install -U pip command to update Pip. Again, you may have to use python3 and pip3 on Linux or other platforms.
v) Now, it’s time to install the OpenAI library, which will allow us to interact with ChatGPT through their API. In the Terminal, run pip install openai command to install the OpenAI library using Pip. If the command does not work, try running it with pip3.
vi) In the Terminal, run pip install gradio command to install the Gradio. Gradio allows you to quickly develop a friendly web interface so that you can demo your AI chatbot. It also lets you easily share the chatbot on the internet through a shareable link.
vii) Now go to OpenAI and create a free account. If you already have an OpenAI account, simply log in.
viii) Next, click on your profile in the top-right corner and select “View API keys” from the drop-down menu.

ix) Here, click on “Create new secret key ” and copy the API key.

Also, do not share or display the API key in public. It’s a private key meant only for access to your account. You can also delete API keys and create multiple private keys (up to five).
x) Now open Notepad++ (or your choice of code editor) and paste the below code:

import gradio as gr

openai.api_key = "Your API key"

messages = [
    {"role": "system", "content": "You are a helpful and kind AI Assistant."},

def chatbot(input):
    if input:
        messages.append({"role": "user", "content": input})
        chat = openai.ChatCompletion.create(
            model="gpt-3.5-turbo", messages=messages
        reply = chat.choices[0].message.content
        messages.append({"role": "assistant", "content": reply})
        return reply

inputs = gr.inputs.Textbox(lines=7, label="Chat with AI")
outputs = gr.outputs.Textbox(label="Reply")

gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Chatbot",
             description="Ask anything you want",

Note: Make sure to replace the Your API key text with your own API key generated above. That’s the only change you have to make.
xi) After that, save and set the file name as and change “Save as type” to “All types” from the drop-down menu. Then, save the file to an easily-accessible location like the Desktop. You can change the name to your preference, but make sure .py is appended.

xii) Now, move to the location where you saved the file ( Shift + Right-click on it and choose “Copy as path“.
xiii) Open the Terminal and enter python, add a space, paste the copied path and hit Enter. Also, on Linux systems, you may have to use python3.
Example: python "C:\Users\Navneet\Desktop\"
Keep in mind, the file path will be different for your computer.
xiv) You may get a few warnings, but ignore them. At the bottom, you will get a local and public URL. Now, copy the local URL and paste it into the web browser.

xv) And that is how you build your own AI chatbot with the ChatGPT API. Your ChatGPT-powered AI chatbot is live. Now, you can ask any question you want and get answers in a jiffy. In addition to ChatGPT alternatives, you can use your own chatbot instead of the official website.

xvi) You can also copy the public URL and share it with your friends and family. The link will be live for 72 hours, but you also need to keep your computer turned on since the server instance is running on your computer.

xvii) To stop the server, move to the Terminal and press “Ctrl + C“. If it does not work, press “Ctrl + C” again.

xviii) To restart the AI chatbot server, simply copy the path of the file again and run the below command again (similar to step #6). Keep in mind, the local URL will be the same, but the public URL will change after every server restart.

python "C:\Users\mearj\Desktop\"

So this is how you can build your own AI chatbot with ChatGPT 3.5

Happy learning! :zap: :wink:


Thanks for this tutorial bro, am wondering how can i integrate the chatbot with live website or build chatbot website using chatgpt , how to integrate with host?

1 Like

great., thankz for ths.,

1 Like