Objectives
- Implement a secure user authentication system (Registration, Login, and Logout) using SQLite and password hashing.
- Link the active authenticated user’s session to SQL-backed session storage (
SQLChatMessageHistory). - Render user-specific history dynamically on login.
- Provide a dashboard with chat capabilities, logout options, and sidebar controls (like clearing chat logs).
Plan
- Database & Hashing Setup: Establish an SQLite database (
chat_history.db) with auserstable and implement SHA-256 password hashing. - User Authentication Flow:
- Use
st.session_stateto track authentication status and the logged-in username. - Build a landing portal where users can toggle between “Login” and “Register” forms.
- Use
- Dashboard & Session Initialization: Once logged in, initialize
SQLChatMessageHistoryusing the username as the session identifier. - 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.
- 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:- Create the target directory
langchain/1_chat_modelsif it does not already exist. - Create a new Python file named
7_chat_model_custom_chatgpt.pyinside this folder.
Step 2: Imports and Page Config
Plan:- Import
streamlit,sqlite3,hashlib,SQLChatMessageHistory, and message schemas. - Configure the page settings and load environment variables.
Step 3: Database Setup & Password Hashing
Plan:- Create a helper
init_db()to create theuserstable if it doesn’t exist. - Create
hash_password(password)usinghashlib.sha256to avoid storing plain-text passwords.
Step 4: Registration and Login Functions
Plan:- Implement
register_user(username, password)to insert credentials into theuserstable (handling potential duplicate username errors). - Implement
login_user(username, password)to fetch the password for a username and verify it matches the hashed password input.
Step 5: Streamlit Session State & Authentication UI
Plan:- Set up session state variables
authenticatedandusername. - If the user is not authenticated, show a login/registration portal using
st.radio.
Step 6: Authenticated Chat Dashboard & Logout
Plan:- If authenticated, render the dashboard.
- Add a Log Out button in the sidebar to reset session variables.
- Initialize the SQL-backed chat history using the active username and load the chat interface.
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
- Inside the authenticated screen block (
else:), add a selectbox in the sidebar containing model choices:"Llama 3.3 (Groq)"and"Gemini 2.5 (Google)". - Based on selection, determine the correct
model_nameandmodel_provider. - Pass these parameters to the
init_chat_modelinstantiation dynamically.
Solution
Solution
Practice & Exercises
To practice, open the interactive notebook:Practice & Exercises
Practice setting up and running your custom ChatGPT Streamlit application.💻 VS Code | 🚀 Colab | 📥 Download