使用 RAG 檢查接地

在 AI 應用程式中,您可以透過檢查建立基準來判斷一段文字 (稱為「答案候選項」) 與一組特定參考文字 (稱為「事實」) 的關聯程度。

check grounding API 會傳回 0 到 1 的整體支援分數,用來表示答案候選項與給定事實的一致程度。回覆內容也會包含參考資料,說明答案候選項中每項聲明的相關事實。

完美依據要求,答案候選中的每項主張都必須由一或多項給定事實支持。換句話說,聲明完全包含在事實中。如果索賠內容僅部分包含在內,則不視為有根據。舉例來說,「Google 是由 Larry Page 和 Sergey Brin 於 1975 年創立」這項聲明只有部分正確 (創辦人的名字正確,但日期有誤),因此整個聲明會被視為不實。在這個版本的檢查接地 API 中,一句話會視為單一陳述。

您可以使用 check grounding API 檢查任何文字。可以是人為產生的簡介,或是機器產生的回應。典型用途是檢查 LLM 生成的回覆是否符合給定的事實集合。檢查接地 API 的設計旨在縮短延遲時間,讓延遲時間低於 500 毫秒。這個速度可讓聊天機器人在每次推論期間呼叫檢查依據 API,而不會造成明顯的速度減緩。檢查基準 API 也能提供參考資料來支援其發現的結果,讓使用者瞭解產生回應的哪些部分可靠。API 也會提供支援分數,用於指出回應的整體準確度。只要設定引文閾值,聊天機器人就能在推論期間過濾可能含有幻覺斷言的回覆。

本頁面說明如何使用檢查接地 API 檢查接地。

實驗功能

如果您想試用以下可用於檢查接地 API 的實驗功能,請與 Google 帳戶團隊聯絡,要求將您加入許可清單:

定義和說明

在使用 Check Grounding API 之前,請先瞭解輸入內容和輸出內容,以及如何建構地面事實,以獲得最佳結果。

輸入資料

check grounding 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,表示系統判定聲明不需要依據,因此不會傳回引文和反引文。舉例來說,「我發現了以下內容」這類句子本身並非事實,因此不需要進行基礎檢查。

    如果「需要依據基礎資料進行檢查」的值為 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:與事實相關的中繼資料屬性名稱,例如 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 指令的格式

  • Fact 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.
    Grounding check required: 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]
    Grounding check required:true
  • 1. Cited chunks: It was based on the sinking of the RMS Titanic that led to the death of 1500 people.
    Citations:[1]
    Grounding check required: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:與事實相關的中繼資料屬性名稱,例如 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。

    • 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.
    Grounding check required: 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. (請參閱「事實示例」中的完整文字)。

    • 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。

    • 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.
    Citations:[0] Anti-citations:[]
    Grounding check required:true
  • 1. It starred Brad Pitt and Kate Winslet
    Citations: [] Anti-citations: [0]
    Grounding check required: true

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

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

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

  • 解決提示的核心意圖
  • 提供完整詳細資料,同時保持簡潔
  • 直接回答問題或完成提示中要求的任務
  • 提供相關資訊
  • 是否清楚明確
  • 避免使用不必要的細節和專業術語

如要取得有依據分數和有幫助分數,您必須提供提示、答案候選項和事實。check grounding 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. (請參閱「事實示例」中的完整文字)。

    • 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)

    • 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 不簡潔

使用資料儲存庫檢查接地

根據預設,系統會根據檢查基於事實的 API 呼叫中提供的一組事實,檢查基於事實的內容。不過,您也可以選擇不直接提供事實資料,而是檢查答案候選項是否與 Vertex AI Search 資料儲存庫中的所有事實相符。

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

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

如要檢查與資料儲存庫的接地情形,請按照下列步驟操作:

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

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

  2. 找出應用程式 ID。如果您已取得應用程式 ID,請直接跳到下一個步驟。

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

      前往「應用程式」

    2. 在「Apps」頁面中找出應用程式名稱,然後從「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 個符記,其中符記是指句子中的字詞或句號 (用於結束句子的標點符號)。舉例來說,句子「They wore off-the-rack clothes in 2024.」有七個符記,包括六個字詞和一個句號。

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