In this section, we present how to connect Gmail accounts to MindsDB.

Gmail is a widely used and popular email service developed by Google.

By connecting your Gmail account to MindsDB, you can utilize various AI models available within MindsDB to summarize emails, detect spam, or even automate email replies.

Please note that currently you can connect your Gmail account to local MindsDB installation by providing a path to the credentials file stored locally.

If you want to connect your Gmail account to MindsDB Cloud, you can upload the credentials file, for instance, to your S3 bucket and provide a link to it as a parameter.

Prerequisites

Before proceeding, ensure the following prerequisites are met:

  1. Install MindsDB locally via Docker or use MindsDB Cloud.
  2. To connect Gmail to MindsDB, install the required dependencies following this instruction.
  3. Install or ensure access to Gmail.

Connection

The required arguments to establish a connection are as follows:

  • credentials_file local path to the credentials.json or credentials_url in case your file is uploaded to s3. Follow the instructions below to generate the credentials file.
  • scopes define the level of access granted. It is optional and by default it uses ’https://…/gmail.compose’ and ’https://…/gmail.readonly’ scopes.

In order to make use of this handler and connect the Google Calendar app to MindsDB, the following syntax can be used:

CREATE DATABASE mindsdb_gmail
WITH ENGINE = 'gmail',
parameters = {
    "credentials_file": "mindsdb/integrations/handlers/gmail_handler/credentials.json",
    "scopes": ['https://.../gmail.compose', 'https://.../gmail.readonly', ...]
};

Or, you can also connect by giving the credentials file from an s3 pre signed url. To do this you need to pass in the credentials_file parameter as a pre signed url. For example:

CREATE DATABASE mindsdb_gmail
WITH ENGINE = 'gmail',
parameters = {
    "credentials_url": "https://s3.amazonaws.com/your_bucket/credentials.json?response-content-disposition=inline&X-Amz-Security-Token=12312...",
    -- "scopes": ['SCOPE_1', 'SCOPE_2', ...] -- Optional scopes. By default 'https://.../gmail.compose' & 'https://.../gmail.readonly' scopes are used
};

You need a Google account in order to use this integration. Here is how to get the credentials file:

  1. Create a Google Cloud Platform (GCP) Project:

    1.1 Go to the GCP Console (https://console.cloud.google.com/).

    1.2 If you haven’t created a project before, you’ll be prompted to do so now.

    1.3 Give your new project a name.

    1.4 Click Create to create the new project.

  2. Enable the Gmail API:

    2.1 In the GCP Console, select your project.

    2.2 Navigate to APIs & Services > Library.

    2.3 In the search bar, search for Gmail.

    2.4 Click on Gmail API, then click Enable.

  3. Create credentials for the Gmail API:

    3.1 Navigate to APIs & Services > Credentials.

    3.2 Click on the Create Credentials button and choose OAuth client ID.

    3.3 If you haven’t configured the OAuth consent screen before, you’ll be prompted to do so now. Make sure to choose External for User Type, and select the necessary scopes. Make sure to save the changes.

    Now, create the OAuth client ID. Choose Web application for the Application Type and give it a name.

    3.4 Add the following MindsDB URL to Authorized redirect URIs:

    • For local installation, add http://localhost/verify-auth
    • For Cloud, add http://cloud.mindsdb.com/verify-auth.

    3.5 Click Create.

  4. Download the JSON file:

    4.1 After creating your credentials, click the download button (an icon of an arrow pointing down) on the right side of your client ID. This will download a JSON file, so you will use the location to it in the credentials_file param.

Usage

This creates a database called mindsdb_gmail. This database ships with a table called emails that we can use to search for emails as well as to write emails.

Now you can use your Gmail data, like this:

  • searching for email:

    SELECT *
    FROM mindsdb_gmail.emails
    WHERE query = 'alert from:*@google.com'
    AND label_ids = "INBOX,UNREAD"
    LIMIT 20;
    
  • writing emails:

    INSERT INTO mindsdb_gmail.emails (thread_id, message_id, to_email, subject, body)
    VALUES ('187cbdd861350934d', '8e54ccfd-abd0-756b-a12e-f7bc95ebc75b@Spark', 'test@example2.com', 'Trying out MindsDB',
            'This seems awesome. You must try it out whenever you can.');
    

Example 1: Automating Email Replies

Now that we know how to pull emails into our database and write emails, we can make use of OpenAPI engine to write email replies.

First, create an OpenAI engine, passing your OpenAI API key:

CREATE ML_ENGINE openai_engine
FROM openai
USING
    openai_api_key = 'your-openai-api-key';

Then, create a model using this engine:

CREATE MODEL mindsdb.gpt_model
PREDICT response
USING
    engine = 'openai_engine',
    max_tokens = 500,
    api_key = 'your_api_key', 
    model_name = 'gpt-3.5-turbo',
    prompt_template = 'From input message: {{body}}\
                        by from_user: {{sender}}\
                        In less than 500 characters, write an email response to {{sender}} in the following format:\
                        Start with proper salutation and respond with a short message in a casual tone, and sign the email with my name mindsdb';

Example 2: Detecting Spam Emails

You can check if an email is spam by using one of the Hugging Face pre-trained models.

CREATE MODEL mindsdb.spam_classifier                           
PREDICT PRED                           
USING
  engine = 'huggingface',              
  task = 'text-classification',        
  model_name = 'mrm8488/bert-tiny-finetuned-sms-spam-detection', 
  input_column = 'text_spammy',        
  labels = ['ham', 'spam'];

Then, create a view that contains the snippet or the body of the email.

CREATE VIEW mindsdb.emails_text AS(
    SELECT snippet AS text_spammy
    FROM mindsdb_gmail.emails
);

Finally, you can use the model to classify emails into spam or ham:

SELECT h.PRED, h.PRED_explain, t.text_spammy AS input_text
FROM mindsdb.emails_text AS t
JOIN mindsdb.spam_classifier AS h;

For more information about available actions and development plans, visit this page.