Passaggio 1: configura il progetto e il repository di origine

In questo passaggio, configuri il tuo Google Cloud progetto e l'ambiente Python in Cloud Shell, abiliti le API richieste e assegni i ruoli Identity and Access Management (IAM) necessari per completare il tutorial. Configuri anche un repository GitHub contenente i file di origine dell'app creando un fork e clonando il GoogleCloudPlatform/generative-ai repository. Dopo aver completato questi passaggi, verifichi la configurazione eseguendo e testando l'app in locale in Cloud Shell.

Prima di iniziare

  1. Accedi al tuo Google Cloud account. Se non conosci Google Cloud, crea un account per valutare le prestazioni dei nostri prodotti in scenari reali. I nuovi clienti ricevono anche 300 $di crediti senza costi per l'esecuzione, il test e il deployment dei workload.
  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 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.

      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 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.

      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.

Configura il repository di origine

  1. In GitHub, crea un fork del repository GoogleCloudPlatform/generative-ai. Scopri di più sulla creazione di fork di repository in GitHub.

    Crea un fork del repository in GitHub

  1. Nella Google Cloud console, attiva Cloud Shell.

    Attiva Cloud Shell

    Nella parte inferiore della Google Cloud console, viene avviata una sessiona di Cloud Shell e viene visualizzato un prompt della riga di comando. Cloud Shell è un ambiente shell con Google Cloud CLI già inclusa e installata e con valori già impostati per il progetto corrente. L'inizializzazione della sessione può richiedere alcuni secondi.

  2. Nel terminale Cloud Shell, esegui i comandi seguenti per clonare il repository con fork e impostare la gemini-streamlit-cloudrun directory come directory attiva:
    cd
    git clone https://github.com/GIT_USER_NAME/FORK_NAME/
    cd FORK_NAME/gemini/sample-apps/gemini-streamlit-cloudrun

    Sostituisci quanto segue:

    • GIT_USER_NAME: il tuo nome utente GitHub.
    • FORK_NAME: il nome del repository con fork che hai appena creato in GitHub.

Configura l'ambiente e le dipendenze

  1. Nel terminale Cloud Shell, esegui i comandi seguenti per configurare un ambiente virtuale:

    python3 -m venv gemini-streamlit
    source gemini-streamlit/bin/activate
    pip install -r requirements.txt
    
  2. Esegui i comandi seguenti per impostare le variabili di ambiente necessarie per l'inizializzazione di Vertex AI:

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

Testa l'app in locale

  1. Dal terminale Cloud Shell, esegui l'app eseguendo il comando seguente:

    streamlit run app.py \
      --browser.serverAddress=localhost \
      --server.enableCORS=false \
      --server.enableXsrfProtection=false \
      --server.port 8080
    
  2. Per visualizzare l'anteprima dell'app, nella barra delle attività di Cloud Shell, fai clic su Pulsante Anteprima web, quindi fai clic su Anteprima sulla porta 8080.

    Per saperne di più sull'utilizzo della funzionalità Anteprima web, vedi Visualizzare l'anteprima delle app web.