Vertex AI 模型不需要注册排名模型。
您可以使用 Vertex AI 模型名称作为 model_id,如以下示例所示。
SELECTindex,score
FROM
ai.rank(model_id=>'semantic-ranker-default-003',
search_string=>'Affordable family-friendly vacation spots in Southeast Asia?',
documents=>
ARRAY['Luxury resorts in South Korea',
'Family vacation packages for Vietnam: Ha Long Bay and Hoi An',
'Budget-friendly beaches in Thailand perfect for families',
'A backpacker guide to solo travel in India'])
语义排名工具的常见用例是对向量搜索返回的结果进行重新排名,以实现更好的查询排序。以下示例展示了如何针对此用例使用语义排名模型。此示例会使用向量搜索检索查询 personal fitness
equipment 的初始结果集。之后,系统会对这些结果重新排名,返回前 5 个结果。
SELECTindex,scoreFROMai.rank(model_id=>'semantic-ranker-default-003',search_string=>'AlloyDB is a PostgreSQL compatible AI database that is ready for production.',documents=>
ARRAY['Alloys are made from combination of metals','The best enterprise-ready PostgreSQL database.','You can feel the heat in Alloy apartments.']);
[[["易于理解","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-05。"],[],[],null,["# Rank search results\n\n| **Preview**\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| 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).\n| **Note:** This experimental launch is a Pre-GA offering.\n\nThis page describes how to rank your search results for\napplications using the Vertex AI ranking model endpoint.\n\nThe Vertex AI ranking API takes a list of documents and ranks\nthose documents based on how relevant the documents are to a given query (a\nsearch string). When you use the `ai.rank()` function, it returns scores for how\nwell a document answers a given query.\n\nTo use instructions on this page, you must have an understanding of\nAlloyDB for PostgreSQL and be familiar with generative AI concepts.\n\nAlloyDB reserves, and creates, the `ai` schema.\n| **Note:** Vertex AI model support is governed by Vertex AI model versioning and lifecycle guidelines. For more information about stable versions, see [Model versions and lifecycle](/vertex-ai/docs/model-registry/model-versions).\n\nBefore you begin\n----------------\n\nBefore you rank search results, do the following:\n\n- [Verify that the `google_ml_integration` extension is installed](/alloydb/docs/ai/configure-vertex-ai#verify-installed-extension).\n- [Verify that the `google_ml_integration.enable_model_support` flag is set to `on`](/alloydb/docs/instance-configure-database-flags).\n- [Integrate with Vertex AI](/alloydb/docs/ai/configure-vertex-ai).\n- [Enable the Discovery Engine API](#enable-discovery-engine-api).\n- [Get the required roles to use ranking models](#required-roles).\n\n### Enable the Discovery Engine API\n\n### Console\n\n1. [Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=discoveryengine.googleapis.com)\n2. In the **Confirm project** step, click **Next** to confirm the name of the project you are going to make changes to.\n3. In the **Enable APIs** step, click **Enable** to enable the Discovery Engine API. If you already enabled this API, you won't see it listed here.\n\n### gcloud\n\nTo use ranking models, you must enable the Discovery Engine API. \n\nReplace \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e with your\nGoogle Cloud project ID and \u003cvar translate=\"no\"\u003ePROJECT_NUMBER\u003c/var\u003e\nwith your corresponding project number. \n\n```console\n # Enable Discovery Engine API\n gcloud services enable discoveryengine.googleapis.com --project=\u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e\n gcloud projects add-iam-policy-binding \u003cvar translate=\"no\"\u003ePROJECT_ID\u003c/var\u003e \\\n --member=\"serviceAccount:service-\u003cvar translate=\"no\"\u003ePROJECT_NUMBER\u003c/var\u003e@gcp-sa-alloydb.iam.gserviceaccount.com\" \\\n --role=\"roles/discoveryengine.viewer\"\n```\n\nModel registration for ranking isn't required for Vertex AI models.\nYou can use the Vertex AI model name as the\n`model_id`, which is shown in the following example. \n\n```bash\n SELECT index, score\n FROM\n ai.rank(\n model_id =\u003e 'semantic-ranker-default-003',\n search_string =\u003e 'Affordable family-friendly vacation spots in Southeast Asia?',\n documents =\u003e\n ARRAY[\n 'Luxury resorts in South Korea',\n 'Family vacation packages for Vietnam: Ha Long Bay and Hoi An',\n 'Budget-friendly beaches in Thailand perfect for families',\n 'A backpacker guide to solo travel in India'])\n```\n\nA common use case for the semantic ranker is to rerank the results returned\nby vector search for better query ordering. The following example shows how to\nuse the semantic ranking model for this use case. The example retrieves an\ninitial result set for the query `personal fitness\nequipment` using vector search. These results are then re-ranked to\nreturn the top five results. \n\n```bash\n WITH initial_ranking AS (\n SELECT id, description, ROW_NUMBER() OVER () AS ref_number\n FROM product\n ORDER BY\n embedding \u003c=\u003e google_ml.embedding(\n 'gemini-embedding-001', 'personal fitness equipment')::vector\n LIMIT 10\n ), reranked_results AS (\n SELECT index, score\n FROM ai.rank(\n model_id =\u003e 'semantic-ranker-default-003',\n search_string =\u003e 'personal fitness equipment',\n documents =\u003e (SELECT ARRAY_AGG(description ORDER BY ref_number) FROM initial_ranking),\n top_n =\u003e 5)\n )\n SELECT id, description\n FROM initial_ranking, reranked_results\n WHERE initial_ranking.ref_number = reranked_results.index\n ORDER BY reranked_results.score DESC;\n \n```\n\n\u003cbr /\u003e\n\nFor a list of available models and use cases, see [Supported models](/generative-ai-app-builder/docs/ranking#models).\n\n\u003cbr /\u003e\n\n### Integrate with Vertex AI and install the extension\n\n1. [Integrate with Vertex AI](/alloydb/docs/ai/configure-vertex-ai).\n2. Ensure that the latest version of `google_ml_integration` is installed.\n 1. To check the installed version, run the following\n command:\n\n ```bash\n SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';\n extversion\n ------------\n 1.4.3\n (1 row)\n \n ```\n 2. If the extension isn't installed or if the installed version is\n earlier than 1.4.3, update the extension by running the following commands:\n\n ```bash\n CREATE EXTENSION IF NOT EXISTS google_ml_integration;\n ALTER EXTENSION google_ml_integration UPDATE;\n \n ```\n\n If you experience issues when you run the preceding commands, or if the\n extension isn't updated to version 1.4.3 after you run the preceding\n commands, contact AlloyDB support.\n 3. After you ensure that the version is current, install the preview\n functionality by running the `upgrade_to_preview_version` procedure:\n\n ```bash\n CALL google_ml.upgrade_to_preview_version();\n SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';\n extversion\n ------------\n 1.4.4\n (1 row)\n \n ```\n | Note: The expected version is 1.4.4 or later.\n\n### Required roles\n\nTo get the permissions that you need to use ranking models from\nDiscovery Engine, ask your administrator to grant you the\nDiscovery Engine Viewer (`roles/discoveryengine.viewer`) Identity and Access Management (IAM)\nrole on `your project`. For more information about granting roles, see\n[Manage access to projects, folders, and organizations](/iam/docs/granting-changing-revoking-access).\n\nYou might also be able to get the required permissions through [custom roles](/iam/docs/creating-custom-roles)\nor other [predefined roles](/iam/docs/roles-permissions).\n\nRank your search results\n------------------------\n\nThe following SQL query shows how to rank your search results :\n**Note:** For API limits related to Vertex AI ranking models, see [Rank (or rerank) a set of records according to a query](/generative-ai-app-builder/docs/ranking#rank_or_rerank_a_set_of_records_according_to_a_query). \n\n SELECT\n ai.rank(\n model_id =\u003e '\u003cvar translate=\"no\"\u003eMODEL_ID\u003c/var\u003e',\n search_string =\u003e '\u003cvar translate=\"no\"\u003eSEARCH_STRING\u003c/var\u003e',\n documents =\u003e ARRAY['\u003cvar translate=\"no\"\u003eDOCUMENT_1\u003c/var\u003e', '\u003cvar translate=\"no\"\u003eDOCUMENT_2\u003c/var\u003e', '\u003cvar translate=\"no\"\u003eDOCUMENT_3\u003c/var\u003e']);\n\nReplace the following:\n\nFor a list of the supported Vertex AI ranking models, see [Supported models](/generative-ai-app-builder/docs/ranking#models).\n\nExamples\n--------\n\nTo rank search results using a Vertex AI ranking model, run the following query: \n\n SELECT index, score\n FROM\n ai.rank(\n model_id =\u003e 'semantic-ranker-default-003',\n search_string =\u003e 'AlloyDB is a PostgreSQL compatible AI database that is ready for production.',\n documents =\u003e\n ARRAY[\n 'Alloys are made from combination of metals',\n 'The best enterprise-ready PostgreSQL database.',\n 'You can feel the heat in Alloy apartments.']);\n\nThe response is a table that shows each document and the score based on\nrelevance to the search query. The following is the sample response: \n\n index | score\n -------+------------\n 2 | 0.33\n 1 | 0.28\n 3 | 0.16\n (3 rows)\n\nConsider an example AlloyDB database with a list of review\ndescriptions that are converted to embeddings. The following sample code\nsnippet shows how to use the ranking model to retrieve the name of the\ntop-ranked products based on their review descriptions' semantic similarity to\na query. \n\n WITH initial_ranking AS (\n SELECT product_id, name, review, review_id, ROW_NUMBER() OVER () AS ref_number\n FROM user_reviews\n ORDER BY\n review_desc_embedding \u003c=\u003e google_ml.embedding(\n 'gemini-embedding-001', 'good desserts')::vector\n LIMIT 10\n ), reranked_results AS (\n SELECT index, score\n FROM\n ai.rank(\n model_id =\u003e 'semantic-ranker-512',\n search_string =\u003e 'good desserts',\n documents =\u003e (SELECT ARRAY_AGG(review ORDER BY ref_number) FROM initial_ranking),\n top_n =\u003e 5)\n )\n SELECT product_id, name\n FROM initial_ranking, reranked_results\n WHERE initial_ranking.ref_number = reranked_results.index\n ORDER BY reranked_results.score DESC;\n\n| **Note:** You get the 'semantic-ranker-default-003' model after you enable the Discovery Engine API and grant required permissions. For more information, see [Before you begin](#before-you-begin).\n\nWhat's next\n-----------\n\n- [Register a model endpoint with model endpoint management](/alloydb/docs/ai/register-model-endpoint).\n\n- [Query using AI powered SQL operators](/alloydb/docs/ai/evaluate-semantic-queries-ai-operators)."]]