מדריך למתחילים: שימוש ב-CLI של gcloud עם Developer Knowledge API

במדריך למתחילים הזה נסביר איך לענות על שאילתות, לחפש ולאחזר מאמרי עזרה למפתחים באמצעות Developer Knowledge API ו-Google Cloud CLI.

לפני שמתחילים

לפני שמתחילים להשתמש ב-Developer Knowledge API עם ה-CLI של gcloud, צריך לבצע את השלבים הבאים.

התקנה והגדרה של ה-CLI של gcloud

כדי להתקין ולהגדיר את ה-CLI של gcloud:

  1. אם לא התקנתם את ה-CLI של gcloud, התקינו אותו.

  2. מריצים את הפקודה gcloud components update כדי לוודא שמותקנת הגרסה העדכנית:

    gcloud components update
    
  3. נכנסים לחשבון Google Cloud באמצעות הפקודה gcloud auth login:

    gcloud auth login
    
  4. מגדירים את הפרויקט הפעיל ב-Google Cloud באמצעות הפעלת הפקודה gcloud config set:

    gcloud config set project PROJECT_ID
    

    מחליפים את PROJECT_ID במזהה הפרויקט ב-Google Cloud.

הפעלת ה-API

כדי להפעיל את Developer Knowledge API:

  1. מפעילים את Developer Knowledge API בפרויקט בענן ב-Google Cloud באמצעות הפקודה gcloud services enable:

    gcloud services enable developerknowledge.googleapis.com
    

    כדי להפעיל את ה-API או להשתמש בו, לא צריך תפקידים ספציפיים של ניהול זהויות והרשאות גישה (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: טווחי היסט בייטים, 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: הטקסט המלא של המסמך ב-Markdown.
    • ‫contentLengthBytes: הגודל הכולל של תוכן המסמך בבייטים.
    • ‫dataSource: הדומיין שבו מתארח המסמך.
    • ‫description: סיכום קצר של המסמך.
    • ‫name: שם המשאב הייחודי של המסמך.
    • ‫title: שם המסמך.
    • ‫updateTime: חותמת הזמן של העדכון האחרון של המסמך.
    • ‫uri: כתובת ה-URL הציבורית של דף התיעוד.
    • ‫view: תצוגת המסמך שהוחזרה (DOCUMENT_VIEW_CONTENT, DOCUMENT_VIEW_BASIC או DOCUMENT_VIEW_FULL).

המאמרים הבאים