搜尋結果排名

選取文件版本:

瞭解如何使用 Vertex AI 排序模型 (例如 semantic-ranker-default-003) 排序及重新排序應用程式的搜尋結果。您可以使用 ai.rank() 函式,根據文件與查詢的關聯性為文件評分,並重新排序向量搜尋結果,改善查詢順序,藉此提升搜尋結果品質。

Vertex AI 排序 API 會接收文件清單,並根據文件與指定查詢 (搜尋字串) 的相關程度,對這些文件進行排序。使用 ai.rank() 函式時,系統會傳回分數,指出文件回答特定查詢的程度。

如要使用本頁的說明,您必須瞭解 AlloyDB Omni,並熟悉生成式 AI 概念。

AlloyDB Omni 會保留並建立 ai 結構定義。

事前準備

對搜尋結果進行排序前,請先完成下列操作:

  1. 設定 Vertex AI 模型的使用者存取權
  2. 確認已安裝最新版 google_ml_integration
    1. 如要檢查已安裝的版本,請執行下列指令:

      SELECT extversion FROM pg_extension WHERE extname = 'google_ml_integration';
      extversion
      ------------
      1.5.2
      (1 row)
    2. 如果尚未安裝擴充功能,或安裝的版本低於 1.5.2,請更新擴充功能。

      CREATE EXTENSION IF NOT EXISTS google_ml_integration;
      ALTER EXTENSION google_ml_integration UPDATE;

      如果您在執行上述指令時遇到問題,或執行上述指令後,擴充功能未更新至 1.5.2 版,請與 Google Cloud 支援團隊聯絡。

  3. 如要使用 AlloyDB AI 查詢引擎功能,請將 google_ml_integration.enable_ai_query_engine 旗標設為 on

    1. 使用文字編輯器,在 AlloyDB Omni 安裝的 postgresql.conf 設定檔中設定下列標記:
      google_ml_integration.enable_ai_query_engine = on
    2. 儲存 postgresql.conf 檔案後,請重新啟動 AlloyDB Omni 服務,讓變更生效。
      sudo systemctl restart alloydbomni18
  4. 啟用 Discovery Engine API

  5. 取得使用排名模型所需的角色

啟用 Discovery Engine API

<p>To use ranking models, you must enable the Discovery Engine API.<br>
    Replace <code><var>PROJECT_ID</var></code> with your
    Google Cloud project ID and <code><var>PROJECT_NUMBER</var></code>
    with your corresponding project number.</p>

<pre class="prettyprint lang-terminal">
    # Enable Discovery Engine API
    gcloud services enable discoveryengine.googleapis.com --project=<var>PROJECT_ID</var>
    gcloud projects add-iam-policy-binding <var>PROJECT_ID</var> \
    --member="serviceAccount:service-<var>PROJECT_NUMBER</var>@iam.gserviceaccount.com" \
    --role="roles/discoveryengine.viewer"
</pre>

<p>Model registration for ranking isn't required for Vertex AI models.
    You can use the Vertex AI model name as the
    <code>model_id</code>, which is shown in the following example.
</p>

<pre class="prettyprint lang-sh">
    SELECT index, 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'])
</pre>

<p>A common use case for the semantic ranker is to rerank the results returned
    by vector search for better query ordering. The following example shows how to
    use the semantic ranking model for this use case. The example retrieves an
    initial result set for the query <code>personal fitness
    equipment</code> using vector search. These results are then re-ranked to
    return the top five results.
</p>

<pre class="prettyprint lang-sh">
    WITH initial_ranking AS (
      SELECT id, description, ROW_NUMBER() OVER () AS ref_number
      FROM product
      ORDER BY
        embedding <=> google_ml.embedding(
          'gemini-embedding-001', 'personal fitness equipment')::vector
      LIMIT 10
    ), reranked_results AS (
      SELECT index, score
      FROM ai.rank(
          model_id => 'semantic-ranker-default-003',
          search_string => 'personal fitness equipment',
          documents => (SELECT ARRAY_AGG(description ORDER BY ref_number) FROM initial_ranking),
          top_n => 5)
    )
    SELECT id, description
    FROM initial_ranking, reranked_results
    WHERE initial_ranking.ref_number = reranked_results.index
    ORDER BY reranked_results.score DESC;
  </pre>

  <p>For a list of available models and use cases, see <a href="/generative-ai-app-builder/docs/ranking#models">Supported models</a>.</p>

整合 Vertex AI 並安裝擴充功能

詳情請參閱「設定 AlloyDB Omni 安裝作業,以便查詢雲端模型」。

必要的角色

如要取得使用 Discovery Engine 排序模型所需的權限,請要求管理員在 your project 上,授予 AlloyDB Omni 服務帳戶 Discovery Engine 檢視者 (roles/discoveryengine.viewer) 身分與存取權管理 (IAM) 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

您或許也能透過自訂角色或其他預先定義的角色,取得必要權限。

搜尋結果排名

以下 SQL 查詢顯示如何為搜尋結果排名:

SELECT
  ai.rank(
    model_id => 'MODEL_ID',
    search_string => 'SEARCH_STRING',
    documents => ARRAY['DOCUMENT_1', 'DOCUMENT_2', 'DOCUMENT_3']);

更改下列內容:

參數 說明
MODEL_ID 您定義的模型端點專屬 ID。
SEARCH_STRING 用來排序記錄的搜尋字串。
DOCUMENTS 要排序的專屬文字字串陣列。

如需支援的 Vertex AI 排序模型清單,請參閱「支援的模型」。

範例

如要使用 Vertex AI 排序模型為搜尋結果排序,請執行下列查詢:

SELECT index, score
FROM
  ai.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.']);

回覆內容是表格,顯示每份文件和根據與搜尋查詢相關性得出的分數。以下是回應範例:

 index | score
-------+------------
     2 |  0.33
     1 |  0.28
     3 |  0.16
(3 rows)

假設有一個 AlloyDB Omni 資料庫,其中包含轉換為嵌入的評論說明清單。下列程式碼範例說明如何使用排名模型,根據評論說明與查詢的語意相似度,擷取排名最高的產品名稱。

WITH initial_ranking AS (
    SELECT product_id, name, review, review_id, ROW_NUMBER() OVER () AS ref_number
    FROM user_reviews
    ORDER BY
      review_desc_embedding <=> google_ml.embedding(
        'gemini-embedding-001', 'good desserts')::vector
    LIMIT 10
  ), reranked_results AS (
    SELECT index, score
    FROM
      ai.rank(
        model_id => 'semantic-ranker-512',
        search_string => 'good desserts',
        documents => (SELECT ARRAY_AGG(review ORDER BY ref_number) FROM initial_ranking),
        top_n => 5)
  )
SELECT product_id, name
FROM initial_ranking, reranked_results
WHERE initial_ranking.ref_number = reranked_results.index
ORDER BY reranked_results.score DESC;

後續步驟