[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["わかりにくい","hardToUnderstand","thumb-down"],["情報またはサンプルコードが不正確","incorrectInformationOrSampleCode","thumb-down"],["必要な情報 / サンプルがない","missingTheInformationSamplesINeed","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-09-04 UTC。"],[],[],null,["# Invoke online predictions from Cloud SQL instances\n\n\u003cbr /\u003e\n\nMySQL \\| [PostgreSQL](/sql/docs/postgres/invoke-online-predictions \"View this page for the PostgreSQL database engine\") \\| SQL Server\n\n\u003cbr /\u003e\n\n|\n| **Preview\n| --- [Cloud SQL for MySQL integration with Vertex AI](/sql/docs/mysql/integrate-cloud-sql-with-vertex-ai)**\n|\n|\n| This feature is subject to the \"Pre-GA Offerings Terms\" in the General Service Terms section\n| of the [Service Specific Terms](/terms/service-terms#1).\n|\n| You can process personal data for this feature as outlined in the\n| [Cloud Data Processing\n| Addendum](/terms/data-processing-addendum), subject to the obligations and restrictions described in the agreement under\n| which you access Google Cloud.\n|\n| Pre-GA features are available \"as is\" and might have limited support.\n|\n| For more information, see the\n| [launch stage descriptions](/products#product-launch-stages).\nThis page shows you how to invoke online predictions from a Cloud SQL instance.\n\n\u003cbr /\u003e\n\nCloud SQL lets you get online predictions in\nyour SQL code by calling the\n`mysql.ml_predict_row()`\nfunction. For more information, see [Build generative AI\napplications using Cloud SQL](/sql/docs/mysql/ai-overview).\n\nBefore you begin\n----------------\n\nBefore you can invoke online predictions from a Cloud SQL instance,\nyou must prepare your database and select an appropriate ML model.\n\n### Prepare your database\n\nTo prepare your database, [set up integration between Cloud SQL and Vertex AI.](/sql/docs/mysql/integrate-cloud-sql-with-vertex-ai)\n\n\u003cbr /\u003e\n\n### Select an ML model\n\nWhen you call the `mysql.ml_predict_row()` function,\nyou must specify the location of an ML model. The model that you specify\ncan be one of these:\n\n- A model that's running in the [Vertex AI Model Garden](/vertex-ai/docs/start/explore-models).\n\n The `mysql.ml_predict_row()` function supports\n invoking predictions only on tabular or custom models.\n- A Vertex AI model with an active endpoint that you have Identity and Access Management (IAM) permission to access.\n\n Cloud SQL doesn't support [private endpoints](/vertex-ai/docs/predictions/using-private-endpoints)\n for getting online predictions.\n\nInvoke online predictions\n-------------------------\n\nYou can use the `mysql.ml_predict_row()` SQL function\nto invoke online predictions against your data.\n\nThe format of the function's initial argument depends on whether the ML model\nthat you want to use is in the Vertex AI Model Garden\nor is an endpoint running in a Google Cloud project.\n\n### Use a model in the Vertex AI Model Garden\n\nTo invoke an online prediction using an ML model that's running in the\nVertex AI Model Garden, use the following syntax for the\n`mysql.ml_predict_row()` SQL function: \n\n SELECT mysql.ML_PREDICT_ROW('publishers/google/models/\u003cvar translate=\"no\"\u003eMODEL_ID\u003c/var\u003e', '{ \"instances\": [ \u003cvar translate=\"no\"\u003eINSTANCES\u003c/var\u003e ], \"parameters\":\n \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-s1\"\u003ePARAMETERS\u003c/span\u003e\u003c/var\u003e }');\n\nMake the following replacements:\n\n- \u003cvar translate=\"no\"\u003eMODEL_ID\u003c/var\u003e: the ID of the ML model to use (for example, `gemini-2.0`)\n\n- \u003cvar translate=\"no\"\u003eINSTANCES\u003c/var\u003e: the inputs to the prediction call, in JSON format\n\n- \u003cvar translate=\"no\"\u003ePARAMETERS\u003c/var\u003e: the parameters to the prediction call, in JSON format\n\n**Note:** You must store the ML model in the same project and region as your Cloud SQL instance. \n\n SELECT mysql.ML_PREDICT_ROW('publishers/google/models/\u003cvar translate=\"no\"\u003eMODEL_ID\u003c/var\u003e', '{ \"instances\": [ \u003cvar translate=\"no\"\u003eINSTANCES\u003c/var\u003e ], \"parameters\":\n \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-s1\"\u003ePARAMETERS\u003c/span\u003e\u003c/var\u003e }');\n\nFor information about the model's JSON response messages, see [Generative AI foundational model reference](/vertex-ai/docs/generative-ai/model-reference/overview#models). For examples, see [Example invocations](#examples).\n\n\u003cbr /\u003e\n\n### Use a Vertex AI model endpoint\n\nTo invoke an online prediction using a Vertex AI model endpoint, use the following syntax for the `mysql.ml_predict_row()` SQL function: \n\n SELECT mysql.ML_PREDICT_ROW('endpoints/\u003cvar translate=\"no\"\u003eENDPOINT_ID\u003c/var\u003e', '{ \"instances\": [ \u003cvar translate=\"no\"\u003eINSTANCES\u003c/var\u003e ], \"parameters\":\n \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-s1\"\u003ePARAMETERS\u003c/span\u003e\u003c/var\u003e }');\n\nMake the following replacements:\n\n- \u003cvar translate=\"no\"\u003eENDPOINT_ID\u003c/var\u003e: the ID of the model's endpoint\n\n- \u003cvar translate=\"no\"\u003eINSTANCES\u003c/var\u003e: the inputs to the prediction call, in JSON format\n\n- \u003cvar translate=\"no\"\u003ePARAMETERS\u003c/var\u003e: the parameters to the prediction call, in JSON format\n\n| **Note:** The endpoint must be located in the same project and region as your Cloud SQL instance.\n\nFor information about the model's JSON response messages, see\n[PredictResponse](/vertex-ai/docs/reference/rest/v1/PredictResponse).\n\n### Example invocations\n\nThe following example uses\n[PaLM 2 for Text](/vertex-ai/docs/generative-ai/model-reference/text), available\nin the Model Garden, to generate text based on a short prompt that's\nprovided as a literal argument to `mysql.ml_predict_row()`:\n`sql\nselect mysql.ML_PREDICT_ROW('publishers/google/models/gemini-2.0-flash:generateContent', '{\n\"contents\": [{\n\"role\": \"user\",\n\"parts\": [{\n\"text\": \"Write me a short poem about MySQL\"\n}]\n}]\n}');` **Note:** You can use PaLM 2 for Text foundation models only in the `us-central1` region.\n\nThe response is a JSON object. For more information about the format of the\nobject, see\n[Response body](/vertex-ai/docs/generative-ai/model-reference/text#response_body).\n\nThe next example modifies the previous one in the following ways:\n\n- The example uses the contents of the current database's `messages.message`\n column as input.\n\n- The example demonstrates the use of the\n `json_object()`\n function\n as an aid to formatting the function's parameters.\n\n select mysql.ML_PREDICT_ROW('projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/us-central1/publishers/google/models/text-bison', json_object('instances', json_object('prompt', message), 'parameters', json_object('maxOutputTokens', 1024,'topK', 40,'topP', 0.8,'temperature', 0.2))) from messages;\n\nFor every row in the `messages` table, the returned JSON object now contains\none entry in its `predictions` array.\n\nBecause the response is a JSON object, you can pull specific fields from it: \n\n select JSON_EXTRACT(mysql.ml_PREDICT_ROW('publishers/google/models/gemini-2.0-flash:generateContent', JSON_OBJECT(\n 'contents', JSON_ARRAY(\n JSON_OBJECT(\n 'role', 'user',\n 'parts', JSON_ARRAY(\n JSON_OBJECT(\n 'text', message\n )\n )\n )\n )\n )), '$.candidates[0].content.parts[0].text') from messages;\n\nFor more example arguments to `ml_predict_row()`, see\n[Try the Vertex AI Gemini API](/vertex-ai/docs/generative-ai/start/quickstarts/api-quickstart).\n\nWhat's next\n-----------\n\n- [Build generative AI applications using Cloud SQL](/sql/docs/mysql/ai-overview)\n\n- [Integrate Cloud SQL with Vertex AI](/sql/docs/mysql/integrate-cloud-sql-with-vertex-ai)\n\n- [Understand an example of an embedding workflow](/sql/docs/mysql/understand-example-embedding-workflow)\n\n- [Build LLM-powered applications using LangChain](/sql/docs/mysql/langchain)"]]