import os.pathfrom google.auth.transport.requests import Requestfrom google.oauth2.credentials import Credentialsfrom google_auth_oauthlib.flow import InstalledAppFlowSCOPES = ["https://www.googleapis.com/auth/gmail.readonly"]creds = None# The file token.json stores the user's access and refresh tokens, and is# created automatically when the authorization flow completes for the first# time.if os.path.exists("email_token.json"): creds = Credentials.from_authorized_user_file("email_token.json", SCOPES)# If there are no (valid) credentials available, let the user log in.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( # your creds file here. Please create json file as here https://cloud.google.com/docs/authentication/getting-started "creds.json", SCOPES, ) creds = flow.run_local_server(port=0) # Save the credentials for the next run with open("email_token.json", "w") as token: token.write(creds.to_json())
复制
向 AI 提问
from langchain_community.chat_loaders.gmail import GMailLoader
复制
向 AI 提问
loader = GMailLoader(creds=creds, n=3)
复制
向 AI 提问
data = loader.load()
复制
向 AI 提问
# Sometimes there can be errors which we silently ignorelen(data)
复制
向 AI 提问
2
复制
向 AI 提问
from langchain_community.chat_loaders.utils import ( map_ai_messages,)
复制
向 AI 提问
# This makes messages sent by hchase@langchain.com the AI Messages# This means you will train an LLM to predict as if it's responding as hchasetraining_data = list( map_ai_messages(data, sender="Harrison Chase [hchase@langchain.com](mailto:hchase@langchain.com)"))