A modern, responsive chatbot built with Streamlit and LangChain, powered by Google's Gemini AI models. Features real-time streaming responses, multiple model selection, and a clean, professional interface.
- 🤖 Multiple AI Models: Support for 40+ Google Gemini models (Gemini 1.5, 2.0, 2.5, and experimental)
- 💬 Streaming Responses: Real-time token streaming for smooth interaction
- 🔐 API Key Management: Secure per-session key entry via sidebar
- 🎨 Clean UI: Modern chat interface with avatars + dark/light theme toggle
- 📊 Chat Stats: Track messages in current session
- ⚡ Optimized: Smart caching with st.cache_resource and st.cache_data
- 📱 Mobile Friendly: Works well on different devices
- Python 3.8 or higher
- Google Gemini API key (Get one here)
- Clone the repository
git clone https://github.com/yourusername/streamlit-langchain-chatbot.git
cd streamlit-langchain-chatbot
- Create virtual environment & install dependencies
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
- Run the application
streamlit run app.py
- Open in browser
- The app will automatically open in your default browser
- Or navigate to
http://localhost:8501
Create a requirements.txt
file with these dependencies:
streamlit>=1.28.0
langchain-core>=0.1.0
langchain-google-genai>=1.0.0
python-dotenv>=1.0.0
You can provide your Google Gemini API key in two ways:
-
Through the UI (Recommended for security):
- Enter your API key in the sidebar when you run the app
- The key is stored only in your browser session
-
Environment Variable:
- Create a
.env
file in the project root - Add:
GOOGLE_API_KEY=your_api_key_here
- Create a
Choose from 40+ available models including:
- Gemini 2.0 Flash - Latest and fastest
- Gemini 2.5 Pro - Most capable for complex tasks
- Gemini 1.5 Flash - Good balance of speed and quality
- Experimental models - Cutting-edge features
- Start the app and enter your Google Gemini API key
- Select a model from the dropdown in the sidebar
- Start chatting - type your message and press Enter
- Watch responses stream in real-time
- Use chat controls:
- Clear chat history
- Switch between models mid-conversation
streamlit-langchain-chatbot/
│
├── app.py # Main application file
├── requirements.txt # Python dependencies
├── pyproject.toml # Project metadata (uv/PEP 621)
├── uv.lock # uv lockfile (auto-generated)
├── .gitignore # Git ignore file
├── .python-version # Python version pin (optional)
├── LICENSE # MIT License
├── README.md # Documentation
├── .env # Local env vars (optional, not committed)
└── .venv/ # Local virtual env (ignored)
- Push your code to GitHub
- Go to share.streamlit.io
- Connect your GitHub repository
- Set your
GOOGLE_API_KEY
in the secrets management - Deploy!
- Heroku: Use the Heroku CLI with a
Procfile
- Railway: Connect your GitHub repo for automatic deployment
- Replit: Import from GitHub and run directly
- Local Network: Use
streamlit run app.py --server.address 0.0.0.0
- API keys are handled securely per session
- No conversation data is stored permanently
- Environment variables are used for sensitive configuration
- Input validation prevents malformed requests
- Clone and setup:
git clone <your-repo>
cd streamlit-langchain-chatbot
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
pip install -r requirements.txt
- Run in development mode:
streamlit run app.py --server.runOnSave true
The code is modular and easy to extend:
configure_page()
- UI and page setuphandle_sidebar()
- Sidebar controls and settingsdisplay_chat_messages()
- Message renderinghandle_user_input()
- Input processing and AI interaction
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Streamlit for the amazing web app framework
- LangChain for AI integration tools
- Google AI for the Gemini API
- The open-source community for inspiration and support
- 🐛 Bug Reports: Open an issue
- 💡 Feature Requests: Start a discussion
- ❓ Questions: Check existing issues or create a new one
Made with ❤️ by Zohaib Khan