Developer Knowledge API 可讓您詢問 Google Developers 產品相關問題,並直接獲得自然語言回覆。針對每項查詢,API 會從開發人員知識語料庫中擷取內容,組成回覆 (在 API 參考資料中稱為基準生成),並附上相關說明文件頁面的引用資訊。
事前準備
為偏好的工具設定環境:
gcloud
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_queryMCP 工具):最適合一般 How-to 問題、產品比較,以及需要完整摘要和來源文件連結的逐步指南。SearchDocumentChunks(或search_documentsMCP 工具):最適合查詢確切的 CLI 旗標、程式碼語法、參數名稱或 IAM 權限 (例如service.resource.verb)。請使用兩到五個特定關鍵字,而非完整問題。
後續步驟
- 搜尋及擷取文件,以擷取相符的文字片段或完整 Markdown 頁面。
- 連線至 Developer Knowledge MCP 伺服器,並安裝
retrieving-developer-knowledge代理技能。 - 瀏覽語料庫參考資料,查看所有支援的文件來源。