使用 RAG 检查接地

在 AI 应用中体验检索增强生成 (RAG) 功能时,您可以检查接地情况,以确定一段文本(称为候选答案)在给定的参考文本集(称为事实)中的接地程度。

Check Grounding API 会返回一个介于 0 到 1 之间的总体支持得分,用于指示候选答案与给定事实的一致程度。响应还包括对答案候选项中支持每项声明的事实的引用

完美的事实依据要求回答候选中的每项声明都必须得到一个或多个给定事实的支持。换句话说,事实完全蕴含了相应声明。如果声明仅部分被蕴含,则不视为有事实依据。例如,“Google 是由 Larry Page 和 Sergey Brin 于 1975 年创立的”这一声明只部分正确(创始人姓名正确,但日期错误),因此整个声明都被视为无根据。在此版本的检查依据 API 中,一个句子被视为一个声明。

您可以使用 check grounding API 检查任何文本。可以是人工生成的摘要,也可以是机器生成的回答。一个典型用例是根据一组给定的事实检查 LLM 生成的回答。检查 grounding API 的设计目标是快速,延迟时间不超过 500 毫秒。这种速度使聊天机器人能够在每次推理期间调用检查依据 API,而不会导致速度明显变慢。Check Grounding API 还可以提供参考信息来支持其发现,以便用户判断生成的回答中哪些部分是可靠的。该 API 还会提供支持分数,以指示回答的总体准确性。通过设置引用阈值,聊天机器人可以在推理时过滤掉可能包含幻觉性声明的回答。

本页介绍了如何使用 check grounding API 检查事实依据。

实验性功能

如果您想试用以下适用于检查依据 API 的实验性功能,请与您的 Google 客户支持团队联系,并要求将您添加到许可名单中:

  • 反引用:反引用功能可为您提供矛盾分数,用于指示候选答案与给定事实的矛盾程度。响应还包括每项声明的矛盾事实的引用。如需了解详情,请参阅获取候选答案的矛盾分数

  • 帮助性得分:帮助性得分用于衡量候选答案回答给定请求的程度。如需了解详情,请参阅获取候选答案的实用性得分

  • 使用数据存储区作为依据:从 Vertex AI Search 数据存储区获取依据事实,而不是提供内嵌事实。如需了解详情,请参阅使用数据存储区检查依据

术语定义和说明

在使用检查事实依据 API 之前,最好先了解输入和输出,以及如何构建事实依据以获得最佳结果。

输入数据

检查接地 API 需要在请求中提供以下输入。

  • 候选答案:候选答案可以是您想要检查依据的任何文本。例如,在 Vertex AI Search 的上下文中,回答候选对象可能是回答查询的生成的搜索摘要。然后,该 API 会确定摘要与输入事实之间的关联程度。候选答案的长度不得超过 4096 个 token,其中 token 定义为句子中的一个字词或句号(用于结束句子的标点符号)。例如,“They wore off-the-rack clothes in 2024.”这句话包含 7 个 token,包括 6 个字词和一个句点。

  • 事实:一组文本片段,用作接地参考。 可以为每个文本段提供一组元数据属性(键值对)。例如,“作者”和“标题”是典型的属性键。

    该服务最多支持 200 条事实信息,每条事实信息最多包含 1 万个字符。

    Google 不建议提供包含所有信息的一个非常大的事实。相反,您可以将大型事实拆分为较小的事实,并为较小的事实提供适当的属性,从而获得更好的结果。例如,您可以按标题、作者或网址细分大型事实,并在属性中提供这些信息。

  • 引用阈值:一个介于 0 到 1 之间的浮点值,用于控制支持答案候选的引用的置信度。阈值越高,置信度要求越严格。因此,阈值越高,获得的引用次数越少,但引用强度越高。

输出数据

对于候选答案,Check Grounding API 会返回以下内容:

  • 支持分数:支持分数是一个介于 0 到 1 之间的数字,表示候选答案在所提供的事实集中的接地程度。它大致表示答案候选内容中被发现基于一个或多个给定事实的声明所占的比例。

  • 引用块:引用块是指支持候选答案的输入事实的部分内容。

  • 声明和引用:声明和引用将回答候选者的声明(通常是一个句子)与证实该声明的一个或多个引用块相关联。

    声明使用其开始位置和结束位置来划定。这些是 UTF-8 编码的声明字符串的字节位置。请注意,此长度不是以字符数来衡量的,因此必须在界面中呈现,同时考虑到某些字符占用多个字节。 例如,如果声明文本包含非 ASCII 字符,则以字符(取决于编程语言)和以字节(不取决于编程语言)为单位测量的起始位置和结束位置会有所不同。

  • 声明级支持分数:启用声明级分数后,系统会针对每个声明返回一个介于 0 到 1 之间的支持分数,用于指示声明在所提供的事实集中的依据程度。如需了解详情,请参阅获取候选答案的声明级得分

  • 需要进行事实依据检查:对于每项声明,系统都会返回一个 grounding-check-required 布尔值。如果此方法返回 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.”包含 7 个 token,其中包括 6 个字词和一个句点。

  • 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 应用进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

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"
    }
    }'

答案候选和依据响应示例

下表展示了当您根据示例事实发送示例请求时,不同的候选答案和响应示例。

回答候选 Check Grounding 响应
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.”这句话包含 7 个 token,其中包括 6 个字词和一个句点。

    • 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,
    }
    }'

包含声明级得分的回答示例

下表显示了在您根据示例事实发送示例请求时,示例候选答案及其回答。

回答候选 Check Grounding 响应
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.”这句话包含 7 个 token,其中包括 6 个字词和一个句点。

    • 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",
    }
    }'

包含矛盾信息的回答示例

下表显示了在您根据示例事实发送示例请求时,示例候选答案及其回答。

回答候选 Check Grounding 响应
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 客户支持团队联系,并申请将您添加到许可名单中。

除了支持度得分和矛盾度得分之外,Check Grounding 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.(请参阅事实依据示例中的完整文本)。

    • 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 不够简洁

使用数据存储区检查事实依据

默认情况下,系统会根据在 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 个词元,其中词元是指句子中的一个字或句号(用于结束句子的标点符号)。例如,“They wore off-the-rack clothes in 2024.”这句话包含 7 个 token,其中包括 6 个字词和一个句点。

    • APP_ID:Vertex AI 搜索应用的 ID,该应用的非结构化数据存储区包含您要用作接地来源的事实。