Schritt 1: Projekt und Quell-Repository einrichten

In diesem Schritt richten Sie Ihr Google Cloud -Projekt und Ihre Python-Umgebung in Cloud Shell ein, aktivieren die erforderlichen APIs und weisen die IAM-Rollen (Identity and Access Management) zu, die Sie für die Durchführung der Anleitung benötigen. Außerdem richten Sie ein GitHub-Repository mit den Quellcode-Dateien der App ein, indem Sie das Repository GoogleCloudPlatform/generative-ai forken und klonen. Nachdem Sie diese Schritte abgeschlossen haben, können Sie die Einrichtung überprüfen, indem Sie die App lokal in Cloud Shell ausführen und testen.

Hinweise

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  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 (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 Vertex AI, 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: Vertex AI 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.

      IAM aufrufen
    2. Wählen Sie das Projekt aus.
    3. Klicken Sie auf Zugriffsrechte erteilen.
    4. Geben Sie im Feld Neue Hauptkonten Ihre Nutzer-ID ein. Das ist in der Regel die E‑Mail-Adresse eines Google-Kontos.

    5. Wählen Sie in der Liste Rolle auswählen eine Rolle aus.
    6. Klicken Sie auf Weitere Rolle hinzufügen, wenn Sie weitere Rollen zuweisen möchten.
    7. Klicken Sie auf Speichern.
  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 (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 Vertex AI, 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: Vertex AI 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.

      IAM aufrufen
    2. Wählen Sie das Projekt aus.
    3. Klicken Sie auf Zugriffsrechte erteilen.
    4. Geben Sie im Feld Neue Hauptkonten Ihre Nutzer-ID ein. Das ist in der Regel die E‑Mail-Adresse eines Google-Kontos.

    5. Wählen Sie in der Liste Rolle auswählen eine Rolle aus.
    6. Klicken Sie auf Weitere Rolle hinzufügen, wenn Sie weitere Rollen zuweisen möchten.
    7. Klicken Sie auf Speichern.
  10. Quell-Repository einrichten

    1. Verzweigen Sie das Repository GoogleCloudPlatform/generative-ai auf GitHub. Weitere Informationen zum Forken von Repositories in GitHub

      Repository in GitHub forken

    1. In the Google Cloud console, activate Cloud Shell.

      Activate Cloud Shell

      At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

    2. Führen Sie im Cloud Shell-Terminal die folgenden Befehle aus, um den Fork des Repositorys zu klonen und das Verzeichnis gemini-streamlit-cloudrun als aktives Verzeichnis festzulegen:
      cd
      git clone https://github.com/GIT_USER_NAME/FORK_NAME/
      cd FORK_NAME/gemini/sample-apps/gemini-streamlit-cloudrun

      Ersetzen Sie dabei Folgendes:

      • GIT_USER_NAME: Ihr GitHub-Nutzername
      • FORK_NAME: Der Name des Fork-Repositorys, das Sie gerade in GitHub erstellt haben.
    3. Umgebung und Abhängigkeiten einrichten

      1. Führen Sie im Cloud Shell-Terminal die folgenden Befehle aus, um eine virtuelle Umgebung einzurichten:

        python3 -m venv gemini-streamlit
        source gemini-streamlit/bin/activate
        pip install -r requirements.txt
        
      2. Führen Sie die folgenden Befehle aus, um die für die Vertex AI-Initialisierung erforderlichen Umgebungsvariablen festzulegen:

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

      App lokal testen

      1. Führen Sie die App im Cloud Shell-Terminal mit dem folgenden Befehl aus:

        streamlit run app.py \
          --browser.serverAddress=localhost \
          --server.enableCORS=false \
          --server.enableXsrfProtection=false \
          --server.port 8080
        
      2. Klicken Sie in der Cloud Shell-Taskleiste auf Schaltfläche für Webvorschau und dann auf Vorschau auf Port 8080, um eine Vorschau der App aufzurufen.

        Weitere Informationen zur Verwendung der Webvorschaufunktion finden Sie unter Web-Apps in der Vorschau ansehen.