Create Your Personal Gmail AI Agent Using LangChain
Would you like your own Gmail AI agent, that can help you do just about anything you do with emails and save you time and effort? You can do it in just 20 lines of code, thanks to LangChain.
Let’s first do some pre-requisite work before starting with LangChain.
We need to do the following tasks:
- Enable the Gmail API
- Get our Google account credentials.json file in a local folder
Enable the Gmail API
To enable the Gmail API, you need to log in at: https://console.cloud.google.com with your Google account.
Create a New Project.
- Enter a name for your project and click Create.
Enable Gmail API
- Click on APIs and Services.
and click on Library. Or, you may directly go to https://console.cloud.google.com/apis/library
- Search for Gmail API
- Click Enable
Download Credentials JSON File
- To download the Credentials JSON file, follow the instructions in the Google API docs.
- Save the JSON file as credentials.json file in the same folder as your Python program.
Now, let’s start work on creating the Gmail agent using Langchain with Python.
Create your personal Gmail agent
Install libraries
From the terminal, install the following libraries.
pip install --upgrade google-api-python-client
pip install --upgrade google-auth-oauthlib
pip install --upgrade google-auth-httplib2
pip install -U langchain-community
pip install -U langchain-openai langchainhub
Create the Tools
For our Gmail agent, we use GmailToolkit provided by Langchain.
from langchain_community.agent_toolkits import GmailToolkit
toolkit = GmailToolkit()
Next, we ensure that the gmail toolkit is linked to our gmail account through our credentials.json file.
from langchain_community.tools.gmail.utils import (
build_resource_service,
get_gmail_credentials,
)
credentials = get_gmail_credentials(
token_file="token.json",
scopes=["https://mail.google.com/"],
client_secrets_file="credentials.json",
)
api_resource = build_resource_service(credentials=credentials)
toolkit = GmailToolkit(api_resource=api_resource)
We then get the tools from the toolkit.
tools = toolkit.get_tools()
tools
We will also create the tool from Tavily if required to do a web search. You can get an API for free from tavily.com.
import os
os.environ['TAVILY_API_KEY'] = "Your Tavily API key here"
Create the llm
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(api_key="Your Open AI API key here")
Create the Gmail agent
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_functions_agent
Create the prompts
instructions = """You are an assistant."""
base_prompt = hub.pull("langchain-ai/openai-functions-template")
prompt = base_prompt.partial(instructions=instructions)
Create the agent
Create the agent using the llm, toolkits and the prompt.
agent = create_openai_functions_agent(llm, toolkit.get_tools(), prompt)
We use the AgentExecutor class from LangChain to create an AgentExecutor object from the agent.
agent_executor = AgentExecutor(
agent=agent,
tools=toolkit.get_tools(),
# This is set to False to prevent information about my email showing up on the screen
# Normally, it is helpful to have it set to True however.
verbose=False,
)
That’s it. Our own personal gmail agent is ready to be invoked.
agent_executor.invoke(
{
"input": "Create a test mail with text 'This is a test mail' and put in the Drafts mail folder."
}
)
When, the program is run, you will be asked to authorize the application by signing into your Google account. Once, you sign into your Google account, you will see a screen stating that Google hasn’t verified this app.
You may click on Advanced and go to your app.
Click Continue to provide access to your Google account.
The output after invoking the agent_executor is:
{'input': "Create a test mail with text 'This is a test mail' and put in the Drafts mail folder.",
'output': "I have created a draft email with the text 'This is a test mail'."}
Go to your Gmail Inbox, and you will see the mail in the Drafts folder. Your Gmail AI agent has done the work for you.
Now, let’s ask for a summary of a mail that has come from a specific person.
agent_executor.invoke(
{"input": "Could you briefly summarize the mail that has come from SuperHuman AI"}
)
The output is:
{'input': 'Could you briefly summarize the mail that has come from SuperHuman AI',
'output': 'The email from SuperHuman AI is titled "Academics debate AI\'s impact." It discusses various insights related to AI and technology, including topics like how AI will shape the economy, the CHIPS Act, new AI tools for productivity, AI-generated images, and more. The email provides information on recent developments in the AI and tech industry, including OpenAI\'s SearchGPT, Apple\'s share buyback program, TikTok\'s dispute with Universal Music Group, and Anthropic AI\'s app launch.'}
Congratulations! You have made your own personal Gmail agent willing to do any Gmail related work for you.
Previous Articles
- Beginner’s Guide to LangChain
- Beginner’s Guide To Retrieval Chain From LangChain
- Beginner’s Guide To Conversational Retrieval Chain Using LangChain
- Beginner’s Guide to Creating AI Agents With LangChain
- The Fascinating Mind of an AI Agent With LangChain
- Multiple AI Agents: Creating Multi-Agent Workflows Using LangGraph and LangChain
- Self-Reflecting AI Agents Using LangChain
- Hierarchical AI Agents: Create a Supervisor AI Agent Using LangChain