Developer Knowledge API を使用すると、Google Developers プロダクトに関する質問を送信し、自然言語で直接回答を受け取ることができます。各クエリに対して、API は Developer Knowledge コーパス(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など)をルックアップする場合に最適です。完全な質問ではなく、2 ~ 5 個の具体的なキーワードを使用します。
引用と参照を解析する
answerQuery を呼び出すと、answer オブジェクトは、生成された answerText の特定のセグメントを references のソース パッセージにリンクする、きめ細かいアトリビューションを提供します。
references の 1 つのソース ドキュメントを、異なるテキスト セグメントで複数回引用できます。また、1 つのセグメントで複数の参照を引用できます。
引用を含むレスポンスの例
次の 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: 引用ソースの配列。各エントリには、references配列内のアイテムに対応する 0 ベースのreferenceIndexが含まれます(たとえば、referenceIndex: 0はreferences[0]にマッピングされます)。
references: 回答の生成に使用されたソース ドキュメントのパッセージの配列。各エントリには、次のメタデータを含むdocumentReference.documentChunkが含まれています。document.title: ドキュメント ページのタイトル。document.uri: ドキュメント ページの公開 URL。document.name: ドキュメントの正規リソース名。content: 回答の合成に使用されたドキュメント チャンクから抽出されたテキスト スニペット。parent: 親ドキュメントのリソース名(documents/docs.cloud.google.com/bigquery/docs/datasetsなど)。documents.getまたはdocuments.batchGetに渡して、ドキュメント全体を取得できます。
アプリケーションに引用を実装する
クライアント アプリケーションまたは AI エージェント パイプラインで引用付きの回答をレンダリングするには:
- 引用されたセグメントを抽出する:
answerTextから関連するスパンをスライスするには、startIndexとendIndexのバイト オフセットを使用します。 - ソースの詳細を解決する: ページ
document.titleと宛先document.uriを取得するには、sources[].referenceIndexを使用してreferencesで対応する項目をルックアップします。 - インライン リンクまたは脚注をレンダリングする: ソースページに直接リンクするには、脚注番号(
[1]や[2]など)、プレビュー カード、またはdocument.uriを指すクリック可能なハイパーリンクを挿入します。 - 必要に応じて完全なコンテキストを取得する:
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 バイトに変換します。
次のステップ
- ドキュメントを検索して取得し、一致するテキスト スニペットまたは Markdown ページ全体を取得します。
- Developer Knowledge MCP サーバーに接続し、
retrieving-developer-knowledgeエージェント スキルをインストールします。 - コーパス リファレンスを参照して、サポートされているすべてのドキュメント ソースを確認する。