Etapa 1: configurar o projeto e o repositório de origem

Nesta etapa, você vai configurar seu Google Cloud projeto e o ambiente Python no Cloud Shell, ativar as APIs necessárias e atribuir o Identity and Access Management (IAM) necessários para concluir o tutorial. Você também configura um repositório do GitHub que contém os arquivos de origem do app, bifurcando e clonando o GoogleCloudPlatform/generative-ai repositório. Depois de concluir essas etapas, verifique a configuração executando e testando o app localmente no Cloud Shell.

Antes de começar

  1. Faça login na sua Google Cloud conta do. Se você começou a usar o Google Cloud, crie uma conta para avaliar o desempenho dos nossos produtos em situações reais. Clientes novos também recebem US $300 em créditos para executar, testar e implantar cargas de trabalho.
  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.

Configurar o repositório de origem

  1. No GitHub, bifurque o repositório GoogleCloudPlatform/generative-ai. Saiba mais sobre como bifurcar repositórios no GitHub.

    Bifurque o repositório no GitHub

  1. No Google Cloud console, ative o Cloud Shell.

    Ativar o Cloud Shell

    Na parte inferior do Google Cloud console, uma sessão do Cloud Shell é iniciada e exibe um prompt de linha de comando. O Cloud Shell é um ambiente shell com a Google Cloud CLI já instalada e com valores já definidos para o projeto atual. A inicialização da sessão pode levar alguns segundos.

  2. No terminal do Cloud Shell, execute os comandos a seguir para clonar o repositório bifurcado e definir o gemini-streamlit-cloudrun diretório como o ativo:
    cd
    git clone https://github.com/GIT_USER_NAME/FORK_NAME/
    cd FORK_NAME/gemini/sample-apps/gemini-streamlit-cloudrun

    Substitua:

    • GIT_USER_NAME: seu nome de usuário do GitHub
    • FORK_NAME: o nome do repositório bifurcado que você acabou de criar no GitHub.

Configurar o ambiente e as dependências

  1. No terminal do Cloud Shell, execute os comandos a seguir para configurar um ambiente virtual:

    python3 -m venv gemini-streamlit
    source gemini-streamlit/bin/activate
    pip install -r requirements.txt
    
  2. Execute os comandos a seguir para definir as variáveis de ambiente necessárias para a inicialização da plataforma de agentes:

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

testar o aplicativo localmente

  1. No terminal do Cloud Shell, execute o app com o seguinte comando:

    streamlit run app.py \
      --browser.serverAddress=localhost \
      --server.enableCORS=false \
      --server.enableXsrfProtection=false \
      --server.port 8080
    
  2. Para visualizar o app, na barra de tarefas do Cloud Shell, clique em Botão de visualização da Web e depois em Visualizar na porta 8080.

    Para saber mais sobre como usar o recurso de visualização na Web, consulte Visualizar apps da Web.