RAG でグラウンディングを確認する

Vertex AI Search の検索拡張生成(RAG)エクスペリエンスの一部として、グラウンディングをチェックし、テキストの一部(回答候補と呼びます)が、特定の参照テキスト(事実と呼びます)セット内でどの程度根拠があるかを判断できます。

Check grounding API は、0 から 1 の全体的なサポートスコアを返します。これは、回答候補が指定された事実とどの程度一致しているかを示します。回答候補の各主張を裏付ける事実の引用も回答内に含まれます。

完全なグラウンディングとは、回答候補内のすべての主張が、指定された事実の 1 つ以上によって裏付けられていることを意味します。つまり、その主張は事実によって完全に裏付けられています。主張が部分的にのみ関連付けられている場合、根拠があるとは見なされません。たとえば、「Google は 1975 年にラリー・ペイジとセルゲイ・ブリンによって設立された」という主張は、創設者の名前は正しいものの、設立日が間違っているため、部分的にしか正しくありません。そのため、この主張全体は根拠がないと見なされます。このバージョンの Check grounding API では、1 つの文が 1 つの主張と見なされます。

Check grounding API を使用して、任意のテキストを確認できます。人間が生成した概要や、機械が生成したレスポンスなどです。一般的なユースケースは、LLM によって生成された回答を特定の事実のセットと照合することです。Check grounding API は、レイテンシが 500 ms 未満の高速な動作をするように設計されています。この速度により、chat bot は、大幅な速度低下を招くことなく、各推論中に Check grounding API を呼び出すことができます。Check grounding API は、検出結果を裏付ける参照も提供できるため、生成された回答の信頼できる部分をユーザーが判断できます。この API は、回答の全体的な精度を示すサポートスコアも提供します。引用のしきい値を設定することで、chat bot は、虚偽の主張が含まれている可能性が高い回答を推論時に除外できます。

このページでは、Check grounding API を使用してグラウンディングを確認する方法について説明します。

用語の定義と説明

Check grounding API を使用する前に、入力と出力、および最良の結果を得るためにグラウンディングの事実を構造化する方法を理解しておくことをおすすめします。

入力データ

Check grounding API では、リクエストに次の入力が必要です。

  • 回答候補: 回答候補は、グラウンディングを確認するテキストの任意の部分です。たとえば、Vertex AI Search のコンテキストでは、回答候補はクエリに回答する生成された検索結果の要約になります。API は、サマリーが入力された事実においてどの程度根拠があるかを判断します。回答候補の最大長は 4, 096 トークンです。トークンは、文中の単語またはピリオド(文の終わりに使用される句読点)として定義されます。たとえば、「They wore off-the-rack clothes in 2024.」という文は、6 つの単語とピリオドを含む 7 つのトークンで構成されています。

  • 事実: グラウンディングの参照として使用されるテキスト セグメントのセット。各テキスト セグメントには、メタデータ属性(Key-Value ペア)のセットを指定できます。たとえば、「作成者」や「タイトル」は一般的な属性キーです。

    このサービスは、最大 200 個の事実(それぞれ最大 10,000 文字)をサポートしています。

    すべての情報を含む非常に大きな事実を 1 つ提供することはおすすめしません。代わりに、大きな事実を小さな事実に分割し、小さな事実に適切な属性を指定することで、より良い結果を得ることができます。たとえば、大きな事実をタイトル、作成者、URL で分割し、この情報を属性で提供できます。

  • 引用のしきい値: 回答候補を裏付ける引用の信頼度を制御する 0 から 1 の浮動小数点値。しきい値が高いほど、信頼性が厳しくなります。そのため、しきい値が高いほど、引用数は減りますが、引用の信頼性は高くなります。

出力データ

Check grounding API は、回答候補について以下を返します。

  • サポートスコア: サポートスコアは 0 から 1 の数値で、回答候補が指定された事実のセットにおいてどの程度根拠があるかを示します。これは、指定された 1 つ以上の事実において根拠があったことが判明した回答候補内の主張の割合を大まかに近似したものです。

  • 引用されたチャンク: 引用されたチャンクは、回答候補を裏付ける入力事実の一部です。

  • 主張と引用: 主張と引用は、回答候補の主張(通常は文)と、主張を裏付ける引用されたチャンクを 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.)。回答候補の最大長は 4,096 トークンです。ここで、トークンは文中の単語またはピリオド(文の終わりに使用される句読点)として定義されます。たとえば、「They wore off-the-rack clothes in 2024.」という文は、6 つの単語とピリオドを含む 7 つのトークンで構成されています。

  • TEXT: グラウンディングに使用するテキスト セグメント(例: Titanic is a 1997 American epic... Academy Awards.)。事実の例の全文をご覧ください。

  • ATTRIBUTE: ファクトに関連付けられたメタデータ属性の名前(例: authortitle)。これは、事実のテキストに追加情報を追加するためのユーザー定義ラベルです。たとえば、事実のテキスト Toronto is the capital of Ontarioauthor 属性があり、その値が 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 という主張は、最初の 2 つの主張よりは根拠がありません。

  • VALUE: 属性の値(Simple WikipediaTitanic (1997 film) など)。

  • CITATION_THRESHOLD: 0 から 1 の浮動小数点値。回答候補の主張に対して事実の引用が必要かどうかを決定します。しきい値が高いほど、引用数は減りますが、引用の信頼性は高くなります。しきい値が低いほど、引用数は増えますが、引用の信頼性は低くなります。設定されていない場合、デフォルトのしきい値は 0.6 です。

Python

詳細については、Vertex AI Search Python API のリファレンス ドキュメントをご覧ください。

Vertex AI Search に対する認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証の設定をご覧ください。

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

回答候補とグラウンディング レスポンスの例

次の表に、事実の例に基づいてリクエストの例を送信した場合のさまざまな回答候補とレスポンスの例を示します。

回答候補 グラウンディング レスポンスをチェックする
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.)。回答候補の最大長は 4,096 トークンです。トークンは、文中の単語またはピリオド(文の終わりに使用される句読点)として定義されます。たとえば、「They wore off-the-rack clothes in 2024.」という文は、6 つの単語とピリオドを含む 7 つのトークンで構成されています。

    • TEXT: グラウンディングに使用するテキスト セグメント(例: Titanic is a 1997 American epic... Academy Awards.)。事実の例の全文をご覧ください。

    • ATTRIBUTE: ファクトに関連付けられたメタデータ属性の名前(例: authortitle)。事実のテキストに追加情報を追加するためのユーザー定義ラベルです。たとえば、事実のテキスト Toronto is the capital of Ontarioauthor 属性があり、その値が 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.
    グラウンディング チェックが必要: false
  • 1. Titanic was directed by James Cameron.
    引用: [0]
    グラウンディング チェックが必要: true
    スコア: 0.99
  • 2. It starred Kate Winslet and Leonardo DiCaprio.
    引用: [0]
    グラウンディング チェックが必要: true
    スコア: 0.99