A modern, intelligent chat application built with Streamlit, LangChain, and MongoDB. This app uses Google's Gemini 2.5 Flash model to provide AI-powered conversations while maintaining persistent chat history across sessions.
π Live Demo App: https://langchain-app-chat-with-history.streamlit.app/
- AI-Powered Conversations: Uses Google Gemini 2.5 Flash for intelligent responses
- Persistent Message History: All chat messages are saved in MongoDB for future reference
- Multi-User Support: Different users can maintain separate chat sessions
- Beautiful UI: Modern, responsive Streamlit interface with chat bubbles and status indicators
- Session Management: Each user has their own isolated conversation history
- Real-time Responses: Instant AI responses with loading indicators
.
βββ app.py # Main Streamlit application
βββ requirements.txt # Python dependencies
βββ .env # Environment variables (gitignored)
βββ .gitignore # Git ignore files & folders
βββ README.md # Project documentation
βββ venv/ # Virtual environment (gitignored)
- Python 3.8+
- MongoDB Atlas account
- Google API key for Gemini
# Clone the repository
git clone <repository-url>
cd <project-directory>
# Create virtual environment
python -m venv venv
source venv/Scripts/activate # Windows
# or
source venv/bin/activate # Mac/Linuxpip install -r requirements.txtCreate a .env file in the project root:
GOOGLE_API_KEY=your-google-api-key-hereGet your Google API key from: Google AI Studio
streamlit run app.pyThe app will open in your browser at http://localhost:8501
- Go to MongoDB Atlas
- Create a free cluster
- Get your connection string
- The app uses:
- Database:
langchain_practice - Collection:
chat_history_collection
- Database:
MongoDB Atlas is a fully-managed, cloud-hosted database service. For this project you can use the free tier to create a cluster, create a database user with a strong password, and configure Network Access (IP whitelist) so your machine or server can connect. After creating a cluster, copy the connection string and store it in your .env as MONGODB_CONNECTION_STRING (do not commit credentials).
Update the MongoDB connection string in app.py or move it to .env:
connection_string = os.getenv("MONGODB_CONNECTION_STRING")- User Login: User enters their username in the sidebar
- Message Input: User types their question in the input field
- AI Processing: Gemini 2.5 Flash model processes the request
- History Storage: Both user message and AI response are saved to MongoDB
- Display: Previous chat history is shown on the page
- Persistence: All messages remain accessible across sessions
- streamlit - Web app framework
- langchain-google-genai - LangChain integration with Google Gemini
- langchain-mongodb - MongoDB message history storage
- pymongo - MongoDB Python driver
- python-dotenv - Environment variable management
See requirements.txt for complete list.
Connect to MongoDB and manage your data:
# Connect to Redis (if applicable)
redis-cli -p 6379
# List all keys
KEYS *
# Get string values
GET key_name
# Get list values
LRANGE key_name 0 -1
# Delete all keys in current database
FLUSHDB
# Delete all keys in all databases
FLUSHALL- app.py: Main application file (changed from main.py for better convention with Streamlit)
- requirements.txt: Python package dependencies
- .env: Sensitive environment variables (never commit)
- .gitignore: Files to exclude from git
app.py is the Streamlit entrypoint for this project. It:
- Initializes the LLM client (
ChatGoogleGenerativeAI) using theGOOGLE_API_KEYfrom the environment - Creates a
MongoDBChatMessageHistoryinstance to read/write messages to the configured database and collection - Renders the UI and handles user input, saving both user and AI messages to the history
To change the database or model, update the corresponding variables in app.py or move them into the .env and read with python-dotenv.
- Never commit
.envfile to git - Keep your API keys and MongoDB connection strings private
- Use environment variables for all sensitive data
- Don't hardcode credentials in your code
Update these lines in app.py if needed:
chat_with_history=MongoDBChatMessageHistory(
session_id=user,
connection_string="your-mongodb-uri",
database_name="langchain_practice",
collection_name="chat_history_collection"
)To use a different model, update the LLM initialization:
llm = ChatGoogleGenerativeAI(model="gemini-pro", api_key=os.getenv("GOOGLE_API_KEY"))- Ensure
.envfile is in the project root - Check that
GOOGLE_API_KEYis set correctly - Restart the Streamlit app after updating
.env
- Verify MongoDB Atlas cluster is active
- Check connection string is correct
- Ensure IP whitelist includes your machine (or use 0.0.0.0/0)
- Google Gemini API has usage limits
- Check your quota at Google AI Studio
This project is open source and available under the MIT License.
Created for learning and demonstrating LangChain + MongoDB integration with Streamlit.