Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Halaman ini menjelaskan cara memberi peringkat hasil penelusuran untuk
aplikasi menggunakan endpoint model peringkat Vertex AI.
Vertex AI Ranking API mengambil daftar dokumen dan memberi peringkat
dokumen tersebut berdasarkan seberapa relevan dokumen tersebut dengan kueri tertentu (string
penelusuran). Saat Anda menggunakan fungsi ai.rank(), fungsi ini akan menampilkan skor seberapa baik dokumen menjawab kueri tertentu.
Untuk menggunakan petunjuk di halaman ini, Anda harus memahami AlloyDB untuk PostgreSQL dan memahami konsep AI generatif.
AlloyDB mencadangkan, dan membuat, skema ai.
Sebelum memulai
Sebelum Anda memberi peringkat hasil penelusuran, lakukan hal berikut:
Pada langkah Confirm project, klik Next untuk mengonfirmasi nama project yang akan Anda ubah.
Pada langkah Enable APIs, klik Enable untuk mengaktifkan Discovery Engine API. Jika sudah mengaktifkan API ini, Anda tidak akan melihatnya tercantum di sini.
gcloud
Untuk menggunakan model peringkat, Anda harus mengaktifkan Discovery Engine API.
Ganti PROJECT_ID dengan
Google Cloud project ID dan PROJECT_NUMBER
dengan nomor project yang sesuai.
Pendaftaran model untuk peringkat tidak diperlukan untuk model Vertex AI.
Anda dapat menggunakan nama model Vertex AI sebagai
model_id, yang ditunjukkan dalam contoh berikut.
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'])
Kasus penggunaan umum untuk perankingan semantik adalah untuk memberi peringkat ulang hasil yang ditampilkan
oleh penelusuran vektor untuk pengurutan kueri yang lebih baik. Contoh berikut menunjukkan cara menggunakan model peringkat semantik untuk kasus penggunaan ini. Contoh ini mengambil
kumpulan hasil awal untuk kueri personal fitness
equipment menggunakan penelusuran vektor. Hasil ini kemudian diberi peringkat ulang untuk
menampilkan lima hasil teratas.
Jika Anda mengalami masalah saat menjalankan perintah sebelumnya, atau jika ekstensi tidak diupdate ke versi 1.4.3 setelah Anda menjalankan perintah sebelumnya, hubungi dukungan AlloyDB.
Setelah Anda memastikan bahwa versi sudah yang terbaru, instal fungsi
pratinjau dengan menjalankan prosedur upgrade_to_preview_version:
Untuk mendapatkan izin yang diperlukan untuk menggunakan model peringkat dari
Discovery Engine, minta administrator untuk memberi Anda peran
Discovery Engine Viewer (roles/discoveryengine.viewer) Identity and Access Management (IAM)
di your project. Untuk mengetahui informasi selengkapnya tentang cara memberikan peran, lihat Mengelola akses ke project, folder, dan organisasi.
Anda mungkin juga bisa mendapatkan izin yang diperlukan melalui peran khusus
atau peran bawaan lainnya.
Memberi peringkat hasil penelusuran Anda
Kueri SQL berikut menunjukkan cara memberi peringkat pada hasil penelusuran Anda :
String penelusuran yang digunakan untuk memberi peringkat pada kumpulan data.
DOCUMENTS
String unik yang mengidentifikasi data.
Untuk mengetahui daftar model ranking Vertex AI yang didukung, lihat Model yang didukung.
Contoh
Untuk memberi peringkat hasil penelusuran menggunakan model peringkat Vertex AI, jalankan kueri berikut:
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.']);
Responsnya adalah tabel yang menampilkan setiap dokumen dan skor berdasarkan relevansinya dengan kueri penelusuran. Berikut adalah contoh respons:
Pertimbangkan contoh database AlloyDB dengan daftar deskripsi ulasan yang dikonversi menjadi embedding. Cuplikan kode contoh berikut menunjukkan cara menggunakan model peringkat untuk mengambil nama produk dengan peringkat teratas berdasarkan kemiripan semantik deskripsi ulasannya dengan kueri.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-09-05 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)."]]