根據文件生成答案

Developer Knowledge API 可讓您詢問 Google Developers 產品相關問題,並直接獲得自然語言回覆。針對每項查詢,API 會從開發人員知識語料庫中擷取內容,組成回覆 (在 API 參考資料中稱為基準生成),並附上相關說明文件頁面的引用資訊。

事前準備

為偏好的工具設定環境:

gcloud

安裝及設定 gcloud CLI,並啟用 Developer Knowledge API。

REST

啟用 API 並產生 Developer Knowledge API 金鑰。接著,將金鑰儲存為環境變數:

export DEVELOPERKNOWLEDGE_API_KEY="YOUR_API_KEY"

將 YOUR_API_KEY 替換成 Developer Knowledge API 金鑰。

回答查詢

使用 gcloud developer-knowledge answer-query 指令或 answerQuery REST 方法提問。

下列範例會傳送查詢,詢問如何建立 BigQuery 資料集:

gcloud

gcloud developer-knowledge answer-query \
  --query="How do I create a BigQuery dataset?"

REST

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

回覆包含 answer.answerText 欄位中的文字答案,以及 answer 物件中的 citations 和 references:

{
  "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/docs.cloud.google.com/bigquery/docs/datasets",
            "content": "This page explains how to create BigQuery datasets...",
            "document": {
              "name": "documents/docs.cloud.google.com/bigquery/docs/datasets",
              "title": "Introduction to datasets",
              "uri": "https://docs.cloud.google.com/bigquery/docs/datasets"
            }
          }
        }
      }
    ]
  }
}

篩選說明文件來源

如要限制用於生成答案的文件來源,請在 gcloud CLI 中使用 --query-filter 旗標,或在 REST 要求主體中使用 filter 欄位,傳遞篩選運算式。如要進一步瞭解支援的篩選器欄位和運算子,請參閱「篩選搜尋結果」。

以下範例會將說明文件來源限制為 docs.cloud.google.com:

gcloud

gcloud developer-knowledge answer-query \
  --query="How do I create a BigQuery dataset?" \
  --query-filter='data_source = "docs.cloud.google.com"'

REST

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

選擇介於 AnswerQuery 至 SearchDocumentChunks 之間的值

使用 API 或 Developer Knowledge MCP 伺服器時,請根據需求選擇最合適的方法:

  • AnswerQuery (或 answer_query MCP 工具):最適合一般「How-to」問題、比較產品,以及需要完整摘要和來源文件連結的逐步指南。
  • SearchDocumentChunks (或 search_documents MCP 工具):最適合隨手查確切的 CLI 旗標、程式碼語法、參數名稱或 IAM 權限 (例如 service.resource.verb)。請使用兩到五個特定關鍵字,而非完整問題。

剖析引用和參考資料

呼叫 answerQuery 時,answer 物件會提供精細的歸因資訊,將生成的 answerText 特定區段連結至 references 中的來源段落。

references 中的單一來源文件可在不同文字區段中多次引用,而單一區段也可引用多個參考資料。

附引用來源的回覆範例

下列 JSON 程式碼片段說明 answer 物件,其中有多個引用內容指向不同的文件參考資料:

{
  "answer": {
    "answerText": "You can create a BigQuery dataset using the Google Cloud console, the bq command-line tool, or the client libraries. Each dataset is assigned a region or multi-region location when created.",
    "citations": [
      {
        "startIndex": 0,
        "endIndex": 116,
        "sources": [
          {
            "referenceIndex": 0
          }
        ]
      },
      {
        "startIndex": 117,
        "endIndex": 189,
        "sources": [
          {
            "referenceIndex": 1
          }
        ]
      }
    ],
    "references": [
      {
        "documentReference": {
          "documentChunk": {
            "parent": "documents/docs.cloud.google.com/bigquery/docs/datasets",
            "content": "You can create a dataset in BigQuery using the Google Cloud console, the bq command-line tool, or client libraries...",
            "document": {
              "name": "documents/docs.cloud.google.com/bigquery/docs/datasets",
              "title": "Creating and managing datasets",
              "uri": "https://docs.cloud.google.com/bigquery/docs/datasets"
            }
          }
        }
      },
      {
        "documentReference": {
          "documentChunk": {
            "parent": "documents/docs.cloud.google.com/bigquery/docs/locations",
            "content": "When you create a dataset, you specify the location where your BigQuery data is stored...",
            "document": {
              "name": "documents/docs.cloud.google.com/bigquery/docs/locations",
              "title": "Dataset locations",
              "uri": "https://docs.cloud.google.com/bigquery/docs/locations"
            }
          }
        }
      }
    ]
  }
}

引用資料對應和欄位

如要將引用內容對應至文件來源,請使用下列欄位:

  • answerText:完整生成的自然語言回覆字串。
  • citations:引用片段歸屬的陣列:
    • startIndex:引用片段的起始偏移量 (含),以 UTF-8 位元組為單位。
    • endIndex:引用片段的專屬結尾偏移,以 UTF-8 位元組為單位。
    • sources:引用來源陣列。每個項目都包含一個以 0 為基礎的 referenceIndex,對應 references 陣列中的項目 (例如 referenceIndex: 0 對應至 references[0])。
  • references:用於生成答案的來源文件段落陣列。每個項目都包含 documentReference.documentChunk,其中含有下列中繼資料:
    • document.title:說明文件頁面的標題。
    • document.uri:說明文件頁面的公開網址。
    • document.name:文件的標準資源名稱。
    • content:從文件區塊擷取的文字片段,用於合成答案。
    • parent:父項文件的資源名稱 (例如 documents/docs.cloud.google.com/bigquery/docs/datasets),可傳遞至 documents.get 或 documents.batchGet,以擷取完整文件。

在應用程式中實作引用

如要在用戶端應用程式或 AI 代理管道中,顯示附有引用內容的答案,請按照下列步驟操作:

  1. 擷取引用片段:如要從 answerText 擷取相關片段,請使用 startIndex 和 endIndex 位元組偏移。
  2. 解析來源詳細資料:如要擷取頁面 document.title 和目的地 document.uri,請使用 sources[].referenceIndex 在 references 中隨手查對應的項目。
  3. 顯示內嵌連結或註腳:直接連結至來源頁面、插入註腳編號 (例如 [1] 或 [2])、預覽資訊卡,或指向 document.uri 的可點選超連結。
  4. 視需要擷取完整內容:如果 content 中的摘錄不足,請將 parent 傳遞至 documents.get,以擷取完整的說明文件頁面。
  • UTF-16 語言 (JavaScript 或 TypeScript):先將字串轉換為 UTF-8 位元組,再進行切片。例如,在 Node.js 中使用 Buffer.from(answerText, 'utf-8').subarray(start, end),或在瀏覽器環境中使用 TextEncoder 和 TextDecoder。
  • Python:先將字串編碼為 UTF-8 位元組,再進行切片,然後解碼:answer_text.encode('utf-8')[start:end].decode('utf-8')。
  • 其他語言:如果語言原生以 UTF-8 位元組為字串建立索引 (例如 Go 或 Rust),請直接使用 startIndex 和 endIndex 切割 answerText。否則,請先將字串轉換為 UTF-8 位元組,再進行切片。

後續步驟