คู่มือเริ่มใช้งานฉบับย่อ: ใช้ 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

เปิดใช้ 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. เรียกใช้คำสั่งต่อไปนี้เพื่อถามวิธีสร้างที่เก็บข้อมูล 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 ที่ใช้สร้างคำตอบ

ค้นหาข้อมูลที่แยกย่อยจากเอกสาร

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

    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)

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