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 user session authentication, local SQLite persistence, and chat log retrieval.

Objectives

  1. Implement a user login system using Streamlit inputs.
  2. Link the active username to SQL-backed session storage (SQLChatMessageHistory).
  3. Render user-specific history dynamically on login.
  4. Add sidebar administrative controls (like clearing chat logs).

Plan

  1. User Authentication: Setup a sidebar input to accept a username. If no user is logged in, show an info prompt to block the interface.
  2. SQLite Connection: Once a username is entered, initialize SQLChatMessageHistory using the username as the session identifier.
  3. History Rendering: Retrieve and loop through past session messages from the database, displaying them in user/assistant bubbles.
  4. Chat Logic: Integrate chat inputs so new messages are immediately written to the local database, passed to the model (initialized via core abstractions), and the resulting AI response is saved back to SQLite.
  5. Clear Controls: Implement a sidebar button that executes chat_history.clear() to erase logs and resets the interface.

Step-by-Step Implementation

Let’s build the Custom ChatGPT Clone incrementally step-by-step:

Step 1: Imports and Page Config

Plan:
  1. Import streamlit, SQLChatMessageHistory, and message schemas.
  2. Load environment variables.
  3. Configure the browser page title and icon using st.set_page_config.
Code Implementation:

Step 2: Sidebar Authentication

Plan:
  1. Create a text input box in the sidebar for the username.
  2. Use an if username: check to block execution of the chatbot until a valid session ID is provided.
Code Implementation:

Step 3: Initialize SQLite Persistent History & Model

Plan:
  1. Setup connection variables and instantiate SQLChatMessageHistory inside the authentication block.
  2. Add a default SystemMessage if the session history is brand new.
  3. Initialize the chat model using init_chat_model.
Code Implementation:

Step 4: Render Saved Logs, Input Loops, and Clear Controls

Plan:
  1. Loop through chat_history.messages to render user and assistant message bubbles.
  2. Collect chat inputs, save them to the database, invoke the model, render the AI’s reply, and save it.
  3. Add a sidebar clear button that invokes .clear() on the database history.
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.

Plan

  1. Add a selectbox in the sidebar containing model strings: "Llama 3.3 (Groq)" and "Gemini 2.5 (Google)".
  2. Based on selection, set the corresponding model_name and model_provider parameters.
  3. Pass these parameters to the init_chat_model instantiation inside the app execution.

Practice & Exercises

To execute the Custom ChatGPT app locally, run the script from your terminal: