- unwind ai
- Posts
- Build RAG App to Chat with your Gmail Inbox
Build RAG App to Chat with your Gmail Inbox
Fully Functional LLM App in just 30 lines of Python Code (step-by-step instructions)
Gemini with Gmail looks great! But is it worth $20 a month?
In just 30 lines of Python, you can build an AI assistant that connects with your Gmail inbox, retrieves email content, and answers questions about your emails using Retrieval-Augmented Generation (RAG) techniques and a selected LLM.
🎁 $50 worth AI Bonus Content at the end!
What We’re Building
This app uses RAG to scan your Gmail inbox and provide intelligent responses to your queries based on the content of your emails. It uses:
Streamlit: Provides a user-friendly interface where users can interact with the app and ask questions about their Gmail inbox.
Embedchain: Manages the knowledge base, which stores Gmail emails as documents, and integrates the LLM to generate responses.
Gmail API: Allows the app to securely connect to your Gmail account, retrieve email content, and add it to the knowledge base.
OpenAI GPT-4 Turbo: This LLM is used to generate accurate, context-aware responses based on the content of your emails.
Prerequisites
Before we begin, make sure you have:
Python installed (version 3.7 or higher is recommended)
Basic familiarity with Python programming
Your OpenAI API key (or another LLM provider’s API key)
Google Cloud Project with Gmail API enabled
OAuth credentials JSON file from Google Cloud
Step-by-Step Instructions
Step 1: Setting Up the Environment
First, let's get our development environment ready:
Clone the GitHub repository:
git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
Go to the Chat with Gmail folder
cd chat_with_gmail
Install the required dependencies:
pip install -r requirements.txt
Create a project in Google Cloud:
Head to Google Cloud Console https://console.cloud.google.com.
Click on 'Create Project', give it a name, and proceed.
Set up the OAuth Consent Screen:
Open OAuth Consent Screen: In your Google Cloud Console, navigate to "APIs & Services > OAuth consent screen".
Configure User Type: Select 'External' as the user type if your app will be used by users outside of your organization.
Publish the OAuth consent screen:
Provide the necessary app information, including the app name, user support email, and developer contact information.
After filling out all the details, click 'Save and Continue' through any additional screens.
Enable Gmail API & Create Auth Credentials:
Search for "Gmail API", select it, and click 'Enable'
Go to the Credentials tab
Select 'Create Credentials' > 'OAuth Client ID'
Choose 'Web application', name it, and add http://localhost:8080/ as a redirect URI.
Set up Gmail Credentials for RAG App
Download the credentials in JSON format and save them ascredentials.json
in your working directory. This file is crucial for authentication.
Step 2: Importing Libraries and Setting Up the App
Import Required Libraries: At the top of your file, add:
import tempfile
import streamlit as st
from embedchain import App
Configure the Embedchain App:
For this application, we will use GPT-4 Turbo. You can choose from Cohere, Anthropic or any other LLM of your choice.
Select the vector database as the opensource chroma db (you are free to choose any other vector database of your choice)
def embedchain_bot(db_path, api_key):
return App.from_config(
config={
"llm": {"provider": "openai", "config": {"model": "gpt-4-turbo", "temperature": 0.5, "api_key": api_key}},
"vectordb": {"provider": "chroma", "config": {"dir": db_path}},
"embedder": {"provider": "openai", "config": {"api_key": api_key}},
}
)
Set up the Streamlit App: Streamlit lets you create user interface with just python code, for this app we will:
Add a title to the app using 'st.title()'
Create a text input to enter their OpenAI API key using 'st.text_input()'
Set the gmail filter to select inbox
st.title("Chat with your Gmail Inbox 📧")
st.caption("This app allows you to chat with your Gmail inbox using OpenAI API")
# Get OpenAI API key from the user
openai_access_token = st.text_input("Enter your OpenAI API Key", type="password")
# Gmail filter to fetch emails
gmail_filter = "to: me label:inbox"
Initialize the Embedchain App:
If the OpenAI API key is provided, create a temporary directory for the vector database using 'tempfile.mkdtemp()'
Initialize the Embedchain app using the 'embedchain_bot' function
Add filtered emails to the knowledge base
if openai_access_token:
# Create a temporary directory for storing the database
db_path = tempfile.mkdtemp()
app = embedchain_bot(db_path, openai_access_token)
app.add(gmail_filter, data_type="gmail")
st.success("Added emails from Inbox to the knowledge base!")
Ask questions about your emails and get the answers:
Create a text input for the user to enter their question using 'st.text_input()'
If a question is asked, get the answer from the Embedchain app and display it using 'st.write()'
# Ask a question about the emails
prompt = st.text_input("Ask any question about your emails")
# Chat with the emails
if prompt:
answer = app.query(prompt)
st.write(answer)
Step 3: Running the App
With our code in place, it’s time to run the app locally to interact with your Gmail inbox.
Start the Streamlit App In your terminal, navigate to the project folder, and run the following command:
streamlit run chat_gmail.py
Access Your AI Assistant: Once the app starts, visit the URL provided by Streamlit (typically http://localhost:8501) to chat with your Gmail inbox.
Working Application Demo
Conclusion
Congratulations! You’ve successfully created a Gmail-chatting AI assistant in just 30 lines of Python. The app connects to your Gmail inbox and uses an LLM to answer questions based on the emails found in your inbox.
Feel free to explore more features like changing the Gmail filters or improving the question-answering logic.
We share hands-on tutorials like this 2-3 times a week, to help you stay ahead in the world of AI. If you're serious about levelling up your AI skills and staying ahead of the curve, subscribe now and be the first to access our latest tutorials.
Bonus worth $50 💵💰
Share this newsletter on your social channels and tag Unwind AI (X, LinkedIn, Threads, Facebook) to get AI resource pack worth $50 for FREE. Valid for limited time only!
Reply