คู่มือเริ่มใช้งานฉบับย่อ: ใช้ gcloud CLI กับ Developer Knowledge API

ข้อมูลเบื้องต้นนี้แสดงวิธีตอบคำค้นหา รวมถึงค้นหาและดึงข้อมูล เอกสารประกอบสำหรับนักพัฒนาซอฟต์แวร์ด้วย Developer Knowledge API โดยใช้ Google Cloud CLI

ก่อนเริ่มต้น

ก่อนเริ่มใช้ Developer Knowledge API กับ gcloud CLI ให้ทำตามขั้นตอนต่อไปนี้

ติดตั้งและกำหนดค่า gcloud CLI

หากต้องการติดตั้งและกำหนดค่า gcloud CLI ให้ทำตามขั้นตอนต่อไปนี้

  1. หากยังไม่ได้ติดตั้ง gcloud CLI ให้ติดตั้ง gcloud CLI

  2. เรียกใช้คำสั่ง gcloud components update เพื่อ ตรวจสอบว่าคุณใช้เวอร์ชันล่าสุด

    gcloud components update
    
  3. ลงชื่อเข้าใช้บัญชี Google Cloud โดยเรียกใช้คำสั่ง gcloud auth login ดังนี้

    gcloud auth login
    
  4. ตั้งค่าโปรเจ็กต์ที่อยู่ในระบบคลาวด์ Google ที่ใช้งานอยู่โดยเรียกใช้gcloud config set คำสั่งต่อไปนี้

    gcloud config set project PROJECT_ID
    

    แทนที่ PROJECT_ID ด้วยรหัสของโปรเจ็กต์ Google Cloud

เปิดใช้ API

หากต้องการเปิดใช้ Developer Knowledge API ให้ทำตามขั้นตอนต่อไปนี้

  1. เปิดใช้ Developer Knowledge API ในโปรเจ็กต์ที่อยู่ในระบบคลาวด์ Google โดยเรียกใช้คำสั่ง gcloud services enable

    gcloud services enable developerknowledge.googleapis.com
    

    คุณไม่จำเป็นต้องมีบทบาท Identity and Access Management (IAM) ที่เฉพาะเจาะจงเพื่อเปิดใช้ หรือใช้ API

  2. ยืนยันว่าได้เปิดใช้ API สำหรับโปรเจ็กต์โดยเรียกใช้คำสั่ง gcloud services list ดังนี้

    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. เรียกใช้คำสั่งต่อไปนี้เพื่อถามวิธีสร้าง Bucket ของ 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: ช่วงออฟเซ็ตไบต์ startIndex และ endIndex ใน answerText ที่แมปกับรายการที่รองรับใน references โดย referenceIndex
    • references: ชิ้นส่วนเอกสารประกอบต้นฉบับและข้อมูลเมตา รวมถึงdocumentและparentที่ใช้สร้างคำตอบ

ค้นหา Chunk ของเอกสาร

หากต้องการค้นหาข้อความที่เฉพาะเจาะจงในเอกสารประกอบสำหรับนักพัฒนาซอฟต์แวร์ของ 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 จากผลการค้นหาเหล่านั้น เพื่อดึงเนื้อหามาร์กดาวน์ทั้งหมดของเอกสาร

เรียกใช้คำสั่ง gcloud developer-knowledge documents describe พร้อมชื่อทรัพยากรของเอกสาร เช่น หากต้องการดึงข้อมูลเอกสารเกี่ยวกับการสร้าง Bucket ใน 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)

ขั้นตอนถัดไป