Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
OurBuilding Ambient Agents with LangGraph course is now available on LangChain Academy!
Open In ColabOpen on GitHub

Gmail Toolkit

This will help you get started with the GMailtoolkit. This toolkit interacts with the GMail API to read messages, draft and send messages, and more. For detailed documentation of all GmailToolkit features and configurations head to theAPI reference.

Setup

To use this toolkit, you will need to set up your credentials explained in theGmail API docs. Once you've downloaded thecredentials.json file, you can start using the Gmail API.

Installation

This toolkit lives in thelangchain-google-community package. We'll need thegmail extra:

%pip install-qU langchain-google-community\[gmail\]

To enable automated tracing of individual tools, set yourLangSmith API key:

# os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")

Instantiation

By default the toolkit reads the localcredentials.json file. You can also manually provide aCredentials object.

from langchain_google_communityimport GmailToolkit

toolkit= GmailToolkit()
API Reference:GmailToolkit

Customizing Authentication

Behind the scenes, agoogleapi resource is created using the following methods.you can manually build agoogleapi resource for more auth control.

from langchain_google_community.gmail.utilsimport(
build_resource_service,
get_gmail_credentials,
)

# Can review scopes here https://developers.google.com/gmail/api/auth/scopes
# For instance, readonly scope is 'https://www.googleapis.com/auth/gmail.readonly'
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)

Tools

View available tools:

tools= toolkit.get_tools()
tools
[GmailCreateDraft(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
GmailSendMessage(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
GmailSearch(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
GmailGetMessage(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>),
GmailGetThread(api_resource=<googleapiclient.discovery.Resource object at 0x1094509d0>)]

Use within an agent

Below we show how to incorporate the toolkit into anagent.

We will need a LLM or chat model:

pip install -qU "langchain[google-genai]"
import getpass
import os

ifnot os.environ.get("GOOGLE_API_KEY"):
os.environ["GOOGLE_API_KEY"]= getpass.getpass("Enter API key for Google Gemini: ")

from langchain.chat_modelsimport init_chat_model

llm= init_chat_model("gemini-2.0-flash", model_provider="google_genai")
from langgraph.prebuiltimport create_react_agent

agent_executor= create_react_agent(llm, tools)
API Reference:create_react_agent
example_query="Draft an email to fake@fake.com thanking them for coffee."

events= agent_executor.stream(
{"messages":[("user", example_query)]},
stream_mode="values",
)
for eventin events:
event["messages"][-1].pretty_print()
================================ Human Message =================================

Draft an email to fake@fake.com thanking them for coffee.
================================== Ai Message ==================================
Tool Calls:
create_gmail_draft (call_slGkYKZKA6h3Mf1CraUBzs6M)
Call ID: call_slGkYKZKA6h3Mf1CraUBzs6M
Args:
message: Dear Fake,

I wanted to take a moment to thank you for the coffee yesterday. It was a pleasure catching up with you. Let's do it again soon!

Best regards,
[Your Name]
to: ['fake@fake.com']
subject: Thank You for the Coffee
================================= Tool Message =================================
Name: create_gmail_draft

Draft created. Draft Id: r-7233782721440261513
================================== Ai Message ==================================

I have drafted an email to fake@fake.com thanking them for the coffee. You can review and send it from your email draft with the subject "Thank You for the Coffee".

API reference

For detailed documentation of allGmailToolkit features and configurations head to theAPI reference.

Related


[8]ページ先頭

©2009-2025 Movatter.jp