步驟 1:設定專案和來源存放區

在本步驟中,您將在 Cloud Shell 中設定 Google Cloud 專案和 Python 環境、啟用必要 API,並指派完成本教學課程所需的 Identity and Access Management (IAM) 角色。您也需要設定包含應用程式原始碼檔案的 GitHub 存放區,方法是分叉並複製 GoogleCloudPlatform/generative-ai 存放區。完成這些步驟後,請在 Cloud Shell 中在本機執行及測試應用程式,驗證設定。

事前準備

  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.

    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.

    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
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  6. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    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.

    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
    2. 選取專案。
    3. 按一下「授予存取權」
    4. 在「New principals」(新增主體) 欄位中,輸入您的使用者 ID。 這通常是 Google 帳戶的電子郵件地址。

    5. 在「Select a role」(選取角色) 清單中,選取角色。
    6. 如要授予其他角色,請按一下 「新增其他角色」,然後新增每個其他角色。
    7. 按一下 [Save]
  10. 設定來源存放區

    1. 在 GitHub 中,建立 GoogleCloudPlatform/generative-ai 存放區的分支。進一步瞭解如何在 GitHub 中分叉存放區

      在 GitHub 中建立存放區分支

    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. 在 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:您剛在 GitHub 中建立的分支存放區名稱。
    3. 設定環境和依附元件

      1. 在 Cloud Shell 終端機中執行下列指令,設定虛擬環境:

        python3 -m venv gemini-streamlit
        source gemini-streamlit/bin/activate
        pip install -r requirements.txt
        
      2. 執行下列指令,設定 Vertex AI 初始化所需的環境變數:

        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」

        如要進一步瞭解如何使用網頁預覽功能,請參閱「預覽網頁應用程式」。