How to Build a ChatGPT Super App

SingleStore is a powerful multi-model database system and platform designed to support a wide variety of business use cases. Its distinctive features allow businesses to unify multiple database systems into a single platform, reducing the Total Cost of Ownership (TCO) and simplifying developer workflows by eliminating the need for complex integration tools.

In this article, we’ll explore how SingleStore can transform email campaigns for a web analytics company, enabling the creation of personalized and highly targeted email content.

The notebook file used in the article is available on GitHub.

Introduction

A web analytics company relies on email campaigns to engage with customers. However, a generic approach to targeting customers often misses opportunities to maximize business potential. A more effective solution would involve using a large language model (LLM) to craft personalized email messages.

Consider a scenario where user behavior data are stored in a NoSQL database like MongoDB, while valuable documentation resides in a vector database, such as Pinecone. Managing these multiple systems can become complex and resource-intensive, highlighting the need for a unified solution.

SingleStore, a versatile multi-model database, supports various data formats, including JSON, and offers built-in vector functions. It seamlessly integrates with LLMs, making it a powerful alternative to managing multiple database systems. In this article, we’ll demonstrate how easily SingleStore can replace both MongoDB and Pinecone, simplifying operations without compromising functionality.

In our example application, we’ll use an LLM to generate unique emails for our customers. To help the LLM learn how to target our customers, we’ll use a number of well-known analytics companies as learning material for the LLM. 

We’ll further customize the content based on user behavior. Customer data are stored in MongoDB. Different stages of user behavior are stored in Pinecone. The user behavior will allow the LLM to generate personalized emails. Finally, we’ll consolidate the data stored in MongoDB and Pinecone by using SingleStore.

Create a SingleStore Cloud Account

A previous article showed the steps to create a free SingleStore Cloud account. We’ll use the Standard Tier and take the default names for the Workspace Group and Workspace. We’ll also enable SingleStore Kai.

We’ll store our OpenAI API Key and Pinecone API Key in the secrets vault using OPENAI_API_KEY and PINECONE_API_KEY, respectively.

Import the Notebook

We’ll download the notebook from GitHub.

From the left navigation pane in the SingleStore cloud portal, we’ll select “DEVELOP” > “Data Studio.”

In the top right of the web page, we’ll select “New Notebook” > “Import From File.” We’ll use the wizard to locate and import the notebook we downloaded from GitHub.

Run the Notebook

Generic Email Template

We’ll start by generating generic email templates and then use an LLM to transform them into personalized messages for each customer. This way, we can address each recipient by name and introduce them to the benefits of our web analytics platform.

We can generate a generic email as follows:

Python

 

For example, Alice would see the following message:

Plain Text

 

Other users would receive the same message, but with their name, respectively.

2. Adding a Large Language Model (LLM)

We can easily bring an LLM into our application by providing it with a role and giving it some information, as follows:

Python

 

We’ll create a function to call the LLM:

Python

 

Looping through the list of users and calling the LLM produces unique emails:

Python

 

For example, this is what Alice might see:

Plain Text

 

Equally unique emails will be generated for the other users.

3. Customizing Email Content With User Behavior

By categorising users based on their behavior stages, we can further customize email content to align with their specific needs. An LLM will assist in crafting emails that encourage users to progress through different stages, ultimately improving their understanding and usage of various services.

At present, user data are held in a MongoDB database with a record structure similar to the following:

JSON

 

We’ll connect to MongoDB to get the data as follows:

Python

 

We’ll replace <password> and <host> with the values from MongoDB Atlas.

We have a number of user behavior stages:

Python

 

Using the data about behavior stages, we’ll ask the LLM to further customize the email as follows:

Python

 

For example, here is an email generated for Michael:

Plain Text

 

4. Further Customizing Email Content

To support user progress, we’ll use Pinecone’s vector embeddings, allowing us to direct users to relevant documentation for each stage. These embeddings make it effortless to guide users toward essential resources and further enhance their interactions with our product.

Python

 

We’ll create the embeddings as follows:

Python

 

We’ll search Pinecone for matches as follows:

Python

 

Using the data, we can ask the LLM to further customize the email, as follows:

Python

 

For example, here is an email generated for Melissa:

Plain Text

 

We can see that we have refined the generic template and developed quite targeted emails.

Using SingleStore

Instead of managing separate database systems, we’ll streamline our operations by using SingleStore. With its support for JSON, text, and vector embeddings, we can efficiently store all necessary data in one place, reducing TCO and simplifying our development processes.

We’ll ingest the data from MongoDB using a pipeline similar to the following:

SQL

 

We’ll replace <primary>, <secondary>, <secondary> and <password> with the values from MongoDB Atlas.

The customer table will be created by the pipeline. The vector embeddings for the behavior stages can be created as follows:

Python

 

We’ll need a table to store the data:

SQL

 

Then, we can save the data in the table:

Python

 

We’ll search SingleStore for matches as follows:

Python

 

Using the data, we can ask the LLM to customize the email as follows:

Python

 

For example, here is an email generated for Joseph:

Plain Text

 

Summary

Through this practical demonstration, we’ve seen how SingleStore improves our email campaigns with its multi-model capabilities and AI-driven personalization. Using SingleStore as our single source of truth, we’ve simplified our workflows and ensured that our email campaigns deliver maximum impact and value to our customers.

Acknowledgements

I thank Wes Kennedy for the original demo code, which was adapted for this article.

Source:
https://dzone.com/articles/how-to-build-a-chatgpt-super-app