Gmail 工具包 (Gmail Toolkit)
这将帮助您开始使用 GMail 工具包。此工具包与 GMail API 交互以读取邮件、草拟和发送邮件等。有关所有 GmailToolkit 功能和配置的详细文档,请转到API 参考。(This will help you getting started with the GMail toolkit. 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 the API reference.)
设置
要使用此工具包,您需要设置您的凭据,这在Gmail API 文档中进行了说明。下载完credentials.json
文件后,您就可以开始使用 Gmail API 了。(To use this toolkit, you will need to set up your credentials explained in the Gmail API docs. Once you've downloaded the credentials.json
file, you can start using the Gmail API.)
安装
此工具包位于langchain-google-community
包中。我们需要gmail
额外内容 (This toolkit lives in the langchain-google-community
package. We'll need the gmail
extra)
%pip install -qU langchain-google-community\[gmail\]
如果您希望从单个工具的运行中获得自动跟踪,您还可以通过取消以下注释来设置您的LangSmith API 密钥。(If you want to get automated tracing from runs of individual tools, you can also set your LangSmith API key by uncommenting below)
# os.environ["LANGCHAIN_TRACING_V2"] = "true"
# os.environ["LANGCHAIN_API_KEY"] = getpass.getpass("Enter your LangSmith API key: ")
实例化
默认情况下,工具包读取本地credentials.json
文件。您也可以手动提供一个Credentials
对象。(By default the toolkit reads the local credentials.json
file. You can also manually provide a Credentials
object.)
from langchain_google_community import GmailToolkit
toolkit = GmailToolkit()
自定义身份验证
在幕后,使用以下方法创建了一个googleapi
资源。您可以手动构建一个googleapi
资源以获得更多身份验证控制。(Behind the scenes, a googleapi
resource is created using the following methods. you can manually build a googleapi
resource for more auth control.)
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)
工具
查看可用工具 (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>)]
在代理中使用
下面我们展示如何将工具包集成到代理中。(Below we show how to incorporate the toolkit into an agent.)
我们将需要一个 LLM 或聊天模型 (We will need a LLM or chat model)
- OpenAI
- Anthropic
- Azure
- Cohere
- NVIDIA
- FireworksAI
- Groq
- MistralAI
- TogetherAI
pip install -qU langchain-openai
import getpass
import os
os.environ["OPENAI_API_KEY"] = getpass.getpass()
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini")
pip install -qU langchain-anthropic
import getpass
import os
os.environ["ANTHROPIC_API_KEY"] = getpass.getpass()
from langchain_anthropic import ChatAnthropic
llm = ChatAnthropic(model="claude-3-5-sonnet-20240620")
pip install -qU langchain-openai
import getpass
import os
os.environ["AZURE_OPENAI_API_KEY"] = getpass.getpass()
from langchain_openai import AzureChatOpenAI
llm = AzureChatOpenAI(
azure_endpoint=os.environ["AZURE_OPENAI_ENDPOINT"],
azure_deployment=os.environ["AZURE_OPENAI_DEPLOYMENT_NAME"],
openai_api_version=os.environ["AZURE_OPENAI_API_VERSION"],
)
pip install -qU langchain-google-vertexai
import getpass
import os
os.environ["GOOGLE_API_KEY"] = getpass.getpass()
from langchain_google_vertexai import ChatVertexAI
llm = ChatVertexAI(model="gemini-1.5-flash")
pip install -qU langchain-cohere
import getpass
import os
os.environ["COHERE_API_KEY"] = getpass.getpass()
from langchain_cohere import ChatCohere
llm = ChatCohere(model="command-r-plus")
pip install -qU langchain-nvidia-ai-endpoints
import getpass
import os
os.environ["NVIDIA_API_KEY"] = getpass.getpass()
from langchain import ChatNVIDIA
llm = ChatNVIDIA(model="meta/llama3-70b-instruct")
pip install -qU langchain-fireworks
import getpass
import os
os.environ["FIREWORKS_API_KEY"] = getpass.getpass()
from langchain_fireworks import ChatFireworks
llm = ChatFireworks(model="accounts/fireworks/models/llama-v3p1-70b-instruct")
pip install -qU langchain-groq
import getpass
import os
os.environ["GROQ_API_KEY"] = getpass.getpass()
from langchain_groq import ChatGroq
llm = ChatGroq(model="llama3-8b-8192")
pip install -qU langchain-mistralai
import getpass
import os
os.environ["MISTRAL_API_KEY"] = getpass.getpass()
from langchain_mistralai import ChatMistralAI
llm = ChatMistralAI(model="mistral-large-latest")
pip install -qU langchain-openai
import getpass
import os
os.environ["TOGETHER_API_KEY"] = getpass.getpass()
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(
base_url="https://api.together.xyz/v1",
api_key=os.environ["TOGETHER_API_KEY"],
model="mistralai/Mixtral-8x7B-Instruct-v0.1",
)
from langgraph.prebuilt import create_react_agent
agent_executor = create_react_agent(llm, tools)
example_query = "Draft an email to [email protected] 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 [email protected] 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: ['[email protected]']
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 [email protected] 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 参考。(For detailed documentation of all GmailToolkit
features and configurations head to the API reference.)
相关
- 工具概念指南 (Tool conceptual guide)
- 工具操作指南 (Tool how-to guides)