在此步骤中,您将在 Cloud Shell 中设置 Google Cloud 项目和 Python 环境,启用所需的 API,并分配完成本教程所需的 Identity and Access Management (IAM) 角色。您还需要通过复刻和克隆 GoogleCloudPlatform/generative-ai 代码库来设置一个包含应用源代码文件的 GitHub 代码库。完成这些步骤后,您可以在 Cloud Shell 中本地运行并测试应用,以验证设置。
准备工作
-
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 theresourcemanager.projects.createpermission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
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
serviceusage.services.enablepermission. If you created the project, then you likely already have this permission through the Owner role (roles/owner). Otherwise, you can get this permission through the Service Usage Admin role (roles/serviceusage.serviceUsageAdmin). Learn how to grant roles.-
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
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
-
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.
- 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
-
In the Google Cloud console, go to the IAM page.
Go to IAM - Select the project.
- Click Grant access.
-
In the New principals field, enter your user identifier. This is typically the identifier for a user in a workforce identity pool. For details, see Represent workforce pool users in IAM policies, or contact your administrator.
- Click Select a role, then search for the role.
- To grant additional roles, click Add another role and add each additional role.
- Click Save.
-
设置源代码库
在 GitHub 中,派生 GoogleCloudPlatform/generative-ai 代码库。详细了解如何在 GitHub 中派生代码库。
-
在 Google Cloud 控制台中,激活 Cloud Shell。
Cloud Shell 会话随即会在 Google Cloud 控制台的底部启动,并显示命令行提示符。Cloud Shell 是一个已安装 Google Cloud CLI 且已为当前项目设置值的 Shell 环境。该会话可能需要几秒钟时间来完成初始化。
- 在 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 中创建的分支代码库的名称。
设置环境和依赖项
在 Cloud Shell 终端中,运行以下命令以设置虚拟环境:
python3 -m venv gemini-streamlit source gemini-streamlit/bin/activate pip install -r requirements.txt运行以下命令以设置 Agent Platform 初始化所需的环境变量:
export GCP_PROJECT=$GOOGLE_CLOUD_PROJECT export GCP_REGION='us-central1'
在本地测试应用
在 Cloud Shell 终端中,运行以下命令来运行应用:
streamlit run app.py \ --browser.serverAddress=localhost \ --server.enableCORS=false \ --server.enableXsrfProtection=false \ --server.port 8080如需预览应用,请在 Cloud Shell 任务栏中点击
,然后点击在端口 8080 上预览。
如需详细了解如何使用网页预览功能,请参阅预览 Web 应用。