グラウンディングされた生成でクエリに回答する

AnswerQuery メソッドを使用して、 デベロッパー ナレッジ コーパスに基づいたクエリの回答を取得します。

始める前に

API を有効にして Developer Knowledge API キーを生成し、 キーを環境変数に保存していることを確認します。

export DEVELOPERKNOWLEDGE_API_KEY="YOUR_API_KEY"

使用例

次の例では、「BigQuery データセットを作成するにはどうすればよいですか?」という質問をしています。

curl -X POST "https://developerknowledge.googleapis.com/v1alpha:answerQuery?key=$DEVELOPERKNOWLEDGE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"query": "How do I create a BigQuery dataset?"}'

レスポンスには、answer.answer_text フィールドにテキスト回答が含まれています。 citationsreferencesanswer オブジェクトに含まれています。

{
  "answer": {
    "answerText": "To create a BigQuery dataset, you can use the Google Cloud Console, the bq command-line tool, or the BigQuery client libraries.",
    "citations": [
      {
        "startIndex": 0,
        "endIndex": 123,
        "sources": [
          {
            "referenceIndex": 0
          }
        ]
      }
    ],
    "references": [
      {
        "documentReference": {
          "documentChunk": {
            "parent": "documents/cloud.google.com/bigquery/docs/datasets",
            "content": "This page explains how to create BigQuery datasets...",
            "document": {
              "name": "documents/cloud.google.com/bigquery/docs/datasets",
              "title": "Introduction to datasets",
              "uri": "https://cloud.google.com/bigquery/docs/datasets"
            }
          }
        }
      }
    ]
  }
}