Skip to main content
In this section, you will build a complete, stateful Custom ChatGPT Clone web application. This extends the Streamlit chatbot by adding a secure user registration and login system, local SQLite persistence, and chat log retrieval.

Objectives

  1. Implement a secure user authentication system (Registration, Login, and Logout) using SQLite and password hashing.
  2. Link the active authenticated user’s session to SQL-backed session storage (SQLChatMessageHistory).
  3. Render user-specific history dynamically on login.
  4. Provide a dashboard with chat capabilities, logout options, and sidebar controls (like clearing chat logs).

Plan

  1. Database & Hashing Setup: Establish an SQLite database (chat_history.db) with a users table and implement SHA-256 password hashing.
  2. User Authentication Flow:
    • Use st.session_state to track authentication status and the logged-in username.
    • Build a landing portal where users can toggle between “Login” and “Register” forms.
  3. Dashboard & Session Initialization: Once logged in, initialize SQLChatMessageHistory using the username as the session identifier.
  4. History Rendering & Chat Loop: Retrieve and display past session messages, accept user chat input, save to SQLite, get the model’s response, and save it back.
  5. Session Control & Logs: Implement a “Log Out” button to clear session variables, and a “Clear Chat History” button to wipe chat logs.

Step-by-Step Implementation

Let’s build the Custom ChatGPT Clone incrementally:

Step 1: File and Folder Setup

Plan:
  1. Create the target directory langchain/1_chat_models if it does not already exist.
  2. Create a new Python file named 7_chat_model_custom_chatgpt.py inside this folder.
Command or Action: Create and navigate to the directory in your workspace:

Step 2: Imports and Page Config

Plan:
  1. Import streamlit, sqlite3, hashlib, SQLChatMessageHistory, and message schemas.
  2. Configure the page settings and load environment variables.
Code Implementation:

Step 3: Database Setup & Password Hashing

Plan:
  1. Create a helper init_db() to create the users table if it doesn’t exist.
  2. Create hash_password(password) using hashlib.sha256 to avoid storing plain-text passwords.
Code Implementation:

Step 4: Registration and Login Functions

Plan:
  1. Implement register_user(username, password) to insert credentials into the users table (handling potential duplicate username errors).
  2. Implement login_user(username, password) to fetch the password for a username and verify it matches the hashed password input.
Code Implementation:

Step 5: Streamlit Session State & Authentication UI

Plan:
  1. Set up session state variables authenticated and username.
  2. If the user is not authenticated, show a login/registration portal using st.radio.
Code Implementation:

Step 6: Authenticated Chat Dashboard & Logout

Plan:
  1. If authenticated, render the dashboard.
  2. Add a Log Out button in the sidebar to reset session variables.
  3. Initialize the SQL-backed chat history using the active username and load the chat interface.
Code Implementation:

Combined Code

Combining all the steps above gives the final complete script:

Exercise: Dynamic Model Swapper 🔀

Goal

Extend the sidebar options to include a model selector selectbox (st.sidebar.selectbox) that allows the logged-in user to swap between Llama (llama-3.3-70b-versatile via Groq) and Gemini (gemini-2.5-flash via Google GenAI) models dynamically without resetting the conversation history or logging out.

Plan

  1. Inside the authenticated screen block (else:), add a selectbox in the sidebar containing model choices: "Llama 3.3 (Groq)" and "Gemini 2.5 (Google)".
  2. Based on selection, determine the correct model_name and model_provider.
  3. Pass these parameters to the init_chat_model instantiation dynamically.

Practice & Exercises

To practice, open the interactive notebook:

Practice & Exercises

Practice setting up and running your custom ChatGPT Streamlit application.💻 VS Code | 🚀 Colab | 📥 Download
To execute the Custom ChatGPT app locally, run the script from your terminal: