在 Vertex AI Search 中使用檢索增強生成 (RAG) 功能時,您可以檢查基準,判斷一段文字 (稱為「候選答案」) 在一組參考文字 (稱為「事實」) 中的基準程度。
Check Grounding API 會傳回 0 到 1 的整體支援分數,指出候選答案與指定事實的相符程度。回覆也會引用事實,佐證候選答案中的每項主張。
完美根據事實回答問題時,答案候選項目中的每項主張都必須有至少一項事實佐證。換句話說,著作權聲明完全由事實所衍生。如果聲明僅部分成立,則不視為有根據。舉例來說,「Google 是由 Larry Page 和 Sergey Brin 於 1975 年創立」這項說法只部分正確 (創辦人姓名正確,但日期有誤),因此整項說法會被視為無根據。在這個版本的檢查基礎 API 中,句子會視為單一主張。
您可以使用 check grounding API 檢查任何文字。可以是人為生成的簡介,也可以是機器生成的回覆。常見用途是根據一組事實檢查 LLM 生成的回覆。檢查基礎 API 的設計目標是快速執行,延遲時間不超過 500 毫秒。這項速度可讓聊天機器人在每次推論期間呼叫檢查基礎 API,不會造成明顯的延遲。檢查基礎 API 也會提供參考資料來佐證結果,讓使用者判斷生成的回覆內容是否可靠。API 也會提供支援分數,指出回應的整體準確度。設定引用門檻後,聊天機器人就能在推論時篩除可能含有捏造內容的回覆。
本頁面說明如何使用 check grounding API 檢查基礎。
字詞定義與說明
使用 Check Grounding API 前,建議先瞭解輸入和輸出內容,以及如何建構基礎事實,以獲得最佳結果。
輸入資料
檢查基礎 API 需要在要求中提供下列輸入內容。
候選答案:候選答案可以是任何您想檢查根據的文字。舉例來說,在 Vertex AI Search 的情境中,候選答案可能是生成的回覆查詢搜尋摘要。接著,API 會判斷摘要內容與輸入事實的關聯程度。候選答案最多可有 4096 個權杖,權杖定義為句子中的字詞或句號 (用於結束句子的標點符號)。舉例來說,「They wore off-the-rack clothes in 2024.」這句話有七個符記,包括六個字和一個句號。
事實:一組文字片段,用做參考依據。 每個文字片段都可以提供一組中繼資料屬性 (鍵/值組合)。舉例來說,「作者」和「名稱」是典型的屬性鍵。
這項服務最多支援 200 個事實,每個事實最多可有 1 萬個字元。
Google 建議不要提供包含所有資訊的單一超大事實。建議您將大型事實拆解成較小的事實,並為這些較小的事實提供適當的屬性,這樣就能獲得更準確的結果。舉例來說,你可以依據標題、作者或網址將大型事實拆開,並在屬性中提供這項資訊。
引文門檻:介於 0 到 1 的浮點值,可控制引文的信心程度,以支援候選答案。門檻越高,可信度就越嚴格。因此,門檻越高,引文數量越少,但引文的影響力越強。
輸出資料
Check Grounding API 會針對候選答案傳回下列內容:
支援分數:支援分數是介於 0 到 1 之間的數字,表示候選答案與所提供的事實集有多相符。這項指標大致會估算答案候選項目中,有多少比例的說法是根據一或多個事實而來。
引用的內容塊:引用的內容塊是指輸入事實中,可做為候選答案依據的部分。
聲明和引文:聲明和引文會將候選答案的聲明 (通常是句子) 連結至一或多個引用的區塊,以佐證該聲明。
系統會使用著作權聲明的開始和結束位置來劃分著作權聲明。這些是 UTF-8 編碼聲明字串的位元組位置。請注意,這不是以字元計算,因此必須在使用者介面中算繪,並考量部分字元會佔用超過一個位元組。 舉例來說,如果聲明文字含有非 ASCII 字元,以字元 (取決於程式設計語言) 和位元組 (不取決於程式設計語言) 測量時,開始和結束位置會有所不同。
主張層級支援分數:啟用主張層級分數後,系統會針對每項主張傳回 0 到 1 的支援分數,指出主張與提供的事實集有多相符。詳情請參閱「取得候選答案的聲明層級分數」。
需要進行基礎事實檢查:系統會針對每項聲明傳回基礎事實檢查布林值。如果傳回
False
,表示系統認為該聲明不需要根據事實,因此不會傳回引文。舉例來說,「以下是我找到的資訊」這類句子本身並非事實,因此不需要進行基礎事實查核。如果 grounding-check-required 傳回
true
,表示系統已執行基礎檢查,並傳回支援分數和引文 (如有)。
取得候選答案的支援分數
如要瞭解候選答案與一組事實的關聯程度,請按照下列步驟操作:
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
:與事實相關聯的中繼資料屬性名稱,例如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
詳情請參閱 Vertex AI Search Python API 參考說明文件。
如要向 Vertex AI Search 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。
事實範例
以下列舉幾個事實及其屬性。這些範例可協助您瞭解基礎回應和 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 引用的區塊:
|
Titanic was directed by James Cameron. It was released in
1997.
|
支援分數:0.99 引用的區塊:
|
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 引用的區塊:
|
Titanic was directed by James Cameron. It starred Brad Pitt and
Kate Winslet
|
支援分數:0.54 引用的區塊:
"It starred Brad Pitt and Kate Winslet" 聲明並非完全正確,因此不會獲得任何引用。 |
取得候選答案的聲明層級分數
除了答案層級的支援分數,您也可以取得答案候選項目中每個主張的支援分數。
如要取得聲明層級的分數,請按照下列步驟操作:
準備一組事實。如需更多資訊和範例,請參閱定義及說明的字詞。
使用下列 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 個符記,符記定義為句子中的單字或句號 (用於結束句子的標點符號)。舉例來說,「他們在 2024 年穿了成衣。」這個句子有七個符記,包括六個字和一個句號。TEXT
:用於基礎的文字片段,例如Titanic is a 1997 American epic... Academy Awards.
(請參閱事實範例中的全文)。:與事實相關聯的中繼資料屬性名稱,例如
author
或title
。ATTRIBUTE
這是使用者定義的標籤,可為事實文字新增更多資訊。舉例來說,如果事實文字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 引用的區塊:
|