שלב 1: הגדרת הפרויקט ומאגר המקור

בשלב הזה, תגדירו את הפרויקט ב- Google Cloud ואת סביבת Python ב-Cloud Shell, תפעילו את ממשקי ה-API הנדרשים ותקצו את התפקידים בניהול הזהויות והרשאות הגישה (IAM) שנדרשים להשלמת המדריך. בנוסף, אתם מגדירים מאגר ב-GitHub שמכיל את קובצי המקור של האפליקציה על ידי יצירת עותק (forking) ושכפול (cloning) של המאגר GoogleCloudPlatform/generative-ai. אחרי שמבצעים את השלבים האלה, מאמתים את ההגדרה על ידי הפעלה ובדיקה של האפליקציה באופן מקומי ב-Cloud Shell.

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

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Agent Platform, Compute Engine, Artifact Registry, Identity-Aware Proxy (IAP), Cloud Run Admin, Cloud Build, Identity and Access Management (IAM) API, and Gemini for Google Cloud APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  5. Make sure that you have the following role or roles on the project: Agent Platform User, Cloud Build Editor, Cloud Run Admin, Artifact Registry Admin, Compute Load Balancer Admin, Service Account User, IAP Policy Admin, OAuth Config Editor, and Service Usage Admin.

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Agent Platform, Compute Engine, Artifact Registry, Identity-Aware Proxy (IAP), Cloud Run Admin, Cloud Build, Identity and Access Management (IAM) API, and Gemini for Google Cloud APIs.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the APIs

  9. Make sure that you have the following role or roles on the project: Agent Platform User, Cloud Build Editor, Cloud Run Admin, Artifact Registry Admin, Compute Load Balancer Admin, Service Account User, IAP Policy Admin, OAuth Config Editor, and Service Usage Admin.

    Check for the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. In the Principal column, find all rows that identify you or a group that you're included in. To learn which groups you're included in, contact your administrator.

    4. For all rows that specify or include you, check the Role column to see whether the list of roles includes the required roles.

    Grant the roles

    1. In the Google Cloud console, go to the IAM page.

      Go to IAM
    2. Select the project.
    3. Click Grant access.
    4. In the New principals field, enter your user identifier. This is typically the email address for a Google Account.

    5. Click Select a role, then search for the role.
    6. To grant additional roles, click Add another role and add each additional role.
    7. Click Save.

הגדרת מאגר המקור

  1. ב-GitHub, יוצרים Fork של מאגר GoogleCloudPlatform/generative-ai. מידע נוסף על יצירת Fork של מאגרים ב-GitHub

    יצירת Fork של המאגר ב-GitHub

  1. במסוף Google Cloud , מפעילים את Cloud Shell.

    הפעלת Cloud Shell

    בחלק התחתון של Google Cloud המסוף יתחיל סשן של Cloud Shell ותופיע הודעה של שורת הפקודה. Cloud Shell היא סביבת מעטפת שבה ה-CLI של Google Cloud מותקן ומוגדרים ערכים לפרויקט הקיים. הסשן יופעל תוך כמה שניות.

  2. בטרמינל של Cloud Shell, מריצים את הפקודות הבאות כדי לשכפל את המאגר המפוצל ולהגדיר את הספרייה gemini-streamlit-cloudrun כספרייה הפעילה:
    cd
    git clone https://github.com/GIT_USER_NAME/FORK_NAME/
    cd FORK_NAME/gemini/sample-apps/gemini-streamlit-cloudrun

    מחליפים את מה שכתוב בשדות הבאים:

    • GIT_USER_NAME: שם המשתמש שלכם ב-GitHub.
    • FORK_NAME: השם של מאגר ה-fork שיצרתם ב-GitHub.

הגדרת הסביבה ויחסי התלות

  1. בטרמינל של Cloud Shell, מריצים את הפקודות הבאות כדי להגדיר סביבה וירטואלית:

    python3 -m venv gemini-streamlit
    source gemini-streamlit/bin/activate
    pip install -r requirements.txt
    
  2. מריצים את הפקודות הבאות כדי להגדיר את משתני הסביבה שנדרשים לאתחול של Agent Platform:

    export GCP_PROJECT=$GOOGLE_CLOUD_PROJECT
    export GCP_REGION='us-central1' 
    

בדיקת האפליקציה באופן מקומי

  1. בטרמינל של Cloud Shell, מריצים את האפליקציה באמצעות הפקודה הבאה:

    streamlit run app.py \
      --browser.serverAddress=localhost \
      --server.enableCORS=false \
      --server.enableXsrfProtection=false \
      --server.port 8080
    
  2. כדי לראות תצוגה מקדימה של האפליקציה, בסרגל המשימות של Cloud Shell, לוחצים על כפתור לתצוגה מקדימה של אתר ואז על תצוגה מקדימה ביציאה 8080.

    מידע נוסף על השימוש בתכונה 'תצוגה מקדימה באינטרנט' זמין במאמר תצוגה מקדימה של אפליקציות אינטרנט.