[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-04。"],[[["\u003cp\u003eAlloyDB allows for online predictions within SQL code using the \u003ccode\u003eml_predict_row()\u003c/code\u003e function.\u003c/p\u003e\n"],["\u003cp\u003eBefore invoking predictions, you must set up integration between your database and Vertex AI, and grant users permissions to execute the \u003ccode\u003eml_predict_row()\u003c/code\u003e function.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eml_predict_row()\u003c/code\u003e function can use models from the Vertex AI Model Garden or a Vertex AI model with an active endpoint, using the respective project's details to identify it.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003eml_predict_row()\u003c/code\u003e function's input for the model is a JSON object, and the content should be formatted according to the requirements of the specific model being used, and the result is a JSON response.\u003c/p\u003e\n"],["\u003cp\u003eYou can modify the content of the json object in the \u003ccode\u003eml_predict_row()\u003c/code\u003e to be dynamic based on the content of the database.\u003c/p\u003e\n"]]],[],null,["# Invoke online predictions from AlloyDB databases\n\nSelect a documentation version: 16.3.0keyboard_arrow_down\n\n- [Current (16.8.0)](/alloydb/omni/current/docs/ai/invoke-predictions)\n- [16.8.0](/alloydb/omni/16.8.0/docs/ai/invoke-predictions)\n- [16.3.0](/alloydb/omni/16.3.0/docs/ai/invoke-predictions)\n- [15.12.0](/alloydb/omni/15.12.0/docs/ai/invoke-predictions)\n- [15.7.1](/alloydb/omni/15.7.1/docs/ai/invoke-predictions)\n- [15.7.0](/alloydb/omni/15.7.0/docs/ai/invoke-predictions)\n\n\u003cbr /\u003e\n\nThis page shows how to invoke online predictions from an AlloyDB for PostgreSQL\ndatabase.\n\nAlloyDB gives you the ability to get online predictions in\nyour SQL code by calling the `ml_predict_row()` function. For more information about using machine learning (ML) models with AlloyDB, see [Build generative AI\napplications](/alloydb/omni/16.3.0/docs/ai/overview-ai).\n\nBefore you begin\n----------------\n\nBefore you can invoke online predictions from an AlloyDB database,\nyou must prepare your database and select an appropriate ML model.\n\n### Prepare your database\n\n1. [Set up integration between your database and Vertex AI](/alloydb/omni/16.3.0/docs/install-with-alloydb-ai).\n\n2. Grant permission for database users to execute the `ml_predict_row()`\n function to run predictions:\n\n 1. Connect a `psql` client to the cluster's primary instance, as\n described in [Connect a psql client to an\n instance](/alloydb/omni/16.3.0/docs/run-connect#connect).\n\n 2. At the psql command prompt, connect to the database and grant permissions:\n\n \\c \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eDB_NAME\u003c/span\u003e\u003c/var\u003e\n\n GRANT EXECUTE ON FUNCTION ml_predict_row TO \u003cvar translate=\"no\"\u003e\u003cspan class=\"devsite-syntax-n\"\u003eUSER_NAME\u003c/span\u003e\u003c/var\u003e;\n\n Replace the following:\n - \u003cvar translate=\"no\"\u003eDB_NAME\u003c/var\u003e: the name of the database on which the\n permissions must be granted.\n\n - \u003cvar translate=\"no\"\u003eUSER_NAME\u003c/var\u003e: the name of the user for whom the\n permissions must be granted.\n\n### Select an ML model\n\nWhen you call the `ml_predict_row()` function, you must specify the location of an ML model.\nThe model that you specify can 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 `ml_predict_row()` function supports invoking predictions only on\n 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 AlloyDB doesn't support [private endpoints](/vertex-ai/docs/predictions/using-private-endpoints)\n for getting online predictions.\n\nInvoke online predictions\n-------------------------\n\nUse the `ml_predict_row()` SQL function to invoke online predictions\nagainst your data.\n\nThe format of the function's initial argument depends on whether the ML model that you\nwant to use is in the Vertex AI Model Garden or is an endpoint\nrunning 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 Vertex AI\nModel Garden, use the following syntax for the `ml_predict_row()` SQL function: \n\n SELECT ml_predict_row('projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/\u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e/publishers/google/models/\u003cvar translate=\"no\"\u003eMODEL_ID\u003c/var\u003e', '{ \u003cvar translate=\"no\"\u003eCONTENTS\u003c/var\u003e }');\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the ID of your Google Cloud project\n\n- \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e: the ID of the Google Cloud region that the model is\n located in---for example, `us-central1` for gemini-pro\n\n- \u003cvar translate=\"no\"\u003eMODEL_ID\u003c/var\u003e: the ID of the ML model to use---for example, gemini-pro\n\n- \u003cvar translate=\"no\"\u003eCONTENTS\u003c/var\u003e: the inputs to the prediction call, in JSON format\n\nIf the ML model is stored in the same project and region as your AlloyDB cluster,\nthen you can abbreviate this function's first argument: \n\n SELECT ml_predict_row('publishers/google/models/\u003cvar translate=\"no\"\u003eMODEL_ID\u003c/var\u003e', '{ \u003cvar translate=\"no\"\u003eCONTENTS\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).\n\nFor examples, see [Example invocations](#examples).\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 `ml_predict_row()` SQL function: \n\n SELECT ml_predict_row('projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/\u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e/endpoints/\u003cvar translate=\"no\"\u003eENDPOINT_ID\u003c/var\u003e', '{ \u003cvar translate=\"no\"\u003eCONTENTS\u003c/var\u003e }');\n\nReplace the following:\n\n- \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e: the ID of the Google Cloud project that the model is\n located in\n\n- \u003cvar translate=\"no\"\u003eREGION_ID\u003c/var\u003e: the ID of the Google Cloud region the model is\n located in---for example, `us-central1`\n\n- \u003cvar translate=\"no\"\u003eENDPOINT_ID\u003c/var\u003e: the ID of the model endpoint\n\n- \u003cvar translate=\"no\"\u003eCONTENTS\u003c/var\u003e: the inputs to the prediction call, in JSON format\n\nIf the endpoint is located in the same project and region as your AlloyDB cluster,\nthen you can abbreviate this function's first argument: \n\n SELECT ml_predict_row('endpoints/\u003cvar translate=\"no\"\u003eENDPOINT_ID\u003c/var\u003e', '{ \u003cvar translate=\"no\"\u003eCONTENTS\u003c/var\u003e }');\n\nFor information about the model's JSON response messages, see [PredictResponse](/vertex-ai/docs/reference/rest/v1/PredictResponse).\n\n### Example invocations\n\nThe following example uses [gemini-pro](/vertex-ai/docs/generative-ai/model-reference/text), available in the Model Garden, to generate text based on a short prompt that is provided as a literal argument\nto `ml_predict_row()`: \n\n select ML_PREDICT_ROW('projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/us-central1/publishers/google/models/gemini-1.0-pro:generateContent', '{\n \"contents\": [{\n \"role\": \"user\",\n \"parts\": [{\n \"text\": \"What is AlloyDB?\"\n }]\n }]\n }');\n\nThe response is a JSON object. For more information about the format of the object, see [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` column as input.\n\n- The example demonstrates the use of [the `json_build_object()` function](https://www.postgresql.org/docs/current/functions-json.html) as an aid to formatting the function parameters.\n\n\n select ML_PREDICT_ROW('projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/us-central1/publishers/google/models/gemini-1.0-pro:generateContent', json_build_object('contents', json_build_object('text', message))) from messages;\n\nThe returned JSON object now contains one entry in its `predictions` array for every row in the `messages` table.\n\nBecause the response is a JSON object, you can pull specific fields from it using the PostgreSQL arrow operator: \n\n select ML_PREDICT_ROW('projects/\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e/locations/us-central1/publishers/google/models/gemini-1.0-pro:generateContent', json_build_object('contents', json_build_object('text', message)))-\u003e'predictions'-\u003e0-\u003e'content' FROM messages;\n\nFor more example arguments to `ml_predict_row()`, see [Quickstart using the Vertex AI\nAPI](/vertex-ai/docs/generative-ai/start/quickstarts/api-quickstart)."]]