Mantenha tudo organizado com as coleções
Salve e categorize o conteúdo com base nas suas preferências.
Nesta página, descrevemos como classificar os resultados da pesquisa para
aplicativos usando o endpoint do modelo de classificação da Vertex AI.
A API Ranking da Vertex AI recebe uma lista de documentos e os classifica com base na relevância deles para uma determinada consulta (uma string de pesquisa). Quando você usa a função ai.rank(), ela retorna pontuações de acordo com a qualidade da resposta de um documento a uma determinada consulta.
Para usar as instruções nesta página, você precisa entender o AlloyDB para PostgreSQL e conhecer os conceitos de IA generativa.
O AlloyDB reserva e cria o esquema ai.
Antes de começar
Antes de classificar os resultados da pesquisa, faça o seguinte:
Na etapa Confirmar projeto, clique em Próxima para confirmar o nome do projeto que você vai mudar.
Na etapa Ativar APIs, clique em Ativar para ativar a API Discovery Engine. Se você já tiver ativado essa API, ela não vai aparecer aqui.
gcloud
Para usar modelos de classificação, é necessário ativar a API Discovery Engine.
Substitua PROJECT_ID pelo
ID do projeto Google Cloud e PROJECT_NUMBER
pelo número correspondente.
O registro de modelos para classificação não é necessário para modelos da Vertex AI.
É possível usar o nome do modelo da Vertex AI como o
model_id, conforme mostrado no exemplo a seguir.
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'])
Um caso de uso comum para o classificador semântico é reclassificar os resultados retornados pela pesquisa de vetor para melhorar a ordenação das consultas. O exemplo a seguir mostra como usar o modelo de classificação semântica para esse caso de uso. O exemplo recupera um conjunto de resultados inicial para a consulta personal fitness
equipment usando a pesquisa de vetores. Esses resultados são reclassificados para retornar os cinco principais.
Se você tiver problemas ao executar os comandos anteriores ou se a extensão não for atualizada para a versão 1.4.3 depois de executar os comandos anteriores, entre em contato com o suporte do AlloyDB.
Depois de verificar se a versão está atualizada, instale a funcionalidade de prévia
executando o procedimento upgrade_to_preview_version:
Para receber as permissões necessárias para usar modelos de classificação do
Discovery Engine, peça ao administrador para conceder a você o papel
Leitor do Discovery Engine (roles/discoveryengine.viewer) do Identity and Access Management (IAM)
em your project. Para mais informações sobre a concessão de papéis, consulte
Gerenciar o acesso a projetos, pastas e organizações.
Um ID exclusivo para o endpoint do modelo que você define.
SEARCH_STRING
Uma string de pesquisa em relação à qual os registros são classificados.
DOCUMENTS
Uma string exclusiva que identifica o registro.
Para conferir uma lista dos modelos de classificação da Vertex AI compatíveis, consulte Modelos compatíveis.
Exemplos
Para classificar os resultados da pesquisa usando um modelo de classificação da Vertex AI, execute a seguinte consulta:
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.']);
A resposta é uma tabela que mostra cada documento e a pontuação com base na relevância para a consulta de pesquisa. Confira a seguir um exemplo de resposta:
Considere um exemplo de banco de dados do AlloyDB com uma lista de descrições de avaliações convertidas em embeddings. O snippet de código de exemplo a seguir mostra como usar o modelo de classificação para recuperar o nome dos produtos mais bem classificados com base na semelhança semântica das descrições das avaliações com uma consulta.
[[["Fácil de entender","easyToUnderstand","thumb-up"],["Meu problema foi resolvido","solvedMyProblem","thumb-up"],["Outro","otherUp","thumb-up"]],[["Difícil de entender","hardToUnderstand","thumb-down"],["Informações incorretas ou exemplo de código","incorrectInformationOrSampleCode","thumb-down"],["Não contém as informações/amostras de que eu preciso","missingTheInformationSamplesINeed","thumb-down"],["Problema na tradução","translationIssue","thumb-down"],["Outro","otherDown","thumb-down"]],["Última atualização 2025-09-03 UTC."],[],[],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)."]]