在 Agent Search 的检索增强生成 (RAG) 体验中,您可以检查接地情况,以确定一段文本(称为“候选答案”)在给定的一组参考文本(称为“事实”)中的接地程度。
Check Grounding API 会返回 0 到 1 之间的总体支持得分,表示回答候选项与给定事实的一致程度。回答还包含对支持回答候选项中每项声明的事实的引用。
完美接地要求回答候选项中的每个声明都必须有一个或多个给定事实的支持。换句话说,声明完全由事实推导得出。如果声明仅部分符合,则不被视为有依据。例如,“Google 由 Larry Page 和 Sergey Brin 于 1975 年创立”这一声明只有部分正确,即创始人的姓名正确,但日期错误,因此整个声明被视为不实。在此版本的 Check Grounding API 中,一个句子被视为一个声明。
您可以使用 Check Grounding API 检查任何文本。它可以是人工生成的简介,也可以是机器生成的回答。一个典型的用例是根据一组给定的事实检查 LLM 生成的回答。检查接地 API 旨在实现快速响应,延迟时间不到 500 毫秒。这种速度使聊天机器人能够在每次推理期间调用检查接地 API,而不会导致速度显著降低。接地检查 API 还可以提供参考信息来支持其发现结果,以便用户了解生成的回答中哪些部分是可靠的。该 API 还会提供支持分数,以指示回答的整体准确率。通过设置引用阈值,聊天机器人可以在推理时滤除可能包含虚构声明的回答。
本页面介绍了如何使用检查接地 API 检查接地。
术语定义和解释
在使用 Check Grounding API 之前,最好先了解输入和输出,以及如何构建接地事实以获得最佳结果。
输入数据
Check Grounding API 要求在请求中提供以下输入。
回答候选项:回答候选项可以是您想要检查其依据的任何文本。例如,在 Agent Search 的上下文中,候选回答可能是生成的搜索摘要,用于回答查询。然后,API 会确定摘要在输入事实中的依据程度。候选答案的长度上限为 4096 个 token,其中 token 定义为句子中的一个单词或句点(用于结束句子的标点符号)。例如,“They wore off-the-rack clothes in 2024.”这句话包含 7 个 token,其中包括 6 个单词和 1 个句点。
事实:一组用作接地参考的文本段。每个文本片段都可以提供一组元数据属性(键值对)。例如,“作者”和“标题”是典型的属性键。
该服务最多支持 200 条事实信息,每条信息最多 1 万个字符。
Google 建议不要提供包含所有信息的超大事实。相反,您可以将大型事实拆分为较小的事实,并为较小的事实提供适当的属性,从而获得更好的结果。例如,您可以按标题、作者或网址拆分大型事实,并在属性中提供这些信息。
引用阈值:一个介于 0 到 1 之间的浮点值,用于控制支持回答候选项的引用的置信度。阈值越高,置信度要求越严格。因此,阈值越高,引文数量越少,但引文越强。
输出数据
Check Grounding API 会针对候选回答返回以下内容:
支持得分:支持得分是一个介于 0 到 1 之间的数字,表示候选答案在所提供的事实集中的接地程度。它大致估算了回答候选项中被发现基于一个或多个给定事实的声明比例。
引用的块:引用的块是输入事实中支持回答候选项的部分。
主张和引用:主张和引用将候选答案的主张(通常是一个句子)与一个或多个证实该主张的引用块相关联。
声明使用其开始和结束位置进行界定。这些是采用 UTF-8 编码的声明字符串的字节位置。请注意,该值不是以字符为单位进行衡量的,因此在界面中呈现时必须考虑到某些字符可能占用多个字节。 例如,如果声明文本包含非 ASCII 字符,则以字符(与编程语言有关)和字节(与编程语言无关)为单位衡量的起始位置和结束位置会有所不同。
声明级支持得分:启用声明级得分后,每项声明都会返回一个介于 0 到 1 之间的支持得分,表示声明在所提供事实集中的接地程度。如需了解详情,请参阅获取候选答案的声明级分数。
需要接地检查:对于每个声明,系统都会返回一个“需要接地检查”布尔值。如果此字段返回为
False,则表示系统认为相应主张不需要接地,因此不会返回引用。例如,“Here is what I found.”这样的句子本身不是事实,因此不需要进行接地检查。当 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 个 token,其中 token 定义为句子中的单词或句点(用于结束句子的标点符号)。例如,“They wore off-the-rack clothes in 2024.”这句话包含 7 个 token,包括 6 个单词和 1 个句点。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 OntarioToronto 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 进行身份验证,请设置应用默认凭据。如需了解详情,请参阅为本地开发环境设置身份验证。
事实示例
以下是事实及其属性的几个示例。这些示例旨在帮助您了解接地响应和 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"
}
}'
候选答案和接地响应示例
下表展示了当您根据示例事实发送示例请求时,不同候选答案和回答的示例。
| 回答候选项 | Check Grounding 响应 |
|---|---|
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 个词元,其中词元定义为句子中的单词或句点(用于结束句子的标点符号)。例如,“They wore off-the-rack clothes in 2024.”这句话包含 7 个 token,其中包括 6 个单词和 1 个句点。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 OntarioToronto 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,
}
}'
包含声明级得分的回答示例
下表显示了根据示例事实发送示例请求时,示例候选答案及其回答。
| 回答候选项 | Check Grounding 响应 |
|---|---|
Here is what I found. Titanic was directed by James Cameron. It
starred Kate Winslet and Leonardo DiCaprio.
|
支持分数:0.99引用的文本块:
|