Build Gmail AI Agent using LangChain Agents and OpenAI

Search for a command to run...

No comments yet. Be the first to comment.
Graphiti is a compact yet powerful Python library that converts raw text or JSON into an AI Knowledge Graph—a structured store of facts that acts as AI Agent Memory. Below is the exact workflow I used to load FutureSmart AI data into Neo4j, explore t...
In this blog we’ll walk through practical steps to add long‑term memory to your AI agents using Mem0 and LangGraph. We’ll build incrementally, tackling one section at a time so you can follow along and run the code as you read. Table of Contents Mem...

Retrieval-Augmented Generation (RAG) is becoming the go-to pattern for building AI systems that can fetch real-time or domain-specific knowledge on demand. But RAG alone doesn’t make your chatbot smart. With LangGraph, you can build stateful, agent-l...

Introduction In the era of advanced AI applications, Retrieval-Augmented Generation (RAG) stands out as a game-changing approach. By combining retrieval techniques with generative models, RAG enhances the quality, accuracy, and relevance of generated...

Introduction Search engines and retrieval systems have evolved to become remarkably intelligent. They no longer rely on exact keyword matches or rigid rules to find what you're looking for. Instead, they understand the context and meaning behind your...

The Gmail API allows developers to create applications that interact with user's emails. This capability enables developers to leverage the vast amounts of data and communication flows that occur daily, creating tools that enhance productivity, automate tasks, and introduce innovative ways to manage and organize information.
Now, if you’re dreaming of apps that are not just smart but genius-level 🧠, you’ve gotta check out LangChain Agents. It’s your golden ticket 🎫 to combining Google’s APIs with the brainpower of Large Language Models (LLMs). With LangChain Agents and Tools, you’re just a jump away from creating apps that are as clever as they come.
Want the inside scoop on LangChain and its integration with LLMs? Dive into our blogs Using Langchain and Open Source Vector DB Chroma for Semantic Search with OpenAI's LLM for all the deets. 📚
In this article, I’m going to walk you through a couple of nifty examples of how LangChain plays nice with Gmail API using LangChain Gmail Toolkit. You can create your own personalized Gmail AI Agent. Plus, I’ll spill the beans on some cool ways you can use these dynamic duos. Let’s get this started! 🎉
To use Google's APIs, developers will need to create a Google Cloud account and set up a project Click to learn how to Create and Managing Projects in Google Cloud
Authorize Google Credentials
If you don't know how to authorize Google credentials for your app, then let me give you a quick walkthrough
Step 1: Enable the API that you want to work on (Google Drive API, Gmail API, etc)

Search for the API name in the Google Cloud Console 👆
And Click on Enable 👇

Step 2: Configure the OAuth consent screen
In the Google Cloud console, go to Menu menu > APIs & Services > OAuth consent screen.
For User type select Internal, then click Create.
Complete the app registration form, then click Save and Continue.
For now, you can skip adding scopes and click Save and Continue.
Review your app registration summary. To make changes, click Edit. If the app registration looks OK, click Back to Dashboard.
Step 3: Download Credentials
In the Google Cloud console, go to Menu menu > APIs & Services > Credentials.
Click Create Credentials > OAuth client ID.
The OAuth client created screen appears, showing your new Client ID and Client secret. Click OK. The newly created credential appears under OAuth 2.0 Client IDs.
Save the downloaded JSON file as credentials.json, and move the file to your working directory.
pip install --upgrade --quiet google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client langchain langchain-community langchain-openai langchainhub
from langchain_community.agent_toolkits import GmailToolkit
from langchain_community.tools.gmail.utils import (
build_resource_service,
get_gmail_credentials,
)
credentials = get_gmail_credentials(
scopes=["https://mail.google.com/"],
client_secrets_file="credentials/credentials.json",
token_file="token.json"
)
api_resource = build_resource_service(credentials=credentials)
toolkit = GmailToolkit(api_resource=api_resource)
The first time you use get_gmail_credentials(), you will be displayed with the consent screen in your browser for user authentication. After authentication, 'token.json' will be created automatically at the provided or the default path. Also, if there is already a token.json at that path, then you will not be prompted for authentication. If you don’t know how to generate user access token, then follow this guide.
What are scopes?
Scopes are the level of access that you need or that is granted to an access token.For example, an access token issued to a client app may be granted READ and WRITE access to protected resources, or just READ access. So, if a client receives a token that has READ scope, and it tries to call an API endpoint that requires WRITE access, the call will fail.
You Can review scopes here 👇https://developers.google.com/gmail/api/auth/scopes (For instance, readonly scope is 'https://www.googleapis.com/auth/gmail.readonly' )
tools = toolkit.get_tools()
tools
[GmailCreateDraft(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>),
GmailSendMessage(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>),
GmailSearch(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>),
GmailGetMessage(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>),
GmailGetThread(api_resource=<googleapiclient.discovery.Resource object at 0x7d5cbc7d5e10>)]
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass()
from langchain import hub
from langchain.agents import AgentExecutor, create_openai_tools_agent
from langchain_openai import ChatOpenAI
instructions = """You are an assistant."""
base_prompt = hub.pull("langchain-ai/openai-functions-template")
prompt = base_prompt.partial(instructions=instructions)
llm = ChatOpenAI(temperature=0)
agent = create_openai_tools_agent(llm, toolkit.get_tools(), prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=toolkit.get_tools(),
verbose=True, #set it True to see what happens behind the scenes
)
agent_executor.invoke(
{"input": "Could you search in my drafts for the latest email? what is the title?"}
)
{'input': 'Could you search in my drafts for the latest email? what is the title?',
'output': 'The latest email in your drafts is titled "Reschedule Meeting to Thursday".'}
Instead of providing base prompt from langchain hub, you can pass your custom prompt to the agent. In this way, you can expect more desirable output from the agent. Also, if you're making a ChatBot, then it is easier for you to provide previous conversations or chat history.
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
prompt = ChatPromptTemplate.from_messages(
[
("system", """
You are a helpful assistant.
"""),
MessagesPlaceholder("chat_history", optional=True),
("human", "{input}"),
MessagesPlaceholder("agent_scratchpad"),
]
)
agent = create_openai_tools_agent(llm, tools, prompt)
agent_executor = AgentExecutor(
agent=agent,
tools=tools,
verbose=True,
return_intermediate_steps=True
)
response = agent_executor.invoke(
{
"input": user_query,
"chat_history": chat_history,
}
)
Email Automation: Automate sending and receiving emails, which can be particularly useful for customer support or marketing campaigns.
Message Retrieval: Search for and retrieve email messages or threads based on specific queries, aiding in information management and organization.
Draft Creation: Create email drafts automatically, allowing for quick follow-ups or scheduled correspondence.
Email Parsing: Extract and parse information from emails, which can be used for lead generation or data entry tasks.
In this article, we delved into the powerful capabilities of LangChain tools, specifically their integration with Gmail. By harnessing LangChain’s innovative solutions, users can seamlessly access and manipulate their documents, transforming mundane tasks into efficient workflows. Whether it’s extracting structured answers, building chatbots, or analyzing queries, LangChain empowers developers to create intelligent applications that bridge the gap between language and data.
Remember, the journey doesn’t end here. Dive deeper, explore further, and let LangChain be your guide to a more streamlined and intelligent digital experience. Happy coding! 🚀🔍📚