使用 RAG 检查接地

在 Vertex AI Agent Builder 中使用检索增强生成 (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 的以下实验性功能,请与您的 Google 客户支持团队联系,请求将您添加到许可名单:

  • 反引号:反引号功能会为您提供一个矛盾得分,该得分表示候选答案与给定事实相矛盾的程度。回复还应包含对每项声明的矛盾事实的引用。如需了解详情,请参阅获取答案候选项的矛盾得分

  • 断言级支持得分:除了候选答案的总体支持得分之外,断言级支持得分还表示候选答案中每个断言的依据。如需了解详情,请参阅获取候选答案的声明级得分

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

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

术语定义和说明

在使用检查接地 API 之前,了解输入和输出以及如何构建接地事实结构,有助于您取得最佳结果。

输入数据

检查接地 API 要求在请求中包含以下输入。

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

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

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

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

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

输出数据

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

  • 支持得分:支持得分是一个介于 0 到 1 之间的数字,表示候选答案在所提供的事实集合中的依据程度。它大致近似于答案候选项中被发现基于一个或多个给定事实的断言所占的比例。

  • 引用的代码段:引用的代码段是输入事实中支持候选答案的部分。

  • 断言和引用:断言和引用将答案候选项的断言(通常是一个句子)与一个或多个用于证实该断言的引用内容块相关联。

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

  • 需要进行接地检查:系统会针对每项声明返回一个 ground-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 个令牌,包括 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

如需了解详情,请参阅 Vertex AI Agent Builder Python API 参考文档

如需向 Vertex AI Agent Builder 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅为本地开发环境设置身份验证

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" 并不完全属实,因此没有获得任何引用。在这种情况下,您可以调用启用了反引文的方法,以获得矛盾得分。 如需了解详情,请参阅获取答案候选项的矛盾得分

获取答案候选项的矛盾得分

除了支持得分之外,您还可以获得矛盾得分。矛盾得分大致相当于与所提供事实相矛盾的声明所占的比例。

如需试用此实验性功能,请与您的 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 个令牌,其中令牌定义为句子中的字词或句号(用于结束句子的标点符号)。例如,“他们在 2024 年穿着现成服装”这个句子包含 7 个令牌,包括 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.
    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 还可以提供实用性得分。实用响应是指以信息丰富的方式有效满足用户的请求(如提示中所述)。实用性得分用于衡量回答在以下方面表现如何:

  • 解决问题的核心意图
  • 提供完整详细信息,同时做到简洁
  • 直接回答所问问题或完成提示中请求的任务
  • 提供相关信息
  • 清晰明确
  • 避免添加不必要的详细信息和专业术语

如需同时获得实用性得分和依据得分,您必须提供提示、候选答案和事实。检查基础 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 不简洁

获取候选答案的声明级得分

除了答案级支持得分之外,您还可以为候选答案中的每个断言获取断言级支持得分。

如需试用此实验性功能,请与您的 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",
      "enableClaimLevelScore": "ENABLE_CLAIM_LEVEL_SCORE",
    }
    }'
    

    替换以下内容:

    • PROJECT_ID:您的 Google Cloud 项目的项目编号或 ID。

    • CANDIDATE:您要获取支持得分对应的候选答案字符串,例如 Titanic was directed by James Cameron. It was released in 1997.。候选答案的长度不得超过 4096 个令牌,其中令牌定义为句子中的字词或句号(用于结束句子的标点符号)。例如,“他们在 2024 年穿着现成服装”这个句子包含 7 个令牌,包括 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/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",
      "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

使用数据存储区检查接地

默认情况下,系统会根据 Check Grounding API 调用中内嵌提供的一组事实来检查对话内容是否贴合现实。不过,您也可以通过与 Vertex AI Search 数据存储区中的所有事实进行对比,来检查候选答案。

调用检查接地 API 时,您需要提供 Vertex AI 搜索应用的名称。与该搜索应用关联的非结构化数据存储区则存储用于检查候选答案接地的事实集。

如需检查与数据存储区之间的接地,请按以下步骤操作:

  1. 确定与至少一个包含非结构化数据的数据存储区相关联的通用搜索应用。此数据存储区中的文档是您的基准事实的来源。

    如需了解如何创建数据存储区和搜索应用,请参阅创建搜索数据存储区创建搜索应用

  2. 找到您的应用 ID。如果您已经有应用 ID,请跳至下一步。

    1. 在 Google Cloud 控制台中,前往 Agent Builder 页面。

      前往“应用”

    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": "projects/SOURCE_PROJECT_ID/locations/global/collections/default_collection/engines/APP_ID/servingConfigs/default_search"
        }
    }'
    

    替换以下内容:

    • PROJECT_ID:您的 Google Cloud 项目的项目编号或 ID。

    • SOURCE_PROJECT_ID:包含要作为基准的应用的项目的项目编号或 ID。

      此源项目必须与您的项目位于同一区域,例如,都位于 global 或都位于 eu。(如需了解多区域的一般信息,请参阅位置。)

    • CANDIDATE:您要获取支持得分的候选回答字符串,例如 Titanic was directed by James Cameron. It was released in 1997.。候选回答的长度不得超过 4096 个词元,其中词元定义为句子中的字词或句号(用于结束句子的标点符号)。例如,“They wore off-the-rack clothes in 2024.” 这个句子包含 7 个令牌,包括 6 个字词和一个句号。

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