使用 RAG 檢查基準

在 Agent Search 中使用檢索增強生成 (RAG) 時,您可以檢查內容是否有所依據,以一組指定的參考文字 (稱為「事實」) 為基準,判斷某段文字 (稱為「候選答案」) 有多少內容是根據該事實生成。

Check Grounding API 會傳回 0 到 1 的整體支持分數,代表候選答案與指定事實的相符程度。回覆也包含引用內容,指出候選答案中支持各項主張的事實。

如要達到完美基準,候選答案中的每項主張都必須有一或多項指定事實佐證。換句話說,這項聲明完全符合事實。如果聲明只有部分符合,則不視為有依據。舉例來說,「Google 是由賴利·佩吉和謝爾蓋·布林於 1975 年創立」這項聲明只有部分正確,創辦人的姓名正確,但日期錯誤,因此整個聲明會被視為不符合事實。在這個版本的 Check Grounding API 中,一個句子會視為一項聲明。

您可以使用 Check Grounding API 檢查任何文字。可以是人工生成的簡介或機器生成的回覆。常見用途是根據一組特定事實,檢查 LLM 生成的回覆。Check Grounding API 的設計旨在快速運作,延遲時間不到 500 毫秒。這個速度可讓聊天機器人在每次推論期間呼叫檢查基準 API,而不會大幅降低速度。Check Grounding API 也能提供參考資料來佐證結果,讓使用者判斷生成回覆的哪些部分可靠。API 也會提供支援分數,指出回覆的整體準確度。設定引用門檻後,聊天機器人就能在推論時,篩除可能含有幻覺聲明的回覆。

本頁說明如何使用 Check Grounding API 檢查基準。

定義和解釋

使用 Check Grounding API 前,建議您先瞭解輸入和輸出內容,以及如何建構基準事實,以取得最佳結果。

輸入資料

Check Grounding API 要求在要求中提供下列輸入內容。

  • 候選答案:候選答案可以是您要檢查基準的任何文字。舉例來說,在代理搜尋的情境中,答案候選項目可能是回答查詢時生成的搜尋摘要。接著,API 會判斷摘要與輸入事實的關聯程度。答案候選詞的長度上限為 4096 個詞元,其中詞元定義為句子中的字詞或句號 (用於結束句子的標點符號)。舉例來說,「They wore off-the-rack clothes in 2024.」這句話有七個詞元,包括六個字詞和一個句號。

  • 事實:一組文字片段,用於建立基準。每個文字區隔都可以提供一組中繼資料屬性 (鍵/值組合)。例如,「Author」和「Title」是常見的屬性鍵。

    這項服務最多支援 200 個事實,每個事實最多 1 萬個字元。

    Google 建議不要提供包含所有資訊的大型事實。建議您將大型事實拆解成較小的事實,並為這些事實提供適當的屬性,這樣才能取得更理想的結果。舉例來說,您可以依標題、作者或網址拆分大型事實,並在屬性中提供這項資訊。

  • 引用門檻:介於 0 到 1 之間的浮點值,用於控制支援候選答案的引用可信度。門檻越高,可信度要求越嚴格。因此,較高的門檻會產生較少但更強的引用。

輸出資料

Check Grounding API 會針對候選答案傳回下列資訊:

  • 支援分數:支援分數介於 0 到 1 之間,表示候選答案在提供的基準事實中的依據程度。這項指標會大致估算候選答案中,有多少比例的聲明與一或多項指定事實相符。

  • 引用片段:引用片段是指輸入事實中,支持候選答案的部分。

  • 聲明和引用:聲明和引用會將候選答案的聲明 (通常是句子) 連結至一或多個佐證聲明的引用區塊。

    聲明會以開始和結束位置來界定。這些是 UTF-8 編碼聲明字串的位元組位置。請注意,這不是以字元為單位,因此在使用者介面中算繪時,請務必考量到某些字元會占用多個位元組。舉例來說,如果聲明文字包含非 ASCII 字元,則以字元 (取決於程式設計語言) 和位元組 (與程式設計語言無關) 測量時,起始和結束位置會有所不同。

  • 聲明層級支援分數:啟用聲明層級分數時,每項聲明都會傳回 0 到 1 之間的分數,表示聲明在提供的資訊集中有多可靠。詳情請參閱「取得候選答案的論述層級分數」。

  • 需要查證:系統會針對每項聲明,傳回「需要查證」布林值。如果傳回 False,表示系統認為該聲明不需要引用基準,因此不會傳回引用內容。舉例來說,「以下是我找到的內容」這句話本身並非事實,因此不需要進行基準檢查。

    如果 grounding-check-required 傳回 true,表示系統已執行基準檢查,並傳回支援分數和引用內容 (如有)。

取得候選答案的支持分數

如要瞭解候選答案有多少內容是根據一組事實生成,請按照下列步驟操作:

  1. 準備事實。如需更多資訊和範例,請參閱「定義和說明」。

  2. 使用下列程式碼呼叫 check 方法:

REST

curl -X POST \
-H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json" \
-H "X-Goog-User-Project: PROJECT_ID" \
"https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/groundingConfigs/default_grounding_config:check" \
-d '{
  "answerCandidate": "CANDIDATE",
  "facts": [
  {
    "factText": "TEXT_0",
    "attributes": {"ATTRIBUTE_A": "VALUE_A0","ATTRIBUTE_B": "VALUE_B0"}
  },
  {
    "factText": "TEXT_1",
    "attributes": {"ATTRIBUTE_A": "VALUE_A1","ATTRIBUTE_B": "VALUE_B1"}
  },
  {
    "factText": "TEXT_2",
    "attributes": {"ATTRIBUTE_A": "VALUE_A2","ATTRIBUTE_B": "VALUE_B2"}
  }
  ],
  "groundingSpec": {
    "citationThreshold": "CITATION_THRESHOLD"
  }
}'

更改下列內容:

  • PROJECT_ID: Google Cloud 專案的專案編號或 ID。

  • CANDIDATE:要取得支持分數的候選答案字串,例如 Titanic was directed by James Cameron. It was released in 1997.。候選答案的長度上限為 4096 個詞元,詞元定義為句子中的字詞或句號 (用於結束句子的標點符號)。舉例來說,「They wore off-the-rack clothes in 2024.」這句話包含六個字和一個句號,因此長度為七個詞元。

  • TEXT:用於建立基準的文字區隔,例如 Titanic is a 1997 American epic... Academy Awards. (請參閱「事實範例」中的全文)。

  • ATTRIBUTE:與事實相關聯的中繼資料屬性名稱,例如 author 或 title。這是使用者定義的標籤,可為事實文字新增更多資訊。舉例來說,如果事實文字 Toronto is the capital of Ontario 具有 author 屬性,且其值為 Wikipedia,則下列聲明會視為以事實為依據:

    • Wikipedia cites that Toronto is the capital of Ontario
    • Toronto is the capital of Ontario

      不過,Government of Ontario claims that Toronto is the capital of Ontario 的說法不如前兩個說法有根據。

  • VALUE:屬性的值,例如 Simple Wikipedia 或 Titanic (1997 film)。

  • CITATION_THRESHOLD:介於 0 到 1 之間的浮點值,決定是否必須為候選答案中的聲明引用事實。門檻越高,引用的數量越少,但強度越高;門檻越低,引用的數量越多,但強度越低。如未設定,預設門檻值為 0.6。

Python

詳情請參閱 Agent Search Python API 參考文件。

如要向 Agent Search 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

from google.cloud import discoveryengine_v1 as discoveryengine

# TODO(developer): Uncomment these variables before running the sample.
# project_id = "YOUR_PROJECT_ID"

client = discoveryengine.GroundedGenerationServiceClient()

# The full resource name of the grounding config.
# Format: projects/{project_id}/locations/{location}/groundingConfigs/default_grounding_config
grounding_config = client.grounding_config_path(
    project=project_id,
    location="global",
    grounding_config="default_grounding_config",
)

request = discoveryengine.CheckGroundingRequest(
    grounding_config=grounding_config,
    answer_candidate="Titanic was directed by James Cameron. It was released in 1997.",
    facts=[
        discoveryengine.GroundingFact(
            fact_text=(
                "Titanic is a 1997 American epic romantic disaster movie. It was directed, written,"
                " and co-produced by James Cameron. The movie is about the 1912 sinking of the"
                " RMS Titanic. It stars Kate Winslet and Leonardo DiCaprio. The movie was released"
                " on December 19, 1997. It received positive critical reviews. The movie won 11 Academy"
                " Awards, and was nominated for fourteen total Academy Awards."
            ),
            attributes={"author": "Simple Wikipedia"},
        ),
        discoveryengine.GroundingFact(
            fact_text=(
                'James Cameron\'s "Titanic" is an epic, action-packed romance'
                "set against the ill-fated maiden voyage of the R.M.S. Titanic;"
                "the pride and joy of the White Star Line and, at the time,"
                "the largest moving object ever built. "
                'She was the most luxurious liner of her era -- the "ship of dreams" -- '
                "which ultimately carried over 1,500 people to their death in the "
                "ice cold waters of the North Atlantic in the early hours of April 15, 1912."
            ),
            attributes={"author": "Simple Wikipedia"},
        ),
    ],
    grounding_spec=discoveryengine.CheckGroundingSpec(citation_threshold=0.6),
)

response = client.check_grounding(request=request)

# Handle the response
print(response)

事實資訊範例

以下是事實及其屬性的幾個例子。這些範例可協助您瞭解建立基準的回覆和 curl 指令的格式。

  • 事實 0

    • 文字:"Titanic is a 1997 American epic romantic disaster movie. It was directed, written, and co-produced by James Cameron. The movie is about the 1912 sinking of the RMS Titanic. It stars Kate Winslet and Leonardo DiCaprio. The movie was released on December 19, 1997. It received positive critical reviews. The movie won 11 Academy Awards, and was nominated for fourteen total Academy Awards."

    • 屬性:{"Author": "Simple Wikipedia"}

  • 事實 1

    • 文字:"James Cameron's "Titanic" is an epic, action-packed romance set against the ill-fated maiden voyage of the R.M.S. Titanic; the pride and joy of the White Star Line and, at the time, the largest moving object ever built. She was the most luxurious liner of her era -- the "ship of dreams" -- which ultimately carried over 1,500 people to their death in the ice cold waters of the North Atlantic in the early hours of April 15, 1912."

    • 屬性:{"Author": "Rotten Tomatoes"}

要求範例

準備好事實後,您可以傳送下列要求,並將 CANDIDATE 欄位替換為要檢查基準的不同字串。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/groundingConfigs/default_grounding_config:check" \
    -d '{
    "answerCandidate": "CANDIDATE",
    "facts": [
     {
      "factText": "Titanic is a 1997 American epic romantic disaster movie. It was directed, written, and co-produced by James Cameron. The movie is about the 1912 sinking of the RMS Titanic. It stars Kate Winslet and Leonardo DiCaprio. The movie was released on December 19, 1997. It received positive critical reviews. The movie won 11 Academy Awards, and was nominated for fourteen total Academy Awards.",
      "attributes": {"author":"Simple Wikipedia"}
     },
     {
      "factText": "James Cameron's \"Titanic\" is an epic, action-packed romance set against the ill-fated maiden voyage of the R.M.S. Titanic; the pride and joy of the White Star Line and, at the time, the largest moving object ever built. She was the most luxurious liner of her era -- the \"ship of dreams\" -- which ultimately carried over 1,500 people to their death in the ice cold waters of the North Atlantic in the early hours of April 15, 1912.",
      "attributes": {"author":"Simple Wikipedia"}
     }
    ],
    "groundingSpec": {
      "citationThreshold": "0.6"
    }
    }'

候選答案和基準回覆範例

下表列出根據範例事實傳送範例要求時,不同候選答案和回覆的範例。

候選答案 檢查基準化回覆
Here is what I found. Titanic was directed by James Cameron. 支持分數:0.99

引用語塊:
  1. [From FACT 0]....Titanic is a 1997 American epic romantic disaster movie. It was directed, written, and co-produced by James Cameron. It stars Kate Winslet and Leonardo DiCaprio. The movie was released on December 19, 1997. It received positive critical reviews. The movie won 11 Academy Awards, and was nominated for fourteen total Academy Awards.....
聲明和引用:
  • 0. Here is what I found.
    需要進行基準檢查:false
  • 1. Titanic was directed by James Cameron.
    引用來源:[0]
    需要查證:true
Titanic was directed by James Cameron. It was released in 1997. 支持分數:0.99

引用語塊:
  1. [From FACT 0]....Titanic is a 1997 American epic romantic disaster movie. It was directed, written, and co-produced by James Cameron. It stars Kate Winslet and Leonardo DiCaprio. The movie was released on December 19, 1997. It received positive critical reviews. The movie won 11 Academy Awards, and was nominated for fourteen total Academy Awards.....
  2. [From FACT 1]...Titanic is an epic, action-packed romance set against the ill-fated maiden voyage of the R.M.S. Titanic; the pride and joy of the White Star Line and, at the time, the largest moving object ever built. She was the most luxurious liner of her era which ultimately carried over 1,500 people to their death in the ice cold waters of the North Atlantic in the early hours of April 15, 1912....
聲明和引用:
  • 0. Titanic was directed by James Cameron.
    引用來源:[0]
    需要查證:true
  • 1. It was released in 1997.
    引用來源:[1]
    需要查證:true
Titanic was directed by James Cameron. It was based on the sinking of the RMS Titanic that led to the death of 1500 people. 支持分數:0.95

引用語塊:
  1. [From FACT 0]....Titanic is a 1997 American epic romantic disaster movie. It was directed, written, and co-produced by James Cameron. It stars Kate Winslet and Leonardo DiCaprio. The movie was released on December 19, 1997. It received positive critical reviews. The movie won 11 Academy Awards, and was nominated for fourteen total Academy Awards.....
  2. [From FACT 1]...Titanic is an epic, action-packed romance set against the ill-fated maiden voyage of the R.M.S. Titanic; the pride and joy of the White Star Line and, at the time, the largest moving object ever built. She was the most luxurious liner of her era which ultimately carried over 1,500 people to their death in the ice cold waters of the North Atlantic in the early hours of April 15, 1912....
聲明和引用:
  • 0. Cited chunks: Titanic was directed by James Cameron.
    引用來源:[0]
    需要查證:true
  • 1. Cited chunks: It was based on the sinking of the RMS Titanic that led to the death of 1500 people.
    引用來源:[1]
    需要查證:true
Titanic was directed by James Cameron. It starred Brad Pitt and Kate Winslet 支持分數:0.54

引用語塊:
  1. [From FACT 0]....Titanic is a 1997 American epic romantic disaster movie. It was directed, written, and co-produced by James Cameron. It stars Kate Winslet and Leonardo DiCaprio. The movie was released on December 19, 1997. It received positive critical reviews. The movie won 11 Academy Awards, and was nominated for fourteen total Academy Awards.....
  2. [From FACT 1]...Titanic is an epic, action-packed romance set against the ill-fated maiden voyage of the R.M.S. Titanic; the pride and joy of the White Star Line and, at the time, the largest moving object ever built. She was the most luxurious liner of her era which ultimately carried over 1,500 people to their death in the ice cold waters of the North Atlantic in the early hours of April 15, 1912....
聲明和引用:
  • 0. Titanic was directed by James Cameron.
    引用來源:[0]
    需要查證:true
  • 1. It starred Brad Pitt and Kate Winslet
    引用來源:[]
    需要查證:true
注意:雖然凱特溫絲蕾確實有演出這部電影,但由於 "It starred Brad Pitt and Kate Winslet" 這項聲明並非完全屬實,因此不會獲得任何引用。

取得候選答案的聲明層級分數

除了答案層級的支援分數,您也可以取得候選答案中每個聲明的聲明層級支援分數。

如要取得聲明層級分數,請按照下列步驟操作:

  1. 準備事實。如需更多資訊和範例,請參閱「定義和說明」。

  2. 使用下列 curl 指令呼叫 check 方法:

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/groundingConfigs/default_grounding_config:check" \
    -d '{
    "answerCandidate": "CANDIDATE",
    "facts": [
     {
      "factText": "TEXT_0",
      "attributes": {"ATTRIBUTE_A": "VALUE_A0","ATTRIBUTE_B": "VALUE_B0"}
     },
     {
      "factText": "TEXT_1",
      "attributes": {"ATTRIBUTE_A": "VALUE_A1","ATTRIBUTE_B": "VALUE_B1"}
     },
     {
      "factText": "TEXT_2",
      "attributes": {"ATTRIBUTE_A": "VALUE_A2","ATTRIBUTE_B": "VALUE_B2"}
     },
    ],
    "groundingSpec": {
      "citationThreshold": "CITATION_THRESHOLD",
      "enableClaimLevelScore": "ENABLE_CLAIM_LEVEL_SCORE",
    }
    }'
    

    更改下列內容:

    • PROJECT_ID:Google Cloud 專案的專案編號或 ID。

    • CANDIDATE:要取得支持分數的候選答案字串,例如 Titanic was directed by James Cameron. It was released in 1997.。候選答案的長度上限為 4096 個詞元,詞元定義為句子中的字詞或句號 (用於結束句子的標點符號)。舉例來說,「They wore off-the-rack clothes in 2024.」這個句子包含六個字詞和一個句號,因此長度為七個詞元。

    • TEXT:用於基準建立的文字區隔,例如 Titanic is a 1997 American epic... Academy Awards. (請參閱「事實範例」中的全文)。

    • ATTRIBUTE:與事實相關聯的中繼資料屬性名稱,例如 author 或 title。這是使用者定義的標籤,可為事實文字新增更多資訊。舉例來說,如果事實文字 Toronto is the capital of Ontario 具有 author 屬性,且其值為 Wikipedia,則下列聲明有充分依據:

      • Wikipedia cites that Toronto is the capital of Ontario
      • Toronto is the capital of Ontario

      不過,Government of Ontario claims that Toronto is the capital of Ontario 的說法並非如此。

    • VALUE:屬性的值,例如 Simple Wikipedia 或 Titanic (1997 film)。

    • CITATION_THRESHOLD:介於 0 到 1 之間的浮點值,決定是否必須為候選答案中的聲明引用事實。門檻越高,引文數量越少,但支持聲明的力道越強;門檻越低,引文數量越多,但支持聲明的力道越弱。如未設定,預設門檻值為 0.6。

    • ENABLE_CLAIM_LEVEL_SCORE:布林值。將這個欄位設為 true,即可啟用聲明層級分數功能。如要關閉這項功能,請移除這個欄位,或將這個欄位設為 false。

要求範例

使用上一節的範例事實,您可以傳送下列要求。將 CANDIDATE 欄位替換為不同的字串,檢查各項聲明的基準。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1/projects/PROJECT_ID/locations/global/groundingConfigs/default_grounding_config:check" \
    -d '{
    "answerCandidate": "CANDIDATE",
    "facts": [
     {
      "factText": "Titanic is a 1997 American epic romantic disaster movie. It was directed, written, and co-produced by James Cameron. The movie is about the 1912 sinking of the RMS Titanic. It stars Kate Winslet and Leonardo DiCaprio. The movie was released on December 19, 1997. It received positive critical reviews. The movie won 11 Academy Awards, and was nominated for fourteen total Academy Awards.",
      "attributes": {"author":"Simple Wikipedia"}
     },
     {
      "factText": "James Cameron's \"Titanic\" is an epic, action-packed romance set against the ill-fated maiden voyage of the R.M.S. Titanic; the pride and joy of the White Star Line and, at the time, the largest moving object ever built. She was the most luxurious liner of her era -- the \"ship of dreams\" -- which ultimately carried over 1,500 people to their death in the ice cold waters of the North Atlantic in the early hours of April 15, 1912.",
      "attributes": {"author":"Simple Wikipedia"}
     }
    ],
    "groundingSpec": {
      "citationThreshold": "0.6",
      "enableClaimLevelScore": true,
    }
    }'

含有聲明層級分數的回覆範例

下表顯示根據範例事實,傳送範例要求時,候選答案和回應的範例。

候選答案 檢查基準化回覆
Here is what I found. Titanic was directed by James Cameron. It starred Kate Winslet and Leonardo DiCaprio. 支持分數:0.99

引用語塊:
  1. [From FACT 0]....Titanic is a 1997 American epic romantic disaster movie. It was directed, written, and co-produced by James Cameron. It stars Kate Winslet and Leonardo DiCaprio. The movie was released on December 19, 1997. It received positive critical reviews. The movie won 11 Academy Awards, and was nominated for fourteen total Academy Awards.....
聲明和引用:
  • 0. Here is what I found.
    需要進行基準檢查:false
  • 1. Titanic was directed by James Cameron.
    引用來源:[0]
    需要查證:true
    分數:0.99
  • 2. It starred Kate Winslet and Leonardo DiCaprio.
    引用來源:[0]
    需要查證:true
    分數:0.99