跳到主要内容
Gmail 工具允许您的代理创建和查看来自关联电子邮件帐户的消息。

设置

您可以通过两种方式进行身份验证
  1. 将通过 OAuth2 令牌交换获得的访问令牌提供给凭据对象。这可以是一个字符串或一个函数,以便可以处理令牌过期和验证。这可以使用支持从联合连接获取访问令牌的身份提供商来完成。这是最安全的方法,因为访问和范围将限于特定的最终用户。当在旨在供最终用户使用其自己的 Gmail 帐户的应用程序中使用该工具时,此方法将更适用。
  2. 您需要在此处从 Google 获取 API 密钥启用新的 Gmail API。然后,设置 GMAIL_CLIENT_EMAILGMAIL_PRIVATE_KEYGMAIL_KEYFILE 的环境变量。
要使用 Gmail 工具,您需要安装以下官方对等依赖项
有关安装 LangChain 软件包的一般说明,请参阅此部分
npm
npm install @langchain/openai @langchain/community @langchain/core googleapis

用法

import { initializeAgentExecutorWithOptions } from "@langchain/classic/agents";
import { OpenAI } from "@langchain/openai";
import {
  GmailCreateDraft,
  GmailGetMessage,
  GmailGetThread,
  GmailSearch,
  GmailSendMessage,
} from "@langchain/community/tools/gmail";
import { StructuredTool } from "@langchain/core/tools";

export async function run() {
  const model = new OpenAI({
    temperature: 0,
    apiKey: process.env.OPENAI_API_KEY,
  });

  // These are the default parameters for the Gmail tools
  //   const gmailParams = {
  //     credentials: {
  //       clientEmail: process.env.GMAIL_CLIENT_EMAIL,
  //       privateKey: process.env.GMAIL_PRIVATE_KEY,
  //       // Either (privateKey + clientEmail) or accessToken is required
  //       accessToken: "an access token or function to get access token",
  //     },
  //     scopes: ["https://mail.google.com/"], // Not required if using access token
  //   };

  // For custom parameters, uncomment the code above, replace the values with your own, and pass it to the tools below
  const tools: StructuredTool[] = [
    new GmailCreateDraft(),
    new GmailGetMessage(),
    new GmailGetThread(),
    new GmailSearch(),
    new GmailSendMessage(),
  ];

  const gmailAgent = await initializeAgentExecutorWithOptions(tools, model, {
    agentType: "structured-chat-zero-shot-react-description",
    verbose: true,
  });

  const createInput = `Create a gmail draft for me to edit of a letter from the perspective of a sentient parrot who is looking to collaborate on some research with her estranged friend, a cat. Under no circumstances may you send the message, however.`;

  const createResult = await gmailAgent.invoke({ input: createInput });
  //   Create Result {
  //     output: 'I have created a draft email for you to edit. The draft Id is r5681294731961864018.'
  //   }
  console.log("Create Result", createResult);

  const viewInput = `Could you search in my drafts for the latest email?`;

  const viewResult = await gmailAgent.invoke({ input: viewInput });
  //   View Result {
  //     output: "The latest email in your drafts is from hopefulparrot@gmail.com with the subject 'Collaboration Opportunity'. The body of the email reads: 'Dear [Friend], I hope this letter finds you well. I am writing to you in the hopes of rekindling our friendship and to discuss the possibility of collaborating on some research together. I know that we have had our differences in the past, but I believe that we can put them aside and work together for the greater good. I look forward to hearing from you. Sincerely, [Parrot]'"
  //   }
  console.log("View Result", viewResult);
}

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.