Docs
Official Tools
Function Tools
Gmail

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:

    NameTypeDescription
    tostr*Email address of the recipient
    subjectstr*Subject of the email
    messagestrThe email content in markdown format
    ccList[str]List of email addresses to CC
    bccList[str]List of email addresses to BCC

Example Prompts

  1. Send an Availability Inquiry Email

    Send an email to a@a.com asking if they are available this Friday
  2. Send an Email with CC

    Send a greeting email to a@a.com and CC b@b.com
  3. 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

  1. Fetch Latest Unread Emails

    Get the lastest unread emails in the inbox.
  2. 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

  1. 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

  1. 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

  1. Remove a Label From Emails Sent From a Given Sender

    Remove label 'TEST' from the latest email from a@a.com.
Didn't find the feature you were looking for? Feel free to submit an issue or PR!

Footnotes

Footnotes

  1. 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.