Building a Smart Email Assistant with Streamlit, Langchain, and Gmail API
Managing your email inbox can be overwhelming, especially when dealing with hundreds of unread messages, marketing emails, and newsletters. What if you had an AI-powered assistant that could help you summarize, clean, categorize, and even create filters for your Gmail inbox? In this blog post, I’ll walk you through a project that does exactly that — a Smart Email Assistant built using Streamlit, Langchain, and the Google Gmail API.
Project Overview
This Smart Email Assistant is a Streamlit-based web app that leverages Langchain AI agents to interact with your Gmail account. It uses advanced AI models from OpenAI or Google Gemini to understand your commands and perform tasks such as:
- Summarizing unread emails
- Cleaning up marketing emails older than 30 days
- Auto-categorizing emails into Promotions, Work, and Personal folders
- Creating Gmail filters automatically
- Finding and managing unsubscribe links in newsletters
The assistant provides an interactive chat interface where you can type your requests or use quick action buttons for common tasks.
Key Technologies
- Streamlit: For building the user-friendly web interface.
- Langchain: To create AI agents that understand and execute natural language commands.
- Google Gmail API: To securely access and manage your Gmail inbox.
- OpenAI / Google Gemini: AI models powering the assistant’s understanding and responses.
- OAuth2 Authentication: Ensures secure access to your Gmail account.
How It Works
The app initializes an AI agent that loads Gmail tools for reading, modifying, and sending emails. It uses OAuth2 to authenticate with Gmail securely. The Streamlit UI maintains chat history and provides quick action buttons for tasks like summarizing unread emails or cleaning marketing emails.
When you enter a command or click a quick action, the agent processes the request using Langchain’s structured chat agent and interacts with Gmail via the API to perform the task.
Setup and Running
- Clone the repository and navigate to the project directory.
- Create and activate a Python virtual environment.
- Install dependencies from
requirements.txt
. - Set up your Google API credentials by placing your OAuth client secrets file as
credentials.json
. - Optionally, create a
.env
file with your Google API key to use Google Gemini. - Run the app with:
streamlit run app.py
Key Code Snippets
Initializing the Agent (agent.py)
import os
from dotenv import load_dotenv
from langchain.callbacks.streamlit import StreamlitCallbackHandler
from langchain.agents import initialize_agent, AgentType
from langchain.memory import ConversationBufferMemory
from utils import get_all_tools
load_dotenv()
def get_agent(parent_container):
if os.getenv("GOOGLE_API_KEY"):
from langchain_google_genai import ChatGoogleGenerativeAI
llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
temperature=0,
google_api_key=os.getenv("GOOGLE_API_KEY"),
streaming=True,
callbacks=[StreamlitCallbackHandler(parent_container=parent_container)],
)
else:
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
temperature=0,
streaming=True,
callbacks=[StreamlitCallbackHandler(parent_container=parent_container)],
)
tools = get_all_tools()
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
agent = initialize_agent(
tools,
llm,
agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
memory=memory,
)
return agent
Streamlit App Main (app.py)
import streamlit as st
from agent import get_agent
st.set_page_config(page_title="Smart Email Assistant", layout="wide")
if "messages" not in st.session_state:
st.session_state.messages = []
if "agent_initialized" not in st.session_state:
st.session_state.agent_initialized = False
st.title("Smart Email Assistant")
thinking_container = st.container()
if not st.session_state.agent_initialized:
with st.spinner("Initializing agent..."):
st.session_state.agent = get_agent(thinking_container)
st.session_state.agent_initialized = True
def process_input(query):
if query:
st.session_state.messages.append(("user", query))
with thinking_container:
response = st.session_state.agent.run(query)
st.session_state.messages.append(("assistant", response))
user_input = st.chat_input("Type your message here...", key="user_input")
if user_input:
process_input(user_input)
st.rerun()
Future Enhancements
Some exciting future scopes for this project include:
- Adding a tool to automatically click unsubscribe buttons in newsletters.
- Enhancing email categorization with machine learning.
- Implementing smart reply suggestions.
- Integrating calendar and task management features.
- Supporting multiple email providers beyond Gmail.
- Improving natural language understanding for complex commands.
Conclusion
This Smart Email Assistant project showcases how AI and automation can simplify email management. By combining Streamlit’s ease of use, Langchain’s powerful AI agents, and the Gmail API, you can build a personalized assistant to keep your inbox organized and manageable.
Feel free to check out the project on GitHub and try it yourself!
GitHub Repository: https://github.com/sonu2164/smart-email-assistant/