Documentation IndexFetch the complete documentation index at: /llms.txtUse this file to discover all available pages before exploring further.
Fetch the complete documentation index at: /llms.txt
Use this file to discover all available pages before exploring further.
Join us May 13th & May 14th at Interrupt, the Agent Conference by LangChain. Buy tickets >
Python
使用 LangChain Python 与 Gmail 聊天加载器集成。
pip install -qU google-auth google-auth-oauthlib google-auth-httplib2 google-api-python-client
import os.path from google.auth.transport.requests import Request from google.oauth2.credentials import Credentials from google_auth_oauthlib.flow import InstalledAppFlow SCOPES = ["https://www.googleapis.com/auth/gmail.readonly"] creds = None # 文件 token.json 存储用户的访问令牌和刷新令牌, # 并在首次授权流程完成后自动创建。 if os.path.exists("email_token.json"): creds = Credentials.from_authorized_user_file("email_token.json", SCOPES) # 如果没有(有效的)凭据可用,则让用户登录。 if not creds or not creds.valid: if creds and creds.expired and creds.refresh_token: creds.refresh(Request()) else: flow = InstalledAppFlow.from_client_secrets_file( # 请在此处填写您的凭据文件。请按照 https://cloud.google.com/docs/authentication/getting-started 创建 json 文件 "creds.json", SCOPES, ) creds = flow.run_local_server(port=0) # 保存凭据供下次运行使用 with open("email_token.json", "w") as token: token.write(creds.to_json())
from langchain_community.chat_loaders.gmail import GMailLoader
loader = GMailLoader(creds=creds, n=3)
data = loader.load()
# 有时可能会出现错误,我们会静默忽略 len(data)
2
from langchain_community.chat_loaders.utils import ( map_ai_messages, )
# 这将使由 hchase@langchain.com 发送的邮件成为 AI 消息 # 这意味着您将训练一个 LLM,使其模拟 hchase 的身份进行回复 training_data = list( map_ai_messages(data, sender="Harrison Chase [hchase@langchain.com](mailto:hchase@langchain.com)") )