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,
});
// 这些是 Gmail 工具的默认参数
// const gmailParams = {
// credentials: {
// clientEmail: process.env.GMAIL_CLIENT_EMAIL,
// privateKey: process.env.GMAIL_PRIVATE_KEY,
// // 需要 (privateKey + clientEmail) 或 accessToken 之一
// accessToken: "an access token or function to get access token",
// },
// scopes: ["https://mail.google.com/"], // 如果使用访问令牌则不需要
// };
// 对于自定义参数,请取消注释上面的代码,用您自己的值替换,并将其传递给下面的工具
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 = `为我创建一个 Gmail 草稿以供编辑,内容是一封信,从一只寻求与疏远的朋友(一只猫)合作进行研究的有感知力的鹦鹉的角度出发。在任何情况下都不得发送该消息。`;
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 = `您能在我草稿中搜索最新的邮件吗?`;
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);
}