File size: 2,385 Bytes
cfd3735
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Gmail Toolkit\n",
    "\n",
    "**The Gmail Toolkit** allows you to create drafts, send email, and search for messages and threads using natural language.\n",
    "\n",
    "As a prerequisite, you will need to register with Google and generate a `credentials.json` file in the directory where you run this loader. See [here](https://developers.google.com/workspace/guides/create-credentials) for instructions.\n",
    "\n",
    "This example goes over how to use the Gmail Toolkit:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.llms import OpenAI\n",
    "from langchain.agents.agent_toolkits.gmail.base import create_gmail_agent\n",
    "import json\n",
    "\n",
    "llm = OpenAI(verbose=True)\n",
    "gmail_agent = create_gmail_agent(llm=llm, sender_name=\"Alice\", verbose=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "command = \"search for all messages during november 2022\"\n",
    "output = gmail_agent.run(command)\n",
    "\n",
    "messages = json.loads(output)\n",
    "\n",
    "print(\"Messages:\")\n",
    "for message in messages:\n",
    "  print(f\"{message['id']}: {message['snippet']}\")\n",
    "\n",
    "id = messages[0][\"id\"]\n",
    "\n",
    "command = f\"get the body for message id {id}\"\n",
    "\n",
    "output = gmail_agent.run(command)\n",
    "\n",
    "print(f\"Message body: {output}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "command = \"create a draft email to [email protected] explaining why I can't make the meeting next week.\"\n",
    "output = gmail_agent.run(command)\n",
    "\n",
    "print(output)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "agent-ui",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.8"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}