このクイックスタートでは、Google Cloud CLI を使用して Developer Knowledge API でクエリに回答し、デベロッパー ドキュメントを検索して取得する方法について説明します。
始める前に
gcloud CLI で Developer Knowledge API の使用を開始する前に、次の手順を完了します。
gcloud CLI をインストールして構成する
gcloud CLI をインストールして構成する手順は次のとおりです。
gcloud CLI をインストールしていない場合は、gcloud CLI をインストールします。
gcloud components updateコマンドを実行して、最新バージョンであることを確認します。gcloud components update
gcloud auth loginコマンドを実行して、Google Cloud アカウントにログインします。gcloud auth login
gcloud config setコマンドを実行して、アクティブな Google Cloud プロジェクトを設定します。gcloud config set project PROJECT_ID
PROJECT_IDは、Google Cloud プロジェクトの ID に置き換えます。
API を有効にする
Developer Knowledge API を有効にする手順は次のとおりです。
gcloud services enableコマンドを実行して、Google Cloud プロジェクトで Developer Knowledge API を有効にします。gcloud services enable developerknowledge.googleapis.com
API を有効または使用するために、特定の Identity and Access Management(IAM)ロールは必要ありません。
gcloud services listコマンドを実行して、プロジェクトで API が有効になっていることを確認します。gcloud services list --enabled \ --filter="name:developerknowledge.googleapis.com"出力には、API の名前とタイトルが表示されます。
NAME TITLE developerknowledge.googleapis.com Developer Knowledge API
ドキュメントから回答を生成する
gcloud developer-knowledge answer-query コマンドを使用すると、Google サービスに関する質問を直接自然言語で入力して回答を得ることができます。このコマンドは、公式ドキュメント ソースから情報を取得し、参照されたページへの引用を提供します。
質問して回答を生成する手順は次のとおりです。
次のコマンドを実行して、Cloud Storage バケットの作成方法を尋ねます。
gcloud developer-knowledge answer-query \ --query="How do I create a Cloud Storage bucket?"コマンドが生成された回答とソース参照を YAML 形式で返すことを確認します。
answer: answerText: |- To create a Cloud Storage bucket, you can use the Google Cloud console, the gcloud CLI (`gcloud storage buckets create`), client libraries, or the REST API... citations: - endIndex: 158 sources: - referenceIndex: 0 startIndex: 0 references: - documentReference: documentChunk: content: |- This document shows you how to create a Cloud Storage [bucket](https://docs.cloud.google.com/storage/docs/buckets)... document: dataSource: docs.cloud.google.com name: documents/docs.cloud.google.com/storage/docs/creating-buckets title: Create a bucket uri: https://docs.cloud.google.com/storage/docs/creating-buckets parent: documents/docs.cloud.google.com/storage/docs/creating-buckets出力の
answerオブジェクトには次のフィールドが含まれます。answerText: クエリに対する生成された自然言語の回答。citations:answerText内のバイトオフセット範囲startIndexとendIndex。referenceIndexによってreferences内のサポート エントリにマッピングされます。references: 回答の生成に使用されたソース ドキュメントのチャンクとメタデータ(document、parentなど)。
ドキュメント チャンクを検索する
生成された回答ではなく、Google のデベロッパー ドキュメント内の特定のテキスト抜粋を見つけるには、gcloud developer-knowledge documents search-chunks コマンドを使用します。このコマンドは、ドキュメント コーパスをスキャンし、一致するコンテンツ チャンクとその親ドキュメントのリソース名を返します。
ドキュメント チャンクを検索する手順は次のとおりです。
次のコマンドを実行して、Cloud Storage バケットの作成に関するドキュメントを検索します。
gcloud developer-knowledge documents search-chunks \ --query="How do I create a Cloud Storage bucket?"コマンドが一致するドキュメント チャンクのリストを YAML 形式で返すことを確認します。
--- content: |- This document shows you how to create a Cloud Storage [bucket](https://docs.cloud.google.com/storage/docs/buckets). If not otherwise specified in your request, buckets are created in the [US multi-region](https://docs.cloud.google.com/storage/docs/locations)... document: contentLengthBytes: 31842 dataSource: docs.cloud.google.com description: World-wide storage and retrieval of data in Google Cloud. name: documents/docs.cloud.google.com/storage/docs/creating-buckets title: Create a bucket updateTime: '2026-09-10T20:05:41Z' uri: https://docs.cloud.google.com/storage/docs/creating-buckets view: DOCUMENT_VIEW_BASIC id: c1 parent: documents/docs.cloud.google.com/storage/docs/creating-buckets relevanceScore: 0.856608出力の各チャンクには、次のフィールドが含まれます。
content: ドキュメントから一致したテキスト スニペット。document: ソース ドキュメントに関するメタデータ(title、description、uri、dataSource、updateTimeなど)。id: ドキュメント内のチャンクの識別子。parent: 親ドキュメントのリソース名。この値をdescribeコマンドに渡して、ドキュメント全体を取得できます。relevanceScore: チャンクと検索クエリの関連性スコア。
ドキュメントを取得する
関連するドキュメント チャンクが見つかったら、検索結果の parent フィールドを使用して、ドキュメントの完全な Markdown コンテンツを取得します。
ドキュメントのリソース名を指定して gcloud developer-knowledge documents describe コマンドを実行します。たとえば、Cloud Storage バケットの作成に関するドキュメントを取得するには、次の手順を行います。
次のコマンドを実行します。
gcloud developer-knowledge documents describe \ documents/docs.cloud.google.com/storage/docs/creating-buckets
コマンドがドキュメントのメタデータと完全なマークダウン コンテンツを返すことを確認します。
content: | This document shows you how to create a Cloud Storage [bucket](https://docs.cloud.google.com/storage/docs/buckets). If not otherwise specified in your request, buckets are created in the [`US` multi-region](https://docs.cloud.google.com/storage/docs/locations) with a default storage class of [Standard storage](https://docs.cloud.google.com/storage/docs/storage-classes) and have a seven-day [soft delete](https://docs.cloud.google.com/storage/docs/soft-delete) retention duration... contentLengthBytes: 31842 dataSource: docs.cloud.google.com description: World-wide storage and retrieval of data in Google Cloud. name: documents/docs.cloud.google.com/storage/docs/creating-buckets title: Create a bucket updateTime: '2026-09-10T20:05:41Z' uri: https://docs.cloud.google.com/storage/docs/creating-buckets view: DOCUMENT_VIEW_CONTENT出力には次のフィールドが含まれます。
content: ドキュメントの完全なマークダウン テキスト。contentLengthBytes: ドキュメント コンテンツの合計サイズ(バイト単位)。dataSource: ドキュメントをホストするドキュメント ドメイン。description: ドキュメントの簡単な要約。name: ドキュメントの一意のリソース名。title: ドキュメントのタイトル。updateTime: ドキュメントが最後に更新されたときのタイムスタンプ。uri: ドキュメント ページの一般公開 URL。view: 返されるドキュメント ビュー(DOCUMENT_VIEW_CONTENT、DOCUMENT_VIEW_BASIC、またはDOCUMENT_VIEW_FULL)。
次のステップ
- ドキュメントから回答を生成する方法を確認する。
- 検索結果のフィルタ、ページネーション、ドキュメント ビューの指定方法を確認する。
- コーパス リファレンスで、含まれているドキュメントの完全なリストを確認する。
- 使用可能なすべてのコマンドとフラグの詳細については、
gcloud developer-knowledgeリファレンスをご覧ください。 - API メソッドの完全な仕様については、REST API リファレンスをご覧ください。
- Google Antigravity で Model Context Protocol(MCP)サーバーを設定する方法について説明します。