使用 RAG 檢查接地

在 AI 應用程式的檢索增強生成 (RAG) 體驗中,您可以檢查基準,判斷一段文字 (稱為「候選答案」) 在一組參考文字 (稱為「事實」) 中的基準程度。

Check Grounding API 會傳回 0 到 1 的整體支援分數,指出候選答案與指定事實的相符程度。回覆也會引用事實,佐證候選答案中的每項主張。

完美根據性是指答案候選人中的每項聲明,都必須由一或多項事實佐證。換句話說,著作權聲明完全由事實所推斷。如果聲明僅部分成立,則不視為有根據。舉例來說,「Google 是由 Larry Page 和 Sergey Brin 於 1975 年創立」這項說法只部分正確 (創辦人姓名正確,但日期有誤),因此整項說法會被視為沒有根據。在這個版本的「檢查根據」API 中,句子會視為單一主張。

您可以使用檢查基礎 API 檢查任何文字。可以是人為生成的簡介,也可以是機器生成的回覆。常見用途是根據一組事實檢查 LLM 生成的回覆。檢查基礎 API 的設計目標是快速執行,延遲時間不超過 500 毫秒。這項速度可讓聊天機器人在每次推論期間呼叫檢查基礎 API,不會造成明顯的延遲。Check Grounding API 也會提供參考資料來佐證結果,讓使用者判斷生成的回覆內容是否可靠。API 也會提供支援分數,指出回應的整體準確度。設定引用門檻後,聊天機器人就能在推論時篩除可能含有捏造內容的回覆。

本頁面說明如何使用 check grounding API 檢查基礎。

實驗功能

如要試用下列適用於檢查基礎 API 的實驗功能,請與 Google 帳戶團隊聯絡,並要求將您加入許可清單:

  • 反向引用:反向引用功能會提供矛盾分數,指出候選答案與指定事實的矛盾程度。此外,回覆也會針對每項聲明提供與矛盾事實相關的引文。詳情請參閱「取得答案候選人的矛盾分數」。

  • 實用性分數:實用性分數是用來評估候選答案是否能充分回應特定要求。詳情請參閱「取得候選答案的實用性分數」。

  • 透過資料儲存庫建立基準:從 Vertex AI Search 資料儲存庫取得基準事實,而非提供內嵌事實。詳情請參閱「使用資料存放區檢查基礎」。

字詞定義與說明

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

輸入資料

檢查基礎 API 需要在要求中提供下列輸入內容。

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

  • 事實:一組文字片段,用做參考,以確保內容符合事實。 每個文字片段都可以提供一組中繼資料屬性 (鍵/值組合)。舉例來說,「作者」和「名稱」是典型的屬性鍵。

    這項服務最多支援 200 個事實,每個事實最多可有 10, 000 個字元。

    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 個詞元,詞元定義為句子中的字詞或句號 (用於結束句子的標點符號)。舉例來說,「他們在 2024 年穿了現成衣。」這句話有七個符記,包括六個字和一個句號。

  • TEXT:用於根據事實的文字片段,例如 Titanic is a 1997 American epic... Academy Awards. (請參閱「事實範例」一節中的完整文字)。

  • ATTRIBUTE:與事實相關聯的中繼資料屬性名稱,例如 authortitle。這是使用者定義的標籤,可為事實文字新增更多資訊。舉例來說,如果事實文字 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 WikipediaTitanic (1997 film)

  • CITATION_THRESHOLD:介於 0 到 1 之間的浮點值,用於判斷答案候選內容中的聲明是否必須引用事實。門檻越高,參考文獻越少但越有力;門檻越低,參考文獻越多但越薄弱。如未設定,預設門檻值為 0.6

Python

詳情請參閱 AI Applications Python API 參考說明文件

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

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. (請參閱「事實範例」一節中的完整文字)。

    • :與事實相關聯的中繼資料屬性名稱,例如 authortitleATTRIBUTE這是使用者定義的標籤,可為事實文字新增更多資訊。舉例來說,如果事實文字 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 WikipediaTitanic (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

取得候選答案的矛盾分數

除了支援分數,您也可以取得矛盾分數。矛盾分數大致上是與所提供事實相矛盾的聲明比例。

如要試用這項實驗功能,請與 Google 帳戶團隊聯絡,並要求將您加入許可清單。

取得矛盾分數

如要取得矛盾分數,請按照下列步驟操作:

  1. 準備一組事實。如需更多資訊和範例,請參閱定義及說明的字詞

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

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1alpha/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",
      "enableAntiCitations": "ENABLE_ANTI_CITATION",
      "antiCitationThreshold": "ANTI_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. (請參閱「事實範例」一節中的完整文字)。

    • :與事實相關聯的中繼資料屬性名稱,例如 authortitleATTRIBUTE這是使用者定義的標籤,可為事實文字新增更多資訊。舉例來說,如果事實文字 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 WikipediaTitanic (1997 film)

    • CITATION_THRESHOLD:介於 0 到 1 之間的浮點值,用於判斷答案候選內容中的聲明是否必須引用事實。門檻越高,用來佐證說法的引用內容就越少,但可信度越高;門檻越低,用來佐證說法的引用內容就越多,但可信度越低。如未設定,預設門檻值為 0.6。

    • ENABLE_ANTI_CITATION:布林值。將這個欄位設為 true,即可啟用實驗功能來評估矛盾分數。移除這個欄位,或將這個欄位設為 false,即可關閉這項功能。

    • ANTI_CITATION_THRESHOLD:介於 0 到 1 之間的浮點值,用於判斷事實是否必須引述為與候選答案中的主張相矛盾。門檻越高,與聲明內容相悖的引用內容就越少,但可信度越高;門檻越低,與聲明內容相悖的引用內容就越多,但可信度越低。如未設定,預設門檻值為 0.8。

要求範例

使用上一節的範例事實,您可以傳送下列要求。將 CANDIDATE 欄位替換為要檢查根據和矛盾的不同字串。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1alpha/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",
      "enableAntiCitations": true,
      "antiCitationThreshold": "0.8",
    }
    }'

出現矛盾的回覆範例

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

候選答案 檢查基礎回應
Titanic was directed by James Cameron. It starred Brad Pitt and Kate Winslet 支援分數:0.36

矛盾分數:0.49

引用的區塊:
  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
    引用內容:[] 反引用內容:[0]
    需要進行基礎事實檢查:true

取得候選答案的實用性分數

如要試用這項實驗功能,請與 Google 帳戶團隊聯絡,並要求將您加入許可清單。

除了支援分數和矛盾分數外,檢查基礎 API 還能提供實用分數。有用的回覆是指以資訊豐富的方式,有效滿足使用者要求 (如提示中所述)。實用性分數是用來評估回覆是否符合下列條件:

  • 回應提示的核心意圖
  • 提供完整詳細資料,同時簡潔明瞭
  • 直接回答問題或完成提示中要求的工作
  • 提供相關資訊
  • 清楚明瞭
  • 避免不必要的細節和術語

如要取得實用性分數和依據分數,請一併提供提示、候選答案和事實。檢查基礎 API 會根據提示檢查候選答案,並提供分數,指出候選答案是否能有效回應提示。分數範圍為 [0,1],分數越高,代表答案越有幫助。

取得實用性分數

如要取得實用性分數,請按照下列步驟操作:

  1. 準備提示和候選人答案。

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

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1alpha/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": {
      "enableHelpfulnessScore": true
    },
    "prompt": "PROMPT",
    }'
    

    更改下列內容:

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

    • CANDIDATE:您要取得實用性分數的候選答案字串,例如 Titanic was directed by James Cameron. It was released in 1997.。候選答案最多可包含 4096 個符記。

    • TEXT:用於基礎的文字片段,例如 Titanic is a 1997 American epic... Academy Awards. (請參閱「事實範例」一節中的完整文字)。

    • :與事實相關聯的中繼資料屬性名稱,例如 authortitleATTRIBUTE這是使用者定義的標籤,可為事實文字新增更多資訊。舉例來說,如果事實文字 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 WikipediaTitanic (1997 film)

    • PROMPT:提示是生成候選答案時所依據的查詢,例如 Who directed and starred in the movie Titanic?

要求範例

使用上一節的範例事實,您可以傳送下列要求。以不同答案候選項目取代 CANDIDATE 欄位,即可取得答案實用性分數。

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1alpha/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": {
      "enableHelpfulnessScore": true
    },
    "prompt": "Who directed and starred in the movie Titanic?"
    }'

顯示實用性分數的回覆範例

下表顯示答案候選項目及其實用性分數的範例。在每種情況下,提示都是 Who directed and starred in the Titanic?

候選答案 實用性分數 分數說明
Titanic was directed by James Cameron. It starred Leonardo DiCaprio and Kate Winslet. 0.980 簡潔且完整的 分數
Cameron, DiCaprio and Winslet. 0.947 未完成
James Cameron's 1997 masterpiece, Titanic, captured the hearts of audiences worldwide with its tragic love story set against the backdrop of the ill-fated maiden voyage of the "unsinkable" ship. The film, a mesmerizing blend of historical drama and fictional romance, starred Leonardo DiCaprio as Jack Dawson, a penniless artist who falls for Rose DeWitt Bukater, a young woman trapped by her social standing and played exquisitely by Kate Winslet. Their passionate love affair unfolds amidst the grandeur and opulence of the Titanic, a floating palace of dreams that ultimately succumbs to a devastating fate. 0.738 不簡潔

使用資料儲存庫檢查基礎

根據預設,系統會根據 Check Grounding API 呼叫中提供的內嵌事實,檢查內容是否奠基於既有事實。不過,除了直接提供事實,您也可以比對 Vertex AI Search 資料儲存庫中的所有事實,檢查候選答案是否正確。

呼叫檢查基準 API 時,您會提供 Vertex AI 搜尋應用程式的名稱。與該搜尋應用程式相關聯的非結構化資料儲存庫,則會保存用於檢查候選答案基準的事實集。

如要試用這項實驗功能,請與 Google 帳戶團隊聯絡,並要求將您加入許可清單。

如要根據資料存放區檢查基礎,請按照下列步驟操作:

  1. 找出與至少一個含有非結構化資料的資料儲存庫相關聯的自訂搜尋應用程式。這個資料儲存庫中的文件會做為事實依據的來源。

    如要瞭解如何建立資料儲存庫和搜尋應用程式,請參閱「建立搜尋資料儲存庫」和「建立搜尋應用程式」。

  2. 找出應用程式 ID。如果已有應用程式 ID,請跳到下一個步驟。

    1. 前往 Google Cloud 控制台的「AI Applications」頁面。

      前往「應用程式」

    2. 在「應用程式」頁面中,找出應用程式名稱,然後從「ID」欄取得應用程式的 ID。

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

    curl -X POST \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    -H "Content-Type: application/json" \
    "https://discoveryengine.googleapis.com/v1alpha/projects/PROJECT_ID/locations/global/groundingConfigs/default_grounding_config:check" \
    -d '{
        "answerCandidate": "CANDIDATE",
        "groundingSource": {
          "searchSource": {
            "servingConfig": "projects/SOURCE_PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search"
          }
        }
    }'
    

    更改下列內容:

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

    • SOURCE_PROJECT_ID:包含應用程式的專案編號或 ID,用於基礎模型。

      這個來源專案必須與您的專案位於相同區域,例如兩者都位於 globaleu。(如需多區域的一般資訊,請參閱「位置」一文)。

    • CANDIDATE:您要取得支援分數的候選答案字串,例如 Titanic was directed by James Cameron. It was released in 1997.。候選答案最多可有 4096 個符記,符記是指句子中的單字或句號 (用於結束句子的標點符號)。舉例來說,「他們在 2024 年穿了成衣。」這句話有七個符記,包括六個字和一個句號。

    • APP_ID:Vertex AI Search 應用程式的 ID,該應用程式的非結構化資料儲存庫包含您要用做基準來源的事實。