Gmail
The Gmail Tool allows you to streamline your email management tasks.
Setup
Get Google Workspace OAuth Credentials
Follow this guide to create a desktop app and download your oauth secret file: https://developers.google.com/workspace/guides/create-credentials#desktop-app (opens in a new tab)
Load Credentials
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
def load_google_credentials(token_file: str, scopes: List[str]):
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(token_file):
creds = Credentials.from_authorized_user_file(token_file, 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(
'./credentials/google.json', scopes
)
creds = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(token_file, 'w') as token:
token.write(creds.to_json())
return creds
gmail_creds = load_google_credentials(
token_file='path_to_client_secret.json',
scopes=['https://mail.google.com/']
)
Run the Gmail Tool
from npiai.tools import Gmail
async def main():
async with Gmail(gmail_creds) as gmail:
# do something with gmail.tools
print(gmail.tools)
Supported Features
Sending Emails
- Description: Send an email to a recipient
- In-Context Parameters1:
- Recipient's email address
- (Optional, Inferred) Email's subject
- (Optional, Inferred) Email's body
- (Optional) CC list
- (Optional) BCC list
Developer Insights
-
Function Name:
send_email
-
Function Parameters:
Name Type Description to
str
*Email address of the recipient subject
str
*Subject of the email message
str
The email content in markdown format cc
List[str]
List of email addresses to CC bcc
List[str]
List of email addresses to BCC
Example Prompts
-
Send an Availability Inquiry Email
Send an email to a@a.com asking if they are available this Friday
-
Send an Email with CC
Send a greeting email to a@a.com and CC b@b.com
-
Send an Email and Await a Reply
Send an email to a@a.com about an upcoming AI meetup and wait for their response. Possible meeting dates: Monday, Tuesday, Wednesday.
Fetch Emails
- Description: Search for and retrieve emails matching certain criteria.
- In-Context Parameters:
- (Optional, Inferred) Search query
- (Optional) Maximum number of messages to return
Example Prompts
-
Fetch Latest Unread Emails
Get the lastest unread emails in the inbox.
-
Retrieve Emails from a Specific Sender
Get all emails from a@a.com within the last 10 days.
Reply to Emails
- Description: Respond to an existing email conversation.
- In-Context Parameters:
- Target email to reply to (identified through a search based on given instructions).
Example Prompts
-
Reply to the Most Recent Email from a Given Sender
Reply to the newest email from a@a.com with your thoughts.
Add Labels
- Description: Assign labels to emails. The labels will be created if they do not exist.
- In-Context Parameters:
- Label name(s).
- Target emails receiving the label (identified through a search based on given instructions).
Example Prompts
-
Add a Label to Emails From a Given Sender
Add label 'TEST' to the latest email from a@a.com.
Remove Labels
- Description: Detach labels from selected emails.
- In-Context Parameters:
- Label name(s).
- Target emails for label removal (identified through a search based on given instructions).
Example Prompts
-
Remove a Label From Emails Sent From a Given Sender
Remove label 'TEST' from the latest email from a@a.com.
Footnotes
Footnotes
-
In-Context Parameters refer to the special information that you may specify in the instructions. Parameters marked as Inferred could be deduced from the context without needing explicit mention. ↩