EventGet 50% off your ticket to MongoDB.local NYC on May 2. Use code Web50!Learn more >>
MongoDB Developer
MongoDB
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
MongoDBchevron-right

Using MongoDB Atlas Triggers to Summarize Airbnb Reviews with OpenAI

Pavel Duchovny4 min read • Published Oct 31, 2023 • Updated Oct 31, 2023
AINode.jsChange StreamsJavaScriptMongoDB
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
In the realm of property rentals, reviews play a pivotal role. MongoDB Atlas triggers, combined with the power of OpenAI's models, can help summarize and analyze these reviews in real-time. In this article, we'll explore how to utilize MongoDB Atlas triggers to process Airbnb reviews, yielding concise summaries and relevant tags.
AI review UI and tags.
This article is an additional feature added to the hotels and apartment sentiment search application developed in Leveraging OpenAI and MongoDB Atlas for Improved Search Functionality.

Introduction

MongoDB Atlas triggers allow users to define functions that execute in real-time in response to database operations. These triggers can be harnessed to enhance data processing and analysis capabilities. In this example, we aim to generate summarized reviews and tags for a sample Airbnb dataset.
Our original data model has each review embedded in the listing document as an array:

Prerequisites

  • App Services application (e.g., application-0). Ensure linkage to the cluster with the Airbnb data.
  • OpenAI account with API access.
Open AI Key Open AI Key

Secrets and Values

  1. Navigate to your App Services application.
  2. Under "Values," create a secret named openAIKey with your OPEN AI API key. Secrets and Values
  3. Create a linked value named OpenAIKey and link to the secret.

The trigger code

The provided trigger listens for changes in the sample_airbnb.listingsAndReviews collection. Upon detecting a new review, it samples up to 50 reviews, sends them to OpenAI's API for summarization, and updates the original document with the summarized content and tags. Atlas Trigger Configuration
Please notice that the trigger reacts to updates that were marked with "process" : false flag. This field indicates that there were no summary created for this batch of reviews yet.
Example of a review update operation that will fire this trigger:

Sample reviews function

To prevent overloading the API with a large number of reviews, a function sampleReviews is defined to randomly sample up to 50 reviews:

Main trigger logic

The main trigger logic is invoked when an update change event is detected with a "process" : false field.
Key steps include:
  • API request preparation: Reviews from the changed document are sampled and prepared into a request string for the OpenAI API. The format and instructions are tailored to ensure the API returns a valid JSON with summarized content and tags.
  • API interaction: Using the context.http.post method, the trigger sends the prepared data to the OpenAI API.
  • Updating the original document: Upon a successful response from the API, the trigger updates the original document with the summarized content, negative tags (neg_tags), positive tags (pos_tags), and a process flag set to true.
Here is a sample result that is added to the processed listing document:
Once the data is added to our documents, providing this information in our VUE application is as simple as adding this HTML template:

Conclusion

By integrating MongoDB Atlas triggers with OpenAI's powerful models, we can efficiently process and analyze large volumes of reviews in real-time. This setup not only provides concise summaries of reviews but also categorizes them into positive and negative tags, offering valuable insights to property hosts and potential renters.
Questions? Comments? Let’s continue the conversation over in our community forums.

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Article

8 Best Practices for Building FastAPI and MongoDB Applications


Apr 23, 2024 | 8 min read
Tutorial

Modernize your insurance data models with MongoDB Relational Migrator


Mar 04, 2024 | 14 min read
Quickstart

Aggregation Framework with Node.js 3.3.2 Tutorial


Aug 22, 2023 | 9 min read
Article

Handling MongoDB PHP Errors


Feb 03, 2023 | 7 min read
Table of Contents
  • Introduction