Cloud Blog: How to integrate your Cloud SQL for MySQL database with Vertex AI & vector search

Source URL: https://cloud.google.com/blog/products/ai-machine-learning/integrate-your-cloud-sql-with-vertex-ai-and-vector-search/
Source: Cloud Blog
Title: How to integrate your Cloud SQL for MySQL database with Vertex AI & vector search

Feedly Summary: Search is a critical component of many modern applications – whether searching for products in an online storefront, finding solutions to your customers’ support cases, or building the perfect playlist. But traditional keyword searches often miss the deeper meaning of data. Vector embeddings, however, capture the complexities of your data, enabling highly accurate and powerful searches. Vector embeddings are numerical representations of your data, generated by models, that help computers understand nuances and meaningfully compare data points.
Cloud SQL for MySQL’s generally available vector support allows you to store vector embeddings, build persistent indexes on them, and perform ANN search between them. Now, we have a new Vertex AI integration that makes this process easy by helping you generate vector embeddings using a simple SQL function, eliminating the need for external embedding generation. You can also leverage any Vertex AI model directly from MySQL, bringing advanced AI capabilities closer to your data.
Ready to see how AI can improve searches in your application and help you understand and predict customer trends? Let’s dive in.

aside_block
), (‘btn_text’, ‘Start building for free’), (‘href’, ‘http://console.cloud.google.com/freetrial?redirectPath=/vertex-ai/’), (‘image’, None)])]>

Generate, store, & search vector embeddings
Let’s say you have an ecommerce application and are looking to see how Vertex AI and vector search with Cloud SQL for MySQL can power product searches and help you analyze customer reviews.
After setting up Vertex AI permissions and enabling the feature, you can start embedding your existing product data with the `mysql.ml_embedding` function. You just need to specify the model name and then pass the text you would like to embed–all formatting and parsing of Vertex AI requests is handled for you. 
For example, in your ecommerce application you can use a query like the following to directly store the output of `mysql.ml_embedding` in a VECTOR type column:

code_block
<ListValue: [StructValue([(‘code’, “ALTER TABLE my_products ADD COLUMN embedding vector(768) using varbinary;\r\n\r\n– In this example, product_description can be a text column describing the product in a given row\r\nUPDATE my_products SET embedding = mysql.ml_embedding(‘text-embedding-005’, product_description);"), (‘language’, ‘lang-sql’), (‘caption’, <wagtail.rich_text.RichText object at 0x3e00ecab2850>)])]>

To quickly find similar products, create a vector index for ANN search. You can select the best distance measure for your use case and customize the number of leaves to fine-tune speed and recall.

code_block
<ListValue: [StructValue([(‘code’, ‘CREATE VECTOR INDEX my_products_embedding_idx ON my_products(embedding) USING SCANN DISTANCE_MEASURE=COSINE NUM_LEAVES=500;’), (‘language’, ‘lang-sql’), (‘caption’, <wagtail.rich_text.RichText object at 0x3e00ecab2d60>)])]>

When a customer searches in your application, you can embed their query using `mysql.ml_embedding` and pass it to ANN search. For example, to find clothes for a specific occasion in your database:

code_block
<ListValue: [StructValue([(‘code’, "SELECT mysql.ml_embedding(‘text-embedding-005′,’Dress for a springtime black-tie wedding’) into @query_vector;\r\n\r\nSELECT\r\n approx_distance(embedding ,@query_vector, ‘distance_measure=cosine’) as distance\r\nFROM\r\n my_products\r\nORDER BY\r\n distance ASC\r\nLIMIT 5;"), (‘language’, ‘lang-sql’), (‘caption’, <wagtail.rich_text.RichText object at 0x3e00ecab2130>)])]>

Get responses from Gemini & other Vertex AI models
The Cloud SQL Vertex AI integration lets you make requests to any Vertex AI endpoint, including custom models fine-tuned to your datasets or pre-trained models like Gemini. This means that in addition to being able to generate vector embeddings for product descriptions, you can use Vertex AI to make predictions about things like what your users will purchase or whether a purchase is fraud.
For example, say you store customer reviews of your products in a MySQL database and want to perform analysis on them to get an idea of customer sentiment. You can invoke a Vertex AI LLM to determine customer satisfaction directly from the MySQL client and store its assessment.

code_block
<ListValue: [StructValue([(‘code’, ‘SELECT\r\n mysql.ml_predict_row(\r\n \’publishers/google/models/gemini-2.0-flash:generateContent\’,\r\n CONCAT(\r\n\'{ "contents": [{ "role": "user", "parts":[\r\n {\r\n "text":\r\n "Please rate customer sentiment from 1-10 based on the following review. Only output the number, nothing else. Text: \\n\’, review_content, \’ "\r\n }]}]}\r\n \’)) from customer_reviews;’), (‘language’, ”), (‘caption’, <wagtail.rich_text.RichText object at 0x3e00ecab2ee0>)])]>

Start building
Want to get started with the MySQL Vertex AI integration after seeing how it enhances product search and analysis? Explore our detailed codelab for an embeddings workflow tutorial or dive into our documentation for a comprehensive overview of all capabilities.

AI Summary and Description: Yes

Summary: The text discusses the integration of vector embeddings with Cloud SQL for MySQL, highlighting its utility in enhancing search capabilities and customer trend analysis through artificial intelligence. This development signifies a shift toward deeper data understanding, allowing businesses to utilize advanced AI models effectively within their databases.

Detailed Description: The content focuses on how vector embeddings can significantly improve applications that rely on search functionalities, such as e-commerce platforms and customer support systems. Here are the major points outlined:

– **Introduction to Vector Embeddings**:
– Traditional keyword searches often fail to capture the underlying meaning of data.
– Vector embeddings allow for more nuanced comparisons and deeper data understanding by converting data into numerical representations.

– **Cloud SQL for MySQL Features**:
– General availability of vector support enables users to store and index vector embeddings for efficient Approximate Nearest Neighbor (ANN) searches.
– Integration with Vertex AI streamlines the generation of these embeddings directly using SQL functions, reducing reliance on external tools.

– **Operational Setup**:
– Users can add embedding capabilities to their existing databases by specifying models and utilizing a SQL command to transform product descriptions into embeddings.
– The text explains how to alter database tables to include vector embeddings and how to create vector indices for rapid search capabilities.

– **Enhanced Search Capabilities**:
– Users can conduct searches for similar products based on customer queries through embedding transformations and querying vector embeddings for closest matches.
– This functionality is essential for optimizing user experience in e-commerce settings by improving product discovery.

– **Predictive Capabilities**:
– The integration allows direct access to predictions from Vertex AI models, like Gemini, aiding in marketing analytics, fraud detection, and customer sentiment analysis.
– By analyzing customer reviews stored in MySQL databases, businesses can generate insights on consumer behavior and satisfaction using AI.

– **Call to Action**:
– Encourages users to explore further documentation and tutorials to implement the MySQL Vertex AI integration in their applications.

The advancements indicated in this text provide practical implications for professionals in AI, cloud computing, and software development, helping to elevate data handling and analysis through integrated AI capabilities. This represents a significant step towards leveraging AI for enhanced operational insights and customer engagement within databases.