Gmail 工具組
這將幫助您開始使用 GMail 工具組。此工具組與 GMail API 互動,以讀取訊息、草擬和傳送訊息等等。如需所有 GmailToolkit 功能和組態的詳細文件,請前往 API 參考。
設定
若要使用此工具組,您需要依照 Gmail API 文件中說明的步驟設定您的憑證。下載 credentials.json
檔案後,您就可以開始使用 Gmail API。
安裝
此工具組位於 langchain-google-community
套件中。我們需要 gmail
額外功能
%pip install -qU langchain-google-community\[gmail\]
如果您想要從個別工具的執行中取得自動化追蹤,您也可以設定您的 LangSmith API 金鑰,方法是取消註解下方內容
# os.environ["LANGSMITH_TRACING"] = "true"
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
實例化
預設情況下,此工具組會讀取本機 credentials.json
檔案。您也可以手動提供 Credentials
物件。
from langchain_google_community import GmailToolkit
toolkit = GmailToolkit()
API 參考:GmailToolkit
自訂驗證
在幕後,googleapi
資源是使用下列方法建立的。您可以手動建立 googleapi
資源以取得更多驗證控制。
from langchain_google_community.gmail.utils import (
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 = 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>)]
在代理程式中使用
下方顯示如何將工具組整合到代理程式中。
我們將需要 LLM 或聊天模型
選擇聊天模型
pip install -qU "langchain[openai]"
import getpass
import os
if not os.environ.get("OPENAI_API_KEY"):
os.environ["OPENAI_API_KEY"] = getpass.getpass("Enter API key for OpenAI: ")
from langchain.chat_models import init_chat_model
llm = init_chat_model("gpt-4o-mini", model_provider="openai")
from langgraph.prebuilt import create_react_agent
agent_executor = create_react_agent(llm, tools)
API 參考: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 event in events:
event["messages"][-1].pretty_print()
================================[1m Human Message [0m=================================
Draft an email to fake@fake.com thanking them for coffee.
==================================[1m Ai Message [0m==================================
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
=================================[1m Tool Message [0m=================================
Name: create_gmail_draft
Draft created. Draft Id: r-7233782721440261513
==================================[1m Ai Message [0m==================================
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 參考
如需所有 GmailToolkit
功能和組態的詳細文件,請前往 API 參考。