Saturday, March 2, 2024
No menu items!
HomeCloud ComputingEasily integrate machine learning models into applications with Vertex AI integration for...

Easily integrate machine learning models into applications with Vertex AI integration for Cloud Spanner

Cloud Spanner is a fully managed relational database that provides industry-leading  consistency and availability at any scale. Organizations of all sizes in industries like financial services, retail, and games rely on Spanner to run their mission critical applications, but efficiently running line-of-business applications is often not enough. They want to react to business or customer events faster and in a scalable way by leveraging machine learning (ML) models instead of relying on manual actions. Tasks like fraud detection, algorithmic trading, and bot detection are time sensitive and can have unpredictable loads. Spanner integration with Vertex AI, Google Cloud’s ML platform, lets users leverage ML models in Vertex AI using a simple SQL query in Spanner. Built-in integration with Vertex AI, available now in preview, unifies operational databases and AI, making it simpler to consume published ML models and to build AI-powered applications faster.  

Application developers often have limited bandwidth to integrate ML services with an application, as doing so requires a learning curve and long-term maintenance overhead. These developers have to create a separate module within their application or a separate application/service entirely to interact with ML services like Vertex AI. 

Conceptually, the module would send data to Vertex AI’s prediction model and pass the results to the application that will make a decision based on the results. Such an implementation leads to expansion of the application, added latency to decision making, and more moving parts to maintain. With Spanner Vertex AI integration, in contrast, developers can easily access models built by data scientists and apply them to database transactions using familiar SQL.

This is similar to how BigQuery ML natively supports ML to make it easier for users to leverage ML models within applications, but with an important distinction: Spanner’s Vertex AI integration allows predictions to be made on live transactions, while BigQuery ML analyzes transactions that have already completed on data stored in BigQuery. Spanner’s Vertex AI integration is important for use cases such as fraud detection or detection of toxic players, where you want to make the best decision possible during the transaction or event instead of detecting a poor decision later. This direct integration with Spanner lowers the entry barrier for adding ML to applications, provides lower latency and shorter times transaction locks are held, making it less susceptible to lock contention, an important benefit for busy transactional systems.

Getting started with Vertex AI integration

In this release of Vertex AI integration, we’ve made it easy to consume any existing model already published in Vertex AI. If you are unfamiliar with Vertex AI, check out this quick start videoand documentation on training a tabular data model. Let’s walk through an example of how a games company uses Vertex AI integration with Spanner to protect its users and  company from toxic players. 

Use case example: toxic player detection

Consider Cymbal Entertainment, a hypothetical games and social media company, that leverages Spanner to store information about all user activities, including chats, and comments in their forums. They want their systems to automatically trigger an investigation into potential user misconduct, especially toxic chat/comment behavior, as soon as that toxic behavior occurs. In cases like in-game chat/messaging, they want to detect harmful content as it is submitted by players. They have to evaluate the level of toxicity of the reported user’s comments and to make a decision on whether to flag the comment, suspend the user’s account, or apply any other reasonable measures.

This has to be done almost instantly or it may result in altercations or worse, players may abandon the platform completely. Cymbal Entertainment prides itself for its zero tolerance policy for online bullying and its millions of diverse users bear testimony to its success. Toxic users can damage their hard earned reputation and affect their business overnight. The size of Cymbal’s community and 24×7 activity level of its users makes any solution that relies on humans to prevent such events infeasible.

Preparing the model for Spanner

To automate the process of chat or comment toxicity evaluation, Cymbal Entertainment uses ML. They train a model following Vertex AI Text Classification guide using either data existing in their system or a publicly available dataset like Toxicity Comment Classification from Kaggle. Once the model is trained and deployed to a Vertex AI endpoint, it can be registered in Spanner:

code_block[StructValue([(u’code’, u’CREATE MODEL ToxicityClassifier rnINPUT (content STRING(MAX))rnOUTPUT (displayNames ARRAY<STRING(MAX)>, confidences ARRAY<FLOAT64>)rnREMOTE OPTIONS(rn endpoint = “//$PROJECT_ID/locations/$LOCATION/endpoints/$ENDPOINT_ID”rn);’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e6bf86fc2d0>)])]

The ToxicityClassifier model takes the comment text as input and outputs two arrays: display names and confidences. The former contains all the labels seen in the training dataset. The latter indicates the confidence score for the positionally corresponding label. In the following example, the result will indicate a toxic comment:

Inline toxicity prediction

While inserting comments into the Comments table, they calculate the set of toxicity ratings using the ToxicityClassifier model. According to their company policy, comments that contain identity attacks, severe toxicity, or threats are not allowed on the platform and need to be banned automatically by setting the is_banned flag of the comment record to true. Other comments that contain high ratings of toxicity can be marked as suspicious and re-directed for further investigation. The whole described operation can be executed using the following SQL statement:

code_block[StructValue([(u’code’, u’INSERT INTO Comments (user_id, comment_id, comment_text, is_banned, is_suspicious)rnSELECTrn user_id,rn comment_id,rn comment_text,rn (severe_toxic OR threat OR identity_hate) AS is_banned,rn (toxic OR obscene OR insult) AS is_suspiciousrnFROMrn (SELECTrn user_id,rn comment_id,rn content AS comment_text,rn confidences[OFFSET(0)] > 0.5 AS normal,rn confidences[OFFSET(1)] > 0.5 AS toxic,rn confidences[OFFSET(2)] > 0.5 AS severe_toxic,rn confidences[OFFSET(3)] > 0.5 AS obscene,rn confidences[OFFSET(4)] > 0.5 AS insult,rn confidences[OFFSET(5)] > 0.5 AS identity_hate,rn confidences[OFFSET(6)] > 0.5 AS threatrn FROMrn ML.PREDICT(MODEL ToxicityClassifier,rn (rn SELECTrn @user_id AS user_id,rn @comment_id AS comment_id,rn @comment_text AS contentrn )));’), (u’language’, u”), (u’caption’, <wagtail.wagtailcore.rich_text.RichText object at 0x3e6bf884bc50>)])]

Apart from the simplicity and better overall query performance with this approach, the data, decision and reasoning are stored in the database all in the same step. Accomplishing the same using external functions will require multiple steps and database transactions. 

As you can see (and try it for yourself), all the work needed to enrich Cymbal Entertainment’s application with ML capabilities is done by adding a few SQL commands to their existing code. The subscriptions and payments processing team can also add ML capabilities for payment fraud detection in the same way and just as easily.

Availability and pricing

Vertex AI integration with Spanner is currently in preview and available in all regions Spanner is available. There is no additional charge from Spanner for using this feature. Customers will need access to Vertex AI models and are subject to Vertex AI pricing based on the usage type.

Get started today

It has never been easier to enhance your Spanner applications with ML capabilities so try it out today. 

Use the sample in this blog or check out  documentation for more information.

If you are new to Spanner, try Spannerat no charge with a 90-day free trial instance.

Learn more about how Spanner provides unlimited scale and up to 99.999% availability.

Related Article

Come for the sample app, stay for the main course: Cloud Spanner free trial instances

Cloud Spanner now offers free trial instances with sample data and guided tutorials to try the fully managed relational database.

Read Article

Cloud BlogRead More



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments