Gmail 工具
¥Gmail Tool
Gmail 工具允许你的代理从链接的电子邮件账户创建和查看消息。
¥The Gmail Tool allows your agent to create and view messages from a linked email account.
设置
¥Setup
你可以通过两种方式进行身份验证:
¥You can authenticate via two methods:
向凭证对象提供通过 OAuth2 令牌交换获得的访问令牌。这可以是字符串或函数,以便处理令牌过期和验证。这可以通过使用支持从联合连接获取访问令牌的身份提供者来实现。这是最安全的方法,因为访问权限和范围将仅限于特定的终端用户。如果在供拥有自己 Gmail 账户的终端用户使用的应用中使用该工具,则此方法更为合适。
¥Provide an access token, obtained via OAuth2 token exchange, to the credentials object. This can be a string or a function so that token expiry and validation can be handled. This can be done using an Identity Provider that supports getting access tokens from federated connections. This is the most secure method as the access and scope will be limited to the specific end user. This method will be more appropriate when using the tool in an application that is meant to be used by end users with their own Gmail account.
你需要从 Google 链接 和 启用新的 Gmail API 获取 API 密钥。然后,设置
GMAIL_CLIENT_EMAIL
、GMAIL_PRIVATE_KEY
或GMAIL_KEYFILE
的环境变量。¥You will need to get an API key from Google here and enable the new Gmail API. Then, set the environment variables for
GMAIL_CLIENT_EMAIL
, and eitherGMAIL_PRIVATE_KEY
, orGMAIL_KEYFILE
.
要使用 Gmail Tool,你需要安装以下官方依赖:
¥To use the Gmail Tool you need to install the following official peer dependency:
- npm
- Yarn
- pnpm
npm install @langchain/openai @langchain/community @langchain/core googleapis
yarn add @langchain/openai @langchain/community @langchain/core googleapis
pnpm add @langchain/openai @langchain/community @langchain/core googleapis
用法
¥Usage
import { initializeAgentExecutorWithOptions } from "langchain/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);
}
API Reference:
- initializeAgentExecutorWithOptions from
langchain/agents
- OpenAI from
@langchain/openai
- GmailCreateDraft from
@langchain/community/tools/gmail
- GmailGetMessage from
@langchain/community/tools/gmail
- GmailGetThread from
@langchain/community/tools/gmail
- GmailSearch from
@langchain/community/tools/gmail
- GmailSendMessage from
@langchain/community/tools/gmail
- StructuredTool from
@langchain/core/tools
相关
¥Related
工具 概念指南
¥Tool conceptual guide
工具 操作指南
¥Tool how-to guides