Build a ChatGPT-like Clone in Python with UI- Using Streamlit
- by Author
Have you ever wondered about building your own ChatGPT-like ChatBot in Python with a fantastic User-Interface? In this article, we will cover all the steps involved in “How to build a ChatGPT-like Clone in Python?”.
To create a Python chatbot with Streamlit and OpenAI, we need the OpenAI API key. Follow the steps explained for getting an API Key or API secret key in the article “How to use LangChain with ChatGPT or GPT-3.5?“
We will start with the introduction of Streamlit, OpenAI, and ChatGPT. Then we will deep dive one by one and see how to integrate all in Python for building a ChatGPT alternative.

What is Streamlit?
Streamlit is an open-source and free framework to rapidly build and deploy web applications. It helps to easily build applications for machine learning-based models and data science web apps. Streamlit is a Python-based library or package which is specifically designed for machine learning engineers. Using Streamlit, ML Engineers don’t have to spend weeks deploying an application for POCs. They can do it just by importing functionalities using Streamlit Python library. Check out the complete Streamlit documentation for more information.
Command to Install Streamlit in Python
pip install streamlit
Introduction to OpenAI- An American Artificial Intelligence (AI) Research Laboratory
According to the official website of OpenAI, it’s an AI research and development organization. The mission and goal of the organization is to use Artificial General Intelligence for the benefit of all of Humanity.
Products of OpenAI include GPT-3, GPT-3.5, GPT-4 (the most advanced system), and DALL-E 3.
ChatGPT- An AI-Powered Language Model Developed by OpenAI
ChatGPT is a Large Language Model that can generate responses in a Natural Language using the prompts provided by the User or Human.
It performs various tasks which include content creation, technical writing, problem-solving, composing songs, writing screenplays, and much more. To check its capability, visit- ChatGPT Web Interface.
Get an API Key from OpenAI: Follow these Steps to Build a ChatGPT-like Clone in Python
An API key, which stands for Application Programming Interface key, is a code or token used by computer programs or applications to authenticate and gain access to an API (Application Programming Interface). For Creating a ChatGPT-like model, we will use OpenAI API. To access the API, we require an API key. Follow the steps to get one for you.
Step 1. Goto this link https://platform.openai.com/account/api-keys
Step 2. Click on Signup (for first-time users) or Login (if already registered)
Step 3. Fill in the required details (Name, Date of Birth, Mobile Number, etc.)
Step 4. In the API Keys section, click on + Create new secret key button
Step 5. Provide any name (Optional) and click on Create secret key
Step 6. Copy the secret key and save it in a Notepad or any Sticky Notes for future use. As you will not be able to view it again.
Curious about learning vector databases? Check out my article on Vector Database.
Creating a ChatGPT-like Model- Conversational AI with Streamlit and Python
To create a Streamlit-based chat application, follow the code below.
Copy the code in VS Code or any Python editor and save it using the name “main.py”.
import openai
import streamlit as st
st.title("ChatGPT-like clone - By Pykit")
user_api_key = st.sidebar.text_input(
label="#### Your OpenAI API key 👇",
placeholder="Paste your openAI API key, sk-",
type="password")
# Your API Key- Do not paste it here, you will have to provide it using UI.
openai.api_key = user_api_key
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
if "messages" not in st.session_state:
st.session_state.messages = []
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("What is up?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
for response in openai.ChatCompletion.create(
model=st.session_state["openai_model"],
messages=[
{"role": m["role"], "content": m["content"]}
for m in st.session_state.messages
],
stream=True,
):
full_response += response.choices[0].delta.get("content", "")
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
In the terminal, execute this code to open the Streamlit application in the web browser.
streamlit run .\main.py

Conclusion
In this article, we have explored how to Build a ChatGPT-like Clone in Python using Streamlit and OpenAI. If you are looking to build a Chatbot in Python using NLTK or NLP, read my article on “How To Make AI Chatbot In Python Using NLP (NLTK) In 2023?“.
Contact Us
You can reach out to us at pykit.org@gmail.com.
Have you ever wondered about building your own ChatGPT-like ChatBot in Python with a fantastic User-Interface? In this article, we will cover all the steps involved in ‘How to build a ChatGPT-like Clone in Python?’
We will start with the introduction of Streamlit, OpenAI, and ChatGPT. Then we will deep dive one by one and see how to integrate all in Python for building a ChatGPT alternative.
Comments