Supercharge Targeted Offers with BigQuery ML.GENERATE_TEXT

Ravish Garg
Google Cloud - Community
3 min readMar 15, 2024
Created by AI, By Author

In the competitive landscape of digital marketing, the imperative to deliver personalized content that resonates with each individual customer has never been more crucial. The advent of BigQuery ML.GENERATE_TEXT offers IT professionals an unparalleled opportunity to leverage artificial intelligence within their existing BigQuery workflows, thereby crafting personalized promotions, content, and insights directly from their customer data. This blog post aims to elucidate the capabilities of BigQuery ML.GENERATE_TEXT, providing a comprehensive example that goes beyond simple promotions to a multifaceted customer engagement strategy.

The Essence of BigQuery ML.GENERATE_TEXT

BigQuery ML.GENERATE_TEXT, a feature within Google’s BigQuery, capitalizes on the power of Google AI’s Large Language Models (LLMs) to generate contextually relevant and creative text outputs. Its applications range from generating personalized email subject lines and discount offers to drafting content for social media or customer support. The function’s flexibility and integration within BigQuery make it an essential tool for businesses aiming to enhance their customer engagement and data analysis through personalized content.

Prerequisites for Utilizing BigQuery ML.GENERATE_TEXT

Before diving into the operational details, it’s vital to establish a foundational setup that includes following:

  1. Google Cloud Project with BigQuery enabled and a dataset populated with customer data. This dataset should ideally encompass details such as customer IDs, segmentation, purchase history, and loyalty scores, which play a crucial role in tailoring the content generated by ML.GENERATE_TEXT.
  2. Create a Cloud resource connection and get the connections service account.
  3. Give the service account access with the role: ‘roles/aiplatform.user’.
  4. Create a model
CREATE OR REPLACE MODEL
`PROJECT_ID.DATASET_ID.MODEL_NAME`
REMOTE WITH CONNECTION `PROJECT_ID.REGION.CONNECTION_ID`
OPTIONS (ENDPOINT = 'ENDPOINT');

5. Now we are ready for our use-case.

Use Case: Dynamic Customer Engagement

Created by AI, By Author

Moving beyond the initial guide’s focus on personalized discounts and email subject lines, let’s explore how BigQuery ML.GENERATE_TEXT can facilitate a dynamic customer engagement strategy. Consider a scenario where a retail company seeks to not only offer personalized discounts but also provide tailored product recommendations and generate engaging content for each customer segment.

The following SQL query demonstrates how to use BigQuery ML.GENERATE_TEXT to perform this complex task:

WITH customer_data AS (
SELECT
customer_id,
segment,
STRING_AGG(product_name, ', ') AS favorite_products,
AVG(rating) AS average_rating
FROM
your_project.your_dataset.product_reviews
GROUP BY
customer_id, segment
),
generated_content AS (
SELECT
customer_id,
ml.GENERATE_TEXT(
MODEL 'your_project.your_dataset.engagement_model',
STRUCT(
CONCAT(
"Generate a personalized message that includes product recommendations based on the customer's favorite products: ",
favorite_products,
". Aim for a friendly tone and include a call to action."
) AS prompt,
0.7 AS temperature,
512 AS max_output_tokens
)
) AS generated_message
FROM
customer_data
)
SELECT
customer_data.customer_id,
customer_data.segment,
generated_content.generated_message
FROM
customer_data
JOIN
generated_content
ON
customer_data.customer_id = generated_content.customer_id;

This query first aggregates product reviews by customer, identifying favorite products and average ratings to inform the content generation process. It then invokes ML.GENERATE_TEXT to craft personalized messages for each customer, incorporating product recommendations and a call to action, all derived from their preferences and past interactions.

Key Considerations

  • Data Quality and Preparation: The input data’s quality is paramount. Ensuring it accurately reflects customer preferences and behavior is critical for generating relevant and engaging content.
  • Model Selection and Tuning: Choosing the right model and tuning parameters such as temperature and max output tokens can significantly affect the generated content’s style and relevance.
  • Integration and Automation: Integrating this process into existing customer engagement workflows can automate content creation, making it more efficient and scalable.

Conclusion

BigQuery ML.GENERATE_TEXT stands as a testament to the evolving capabilities of AI within the realm of customer engagement. By providing IT professionals with the tools to generate personalized and contextually relevant text directly within their BigQuery environment, it opens up new avenues for enhancing customer interaction and satisfaction. This example serves as a foundation upon which businesses can build more sophisticated, data-driven engagement strategies, tailored not just to sell but to genuinely connect with their audience.

--

--

Ravish Garg
Google Cloud - Community

Customer Engineer, Data Specialist @ Google Cloud. I assist customers transform & evolve their business via Google’s global network and software infrastructure.