クイックスタート: Developer Knowledge API で gcloud CLI を使用する

このクイックスタートでは、Google Cloud CLI を使用して Developer Knowledge API でクエリに回答し、デベロッパー ドキュメントを検索して取得する方法について説明します。

始める前に

gcloud CLI で Developer Knowledge API の使用を開始する前に、次の手順を完了します。

gcloud CLI をインストールして構成する

gcloud CLI をインストールして構成する手順は次のとおりです。

  1. gcloud CLI をインストールしていない場合は、gcloud CLI をインストールします。

  2. gcloud components update コマンドを実行して、最新バージョンであることを確認します。

    gcloud components update
    
  3. gcloud auth login コマンドを実行して、Google Cloud アカウントにログインします。

    gcloud auth login
    
  4. gcloud config set コマンドを実行して、アクティブな Google Cloud プロジェクトを設定します。

    gcloud config set project PROJECT_ID
    

    PROJECT_ID は、Google Cloud プロジェクトの ID に置き換えます。

API を有効にする

Developer Knowledge API を有効にする手順は次のとおりです。

  1. gcloud services enable コマンドを実行して、Google Cloud プロジェクトで Developer Knowledge API を有効にします。

    gcloud services enable developerknowledge.googleapis.com
    

    API を有効または使用するために、特定の Identity and Access Management(IAM)ロールは必要ありません。

  2. 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 サービスに関する質問を直接自然言語で入力して回答を得ることができます。このコマンドは、公式ドキュメント ソースから情報を取得し、参照されたページへの引用を提供します。

質問して回答を生成する手順は次のとおりです。

  1. 次のコマンドを実行して、Cloud Storage バケットの作成方法を尋ねます。

    gcloud developer-knowledge answer-query \
        --query="How do I create a Cloud Storage bucket?"
    
  2. コマンドが生成された回答とソース参照を 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 コマンドを使用します。このコマンドは、ドキュメント コーパスをスキャンし、一致するコンテンツ チャンクとその親ドキュメントのリソース名を返します。

ドキュメント チャンクを検索する手順は次のとおりです。

  1. 次のコマンドを実行して、Cloud Storage バケットの作成に関するドキュメントを検索します。

    gcloud developer-knowledge documents search-chunks \
        --query="How do I create a Cloud Storage bucket?"
    
  2. コマンドが一致するドキュメント チャンクのリストを 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 バケットの作成に関するドキュメントを取得するには、次の手順を行います。

  1. 次のコマンドを実行します。

    gcloud developer-knowledge documents describe \
      documents/docs.cloud.google.com/storage/docs/creating-buckets
    
  2. コマンドがドキュメントのメタデータと完全なマークダウン コンテンツを返すことを確認します。

    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)。

次のステップ