How to build a custom Gmail Add-on w/ Gemini to find dates đź“…

Zack Akil
Google Cloud - Community
4 min readSep 6, 2024

Source code: https://github.com/ZackAkil/gmail-event-genie

Tired of manually copying dates and details from emails to your Google Calendar? Say hello to Event Genie, a custom Gmail add-on that leverages the power of Gemini Flash on Vertex AI to streamline your event scheduling process.

Event Genie in action

This blog post explores how we built Event Genie, a practical example of integrating Gemini into Google Workspace using Apps Script. If you’re a developer looking to harness the potential of generative AI within Workspace, read on!

Gmail Add-ons: Extending the Power of Email

Gmail add-ons are a fantastic way to augment the functionality of your inbox. They seamlessly integrate with Gmail, providing a handy side panel where you can interact with your emails in new ways. Creating a basic Gmail add-on is surprisingly easy with Apps Script. You can start with a boilerplate template and have a functional add-on in minutes! Check out their docs for how to get started.

Introducing Event Genie: AI-Driven Event Extraction

Event Genie focuses on a single problem: extracting important dates from emails and adding them to your Google Calendar.

Here’s how it works:

  1. AI-Powered Extraction: When you open an email, Event Genie uses the Gemini Flash model, accessed via the Vertex AI API, to identify potential event dates. We chose Gemini Flash for its speed, affordability, and advanced reasoning capabilities, making it perfect for this task.
  2. Intelligent Prompt Engineering: We’ve carefully designed a prompt that not only instructs Gemini to extract dates but also provides context about the current time. This allows the model to understand relative dates like “next Monday” accurately. See Vertex AI prompting guide.
const prompt = `
given the following email text, extract out the meaningful dates
that would be a good calendar events to make in the future,
order by the most important first.

If there is a relivant dates return JSON with fields
{"detectedEvents" : [ "startDateTime"-> json datetime UTZ,
"endDateTime"-> json datetime UTZ,
"eventTitle"-> string ,
"eventDescription" -> string,
"eventLocation"-> string | "" ]

if there is no specific time for a date,
have the time portion of the datetime be from 12:00 to 18:00 in
the current timezone

if there is no relivant dates, return {"detectedEvents" : []}

for context the message was recieved on ${messageDate}

---start of email text---
${text}
---end of email text---

JSON response:`

3. Seamless Calendar Integration: Event Genie automatically populates event details like title, description, and location, saving you time. Adding a calendar event using Apps Script is incredibly efficient with the built-in Calendar API:

function createEvent(eventTitle, eventDescription, eventLocation, eventStartDate, eventEndDate) {

const event = CalendarApp.getDefaultCalendar().createEvent(
eventTitle,
eventStartDate,
eventEndDate
);
event.setDescription(eventDescription);
event.setLocation(eventLocation);
Logger.log("Event created: " + event.getTitle() + " on " + eventStartDate);
return event;
}

4. Instant Validation: Event Genie presents the extracted information side-by-side with the email, allowing you to quickly verify its accuracy. This “Immediate Validate Output” (IVO) design pattern minimizes impacts of potential hallucinations and ensures you’re informed and in control.

Side by side view for instant verification

5. Source Linking: For added transparency, each created calendar event includes a link to the original email in its description. You can easily refer back to the source material if needed.

Source email embedding

Lessons Learned: Building Robust Generative AI Applications

Developing Event Genie taught us valuable lessons about building practical generative AI applications:

  • Specificity is Key: Focus on solving a specific problem well. Event Genie’s singular focus on date extraction allowed us to optimize the AI model and user interface for a seamless experience and solve one problem well.
  • Prompt Engineering is Crucial: Carefully crafted prompts, incorporating context and constraints, are essential for getting the most out of LLMs. For example, how we include the email date within the prompt enables Gemini to accurately extract out relative dates like “Next Monday”, or “tomorrow at 6”.
  • Validate, Validate, Validate: Implementing validation steps throughout the workflow, including user confirmation and source linking, builds trust and robustness. It doesn't prevent hallucinations, but it does enable to user to quickly catch them before they cause issues.

Looking Ahead: Control Generated Output

In retrospect, we would have used the “Control Generated Output” feature in Vertex AI to define a JSON schema for the output. This would have simplified both our prompt and parsing logic.

You can learn more about this powerful feature here: Control generated output.

Conclusion

Event Genie showcases the power of combining Gemini with Google Workspace. We hope this blog post inspires you to explore the possibilities of building your own generative AI applications within the Workspace ecosystem.

Useful links:

--

--

Zack Akil
Google Cloud - Community

ML/AI Engineer - Public Speaker - Developer Advocate - Google