实验设置说明和要求
保护您的账号和进度。请务必在无痕浏览器窗口中,使用实验凭证运行此实验。

使用 Gemini CLI 加速执行各项任务

实验 1 小时 30 分钟 universal_currency_alt 5 个点数 show_chart 入门级
info 此实验可能会提供 AI 工具来支持您学习。
此内容尚未针对移动设备进行优化。
为获得最佳体验,请在桌面设备上访问通过电子邮件发送的链接。

概览

Gemini CLI 是一款免费的开源 AI 智能体,让您可以直接从命令行界面中使用 Gemini。它与您的现有工具相集成,帮助您处理各种棘手的任务,例如修复 bug、创建新功能和改善测试。虽然它有着极为出色的编程性能,但也可以作为一个多功能的本地助理,帮助您履行日常任务,例如生成内容、解决问题、开展研究或管理各种待办事项。

目标

在本实验中,您将获得 Gemini CLI 的入门级实操体验。

学习内容如下:

  • 安装并运行 Gemini CLI。
  • 使用 Gemini CLI 解释代码。
  • 配置 Gemini CLI 并提供上下文,以便您得到符合自身需求的回答。
  • 创建自定义斜杠命令,以重复使用提示。
  • 使用 Gemini CLI 工具安全地更改本地文件系统。
  • 使用 Gemini CLI 构建和修改应用。

设置和要求

点击“开始实验”按钮前的注意事项

注意:请阅读以下说明。

实验是计时的,并且您无法暂停实验。计时器在您点击开始实验后即开始计时,显示 Google Cloud 资源可供您使用多长时间。

此 Google Skills 实操实验可让您在真实的云环境中开展实验活动,免受模拟或演示环境的局限。为此,我们会向您提供新的临时凭证,您可以在该实验的规定时间内通过此凭证登录和访问 Google Cloud。

所需条件

为完成此实验,您需要:

  • 能够使用标准的互联网浏览器(建议使用 Chrome 浏览器)。
  • 为完成实验留出时间。
注意:如果您有自己的个人 Google Cloud 账号或项目,请不要在此实验中使用。 注意:如果您使用的是 Pixelbook,请在无痕式窗口中运行此实验。

如何开始实验并登录控制台

  1. 点击开始实验按钮。如果该实验需要付费,系统会打开一个弹出式窗口供您选择付款方式。左侧面板中包含您在进行该实验时必须使用的临时凭证。

    凭据面板

  2. 复制用户名,然后点击打开 Google 控制台。 该实验会启动资源并打开另一个标签页,显示选择账号页面。

    注意:请在单独的窗口打开标签页,让它们并排显示。
  3. 在“选择账号”页面中点击使用其他账号。“登录”页面即会打开。

    “选择账号”对话框,其中突出显示了“使用其他账号”选项

  4. 粘贴您从“连接详情”面板复制的用户名,然后复制并粘贴相应的密码。

注意:您必须使用“连接详情”面板中的凭证,请勿使用您的 Google Skills 凭证。如果您拥有自己的 Google Cloud 账号,请不要在此实验中使用,以免产生费用。
  1. 继续在后续页面中点击以完成相应操作:
  • 接受条款及条件。
  • 由于这是临时账号,请勿添加账号恢复选项或双重验证。
  • 请勿注册免费试用。

片刻之后,系统会在此标签页中打开 Cloud 控制台。

注意:您可以点击左上角的导航菜单来查看列有 Google Cloud 产品和服务的菜单。 Cloud 控制台菜单

激活 Google Cloud Shell

Google Cloud Shell 是一种装有开发者工具的虚拟机。它提供了一个永久性的 5GB 主目录,并且在 Google Cloud 上运行。

Google Cloud Shell 提供了可用于访问您的 Google Cloud 资源的命令行工具。

  1. 在 Cloud 控制台右上角的工具栏中,点击“打开 Cloud Shell”按钮。

    突出显示的 Cloud Shell 图标

  2. 点击继续

预配并连接到环境需要一些时间。如果您连接成功,即表示您已通过身份验证,且项目 ID 会被设为您的 PROJECT_ID。例如:

在 Cloud Shell 终端中突出显示的项目 ID

gcloud 是 Google Cloud 的命令行工具。它会预先安装在 Cloud Shell 上,且支持 Tab 自动补全功能。

  • 您可以通过以下命令列出有效的账号名称:
gcloud auth list

输出:

Credentialed accounts: - @.com (active)

输出示例

Credentialed accounts: - google1623327_student@qwiklabs.net
  • 您可以通过以下命令列出项目 ID:
gcloud config list project

输出:

[core] project =

输出示例

[core] project = qwiklabs-gcp-44776a13dea667a6 注意:有关 gcloud 的完整文档,请参阅 gcloud CLI 概览指南

任务 1. 安装并运行 Gemini CLI

在此任务中,您将安装并运行 Gemini CLI。

在 Cloud Shell 中试用 Gemini CLI

在 Cloud Shell 中可直接使用 Gemini CLI,无需进行额外配置。

  1. 为启动 Gemini CLI,请在 Cloud Shell 中运行以下命令:

    gemini

    Cloud Shell 中已安装了某个版本的 Gemini CLI。

    Cloud Shell 中的 Gemini CLI

    首次运行 Gemini CLI 时,它可能会从当前版本自动更新为最新的稳定版本。

  2. 如果系统询问此项目的身份验证方式,请使用下拉箭头选择 Use Cloud Shell user credentials,然后按 Enter 键或 Return 键。

  3. 为查看可用命令,请在 Gemini CLI 中运行以下命令,然后按 Enter 键或 Return 键:

    /help

    Gemini CLI 中以斜杠 (/) 开头的命令称为斜杠命令。若要运行斜杠命令,必须按 Enter 键或 Return 键。

    /help 会列出您可以发送给 Gemini CLI 的命令,还会介绍如何运行 shell 命令或如何提供文件以丰富上下文。

    Gemini CLI 需要验证身份,通过后才能与 Gemini 通信。

  4. 请运行以下斜杠命令,查看当前的身份验证方法:

    /auth

    当前的身份验证方法是使用 Cloud Shell 用户凭证。Gemini CLI 会自动使用已登录用户的凭证。

  5. 请按 Esc 键退出身份验证对话框。

  6. 请运行以下斜杠命令退出 Gemini CLI:

    /quit

在虚拟机终端中安装 Gemini CLI

这次,您要登录一个虚拟机,并在虚拟机的命令行界面中安装 Gemini CLI。系统已为您创建了一个名为 的虚拟机。

  1. 在 Cloud Shell 中,建立与虚拟机的 SSH 连接:

    gcloud compute ssh {{{project_0.startup_script.vm_name | VM_NAME}}} --zone={{{project_0.default_zone | ZONE}}}
  2. 必要时,按 Enter 键或 Return 键指定每个输入项的默认值。

    您的 Cloud Shell 会话现在正在虚拟机内运行。

  3. 运行以下命令:

    gemini

    默认情况下,虚拟机上没有安装 Gemini CLI,因此系统找不到该命令。

    您可以通过几种方式运行 Gemini CLI:

    • 您可以采用标准安装方式,使用 npm 进行全局安装后运行它,也可以使用 npx 直接执行 NPM 中的某个版本(不进行全局安装)。
    • 您可以在容器沙盒中运行 Gemini CLI。
    • 您可以克隆仓库并基于源代码运行它。若想为 Gemini CLI 代码库贡献代码,则适合选择此方式。
    • 您可以使用 npx 运行 GitHub 仓库中的最新提交版本。

    若要使用 npmnpx,您需要安装 Node.js。若要控制所使用的 Node.js 版本,还可以安装 nvm

  4. 请运行以下命令安装 nvm:

    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash source ~/.bashrc

    该安装脚本会安装 nvm,并将所需的设置添加到 .bashrc 文件中。随后加载 .bashrc 文件,将这些设置添加到当前 shell 会话。

  5. 请运行以下命令,使用 nvm 安装 Node 的最新稳定版本:

    nvm install node

    Node 安装脚本也会安装 npm。

    现在,Node 的最新稳定版本和 npm 已安装完毕,接下来可以安装 Gemini CLI 了。

  6. 请运行以下命令安装 Gemini CLI:

    npm install -g @google/gemini-cli@0.9.0

运行已安装的 Gemini CLI

Gemini CLI 首次运行时,会显示身份验证选项。Cloud Shell 用户凭证选项不再可用,因为我们是在虚拟机内的命令行界面中运行的。

通过设置 GEMINI_API_KEY 环境变量,可使用 Google AI Studio 提供的 API 密钥运行 Gemini。

若使用 Google 账号登录,您必须提供 GOOGLE_CLOUD_PROJECT 环境变量来指定当下使用的项目,然后 Gemini CLI 会提示您登录。

若使用 Vertex AI 登录,您可以获取 Google Cloud API 密钥并将其传递给名为 GOOGLE_API_KEY 的环境变量,也可以使用应用默认凭证代替 API 密钥并导出 GOOGLE_CLOUD_PROJECTGOOGLE_CLOUD_LOCATION 环境变量。

  1. 请运行以下命令使用 Vertex AI 在项目目录中运行 Gemini CLI:

    export GOOGLE_CLOUD_PROJECT={{{project_0.project_id | PROJECT_ID}}} export GOOGLE_CLOUD_LOCATION={{{project_0.default_region | REGION}}} export GEMINI_MODEL=gemini-2.5-flash echo "export GOOGLE_CLOUD_PROJECT={{{project_0.project_id | PROJECT_ID}}}" >> ~/.bashrc echo "export GOOGLE_CLOUD_LOCATION={{{project_0.default_region | REGION}}}" >> ~/.bashrc echo "export GEMINI_MODEL=gemini-2.5-flash" >> ~/.bashrc mkdir ~/project1 cd ~/project1 gemini

    这些命令执行以下操作:

    • 导出使用 Vertex AI 登录所需的变量。
    • 导出用于设置 Gemini 所用模型的变量。
    • 将这些导出语句添加到 .bashrc 文件中,以便在登录虚拟机时自动导出上述变量。
    • 创建一个项目专用目录,以便在其中运行 Gemini。
    • 运行 Gemini。

    Gemini 提示您选择身份验证方法。

  2. 选择 Vertex AI,然后按 Enter 键或 Return 键。

    由于所需的环境变量已经提供,因此您现在通过了 Gemini CLI 的身份验证。

  3. 请运行以下斜杠命令修改 Gemini CLI 设置:

    /settings

    此时会显示用户设置。

  4. 使用向下键滚动到 Disable Auto Update 设置,然后按 Enter 键或 Return 键。

    “Disable Auto Update”设置已设置为 true。

  5. 请按 Esc 键退出设置对话框。

  6. 请输入 /quit 斜杠命令退出 Gemini CLI。

点击检查我的进度,验证已完成以下目标: 安装并运行 Gemini CLI

任务 2. 探索 Gemini CLI 配置

在此任务中,您将了解 Gemini CLI 配置

Gemini CLI 配置文件名为 settings.json,可在三个不同的层级指定相应设置:

  • 用户设置是在 ~/.gemini/settings.json 中指定的,其中 ~ 是您的主目录。用户设置文件中的设置适用于当前用户的所有 Gemini CLI 会话。
  • 项目设置是在项目根目录内的 .gemini/settings.json 中指定的。只有从这一特定项目运行 Gemini CLI 时,才应用此处的设置。
  • 在 Linux 系统中,系统设置是在 /etc/gemini-cli/settings.json 中指定的。非管理员用户通常无法修改系统设置。系统设置适用于系统上的所有用户会话。

设置的应用是分层级的。系统设置会覆盖项目设置,项目设置会覆盖用户设置。系统设置和项目设置文件只有在指定相应类型的设置之后才会创建出来。

探索配置设置文件

  1. 请运行以下命令列出用户设置文件的内容:

    cat ~/.gemini/settings.json

    用户设置中指定了所选的身份验证类型 vertex-ai

  2. 请运行以下命令启动 Gemini CLI:

    cd ~/project1 gemini
  3. 请运行以下斜杠命令修改 Gemini CLI 设置:

    /settings
  4. 使用向上键和向下键滚动到 Show Line Numbers 设置,然后按 Enter 键或 Return 键。

    对于当前用户,“Show Line Numbers”设置为 true。现在,CLI 输入界面中的代码块将显示行号。

    您还可以在此处管理某些项目级和系统级设置。

  5. 请按 Esc 键退出设置对话框。

  6. 要进入 shell 模式,请按 ! 键。

    在 shell 模式下,您可以直接从 Gemini CLI 中与系统 shell 进行交互。只需输入一个感叹号,即可切换 shell 模式。

  7. 请运行以下 shell 命令查看设置文件的新内容:

    cat ~/.gemini/settings.json 注意:在 shell 模式下执行的命令与直接在终端中运行的命令具有相同的权限和影响力,因此请谨慎使用可更改或移除文件和目录的命令。

    现在,您应该会在 settings.json 文件中看到“showLineNumbers”设置。

    通过感叹号提示符和 shell mode enabled 指示文本,可以判断出 Gemini CLI 处于 shell 模式。

    两个箭头分别指向感叹号提示符和“shell mode enabled”指示文本。

  8. 请按 ! 键退出 shell 模式。

  9. 请输入 /quit 斜杠命令退出 Gemini CLI。

添加上下文

上下文文件为 Gemini 模型设置指导性上下文。借助此功能,您可以向 Gemini 提供项目专属规则、编码风格指南或任何相关背景信息。这有助于您获得更符合自身需求的回答。

Gemini CLI 的上下文通常存储在名为 GEMINI.md 的文件中。接下来,您将使用一个上下文文件来帮助我们理解代码库。

  1. 请运行以下命令下载 Gemini CLI 代码库并列出代码库根目录:

    cd ~ git clone https://github.com/google-gemini/gemini-cli.git ls ~/gemini-cli

    Gemini CLI 代码库的根目录中包含一个上下文文件 GEMINI.md。当您的团队使用 Gemini 协助完成开发任务时,上下文文件可为 Gemini 提供指引,其中涵盖编码标准、首选库和模式乃至构建和测试标准。

  2. 请运行以下命令在 gemini-cli 目录中启动 Gemini CLI:

    cd ~/gemini-cli gemini
  3. 请提交以下提示,让 Gemini CLI 向您介绍代码库:

    Provide me a bulleted high-level description of the codebase in the current directory.

    Gemini CLI 会将代码、GEMINI.md 上下文文件中的所有内容以及您的提示统统发送给 Gemini。

    Gemini 可能会给出如下回答:

    ✦ Here's a high-level description of the Gemini CLI codebase: * Purpose: Integrates Google's Gemini AI directly into the command line for developers. * Core Technologies: Built with Node.js and TypeScript. * User Interface: Utilizes React (Ink) for an interactive terminal experience. * Key Capabilities: Offers AI-powered code understanding, generation, automation, and debugging. * Tooling: Includes built-in tools for file system operations, shell commands, and Google Search grounding. * Extensibility: Supports custom integrations via the Model Context Protocol (MCP). * Development Practices: Follows modern TypeScript guidelines, emphasizing functional programming, immutability, and comprehensive testing with Vitest.

    这段简要描述提供了代码库的完整简介。不过,如果指导信息不足,您收到的审查结果可能会过于详细。

    现在,请使用自己的上下文来塑造 Gemini 的回答。

  4. 请输入 /quit 斜杠命令退出 Gemini CLI。

  5. 请运行以下命令为用户创建 GEMINI.md 上下文文件:

    cat > ~/.gemini/GEMINI.md <<EOF # Gemini CLI: Explain Mode You are Gemini CLI, operating in a specialized **Explain Mode**. Your function is to serve as a virtual Senior Engineer and System Architect. Your mission is to act as an interactive guide, helping users understand complex codebases through a conversational process of discovery. Your primary goal is to act as an intelligence and discovery tool. You deconstruct the "how" and "why" of the codebase to help engineers get up to speed quickly. You must operate in a strict, read-only intelligence-gathering capacity. Instead of creating what to do, you illuminate how things work and why they are designed that way. Your core loop is to **scope, investigate, explain, and then offer the next logical step**, allowing the user to navigate the codebase's complexity with you as their guide. # Core Principles of Explain Mode - **Guided Discovery:** You do not provide a single, massive explanation. You break down complex topics into manageable parts and ask the user where to begin. Your goal is to lead an interactive tour, not deliver a lecture. - **Uncompromising Read-Only Access:** You are empowered to perform deep system interrogation by mapping dependencies, tracing execution paths, and cross-referencing code with external documentation. - **Absolutely No Modifications:** You are fundamentally an analysis tool. You are prohibited from any action that alters the project or system. - **Context-Aware Follow-up:** Every explanation you provide must end by proposing specific, logical next steps for a deeper dive, based on the information you just presented. # Interactive Steps 1. **Acknowledge & Decompose:** Confirm you are in **Explain Mode**. Analyze the user's initial query. If the query is broad (e.g., "explain the auth system," "how does the database work?"), your **first response must be to decompose the topic into a list of specific sub-topics.** You will then ask the user to choose which area to investigate first. Do not proceed until the user provides direction. 2. **Conduct Focused Investigation:** Based on the user's choice, perform a targeted investigation. Before presenting the full explanation, briefly summarize your investigation path (the "Investigation Footprint"). 3. **Synthesize the Technical Narrative:** Formulate a clear, structured explanation for the *specific sub-topic* the user selected. Connect concepts, explain design patterns, and clarify the responsibilities of the relevant code. 4. **Present Explanation & Propose Next Steps:** Present your focused explanation. Critically, conclude your response by offering a list of new, context-aware questions that represent logical next steps. This guides the user deeper into the system. For example, after explaining a specific API route, you might ask if they want to see the service it calls, the data model it uses, or its authentication middleware. EOF

    此上下文文件定义了一种名为 Explain Mode 的模式,它把 Gemini CLI 转变成一位虚拟的高级工程师和系统架构师。该模式设计为只读,旨在通过交互式流程帮助用户理解代码库。

  6. 请运行以下命令启动 Gemini CLI:

    cd ~/gemini-cli gemini
  7. 请输入以下提示,让 Gemini CLI 使用 Explain Mode 为您介绍代码库:

    Tell me about the codebase in the current directory.

    此时 Gemini 的回答不同于没有 Explain Mode 上下文时使用相同提示所获得的回答。新的回答可能如下所示:

    ✦ I am in Explain Mode. This codebase appears to be a monorepo containing several packages, along with documentation, configuration, and CI/CD workflows. To help you understand it better, I can explain the following high-level areas: 1. Project Configuration: How the project is configured (e.g., linting, formatting, build settings). 2. Documentation: The structure and content of the project's documentation. 3. CI/CD & GitHub Workflows: The automated processes for continuous integration, deployment, and other GitHub actions. 4. Scripts: The various utility and build scripts used in the project. 5. Core Packages/Modules: The main functional components of the monorepo (e.g., a2a-server, cli, core, vscode-ide-companion). 6. Integration Tests: How integration tests are structured and executed. 7. Example Extension: The "hello world" example extension. Which area would you like to investigate first?
  8. 为了让它继续回答,请提及 Gemini 列出的一个关键领域。

    例如,您可以这样回答:

    core packages

    Gemini 继续使用 Explain Mode 上下文,因为聊天记录也作为上下文提供给 Gemini。回答可能如下所示:

    ✦ You've chosen to explore the core packages. Within the packages/ directory, I see the following sub-packages: 1. a2a-server/: Likely an application-to-application server component. 2. cli/: The command-line interface application. 3. core/: Core functionalities or shared utilities used across other packages. 4. test-utils/: Utilities specifically designed for testing purposes. 5. vscode-ide-companion/: A companion component for Visual Studio Code integration. Which of these core packages would you like to delve into first?

    Gemini CLI 现在使用的是两个 GEMINI.md 文件提供的上下文,还结合了通用的 Explain Mode 上下文和 gemini-cli 目录中的项目专属信息。

    您还可以使用 /memory 斜杠命令添加上下文。

  9. 请运行以下斜杠命令查看正在使用的上下文:

    /memory show

    系统会显示用户级上下文 ~/.gemini/GEMINI.md 和项目级上下文 ./GEMINI.md 的内容。

  10. 请运行以下命令向用户级上下文添加文本:

    /memory add When listing sub-topics for the user, mark one of them with RECOMMENDED, to indicate that it is the best next topic for the user.

    Gemini CLI 会显示即将添加到用户级上下文的文本。

  11. 请按 Enter 键或 Return 键应用更改。

  12. 请输入 /quit 斜杠命令退出 Gemini CLI。

  13. 请运行以下命令查看用户级上下文的内容:

    cat ~/.gemini/GEMINI.md

    您应该会在文件底部看到新添加的上下文。

  14. 请运行以下命令启动 Gemini CLI:

    cd ~/gemini-cli gemini
  15. 请输入以下提示,要求解释代码:

    Tell me about the codebase in the current directory.

    现在,Gemini 会将一个子主题标记为 RECOMMENDED

  16. 请输入 /quit 斜杠命令退出 Gemini CLI。

点击检查我的进度,验证已完成以下目标: 探索 Gemini CLI 配置

任务 3. 创建自定义斜杠命令

在此任务中,您将了解自定义斜杠命令

借助自定义命令,您可以在 Gemini CLI 中将常用的提示保存为个人快捷方式,以便重复使用。自定义命令存储在特定目录下的 TOML 文件中。

您可以在 ~/.gemini/commands/ 下创建全局可用的命令,也可以在项目目录内的 .gemini/commands/ 下创建特定项目内可用的命令。

您将创建一个 explain 斜杠命令,以替换上下文文件中的 explain 内容。

  1. 请运行以下命令移除用户级上下文文件:

    rm ~/.gemini/GEMINI.md

    Explain Mode 上下文不再提供给 Gemini CLI。

  2. 请运行以下命令创建命令文件:

    mkdir ~/.gemini/commands cat > ~/.gemini/commands/explain.toml <<EOF description = "use Explain Mode" prompt = ''' # Gemini CLI: Explain Mode You are Gemini CLI, operating in a specialized **Explain Mode**. Your function is to serve as a virtual Senior Engineer and System Architect. Your mission is to act as an interactive guide, helping users understand complex codebases through a conversational process of discovery. Your primary goal is to act as an intelligence and discovery tool. You deconstruct the "how" and "why" of the codebase to help engineers get up to speed quickly. You must operate in a strict, read-only intelligence-gathering capacity. Instead of creating what to do, you illuminate how things work and why they are designed that way. Your core loop is to **scope, investigate, explain, and then offer the next logical step**, allowing the user to navigate the codebase's complexity with you as their guide. # Core Principles of Explain Mode - **Guided Discovery:** You do not provide a single, massive explanation. You break down complex topics into manageable parts and ask the user where to begin. Your goal is to lead an interactive tour, not deliver a lecture. - **Uncompromising Read-Only Access:** You are empowered to perform deep system interrogation by mapping dependencies, tracing execution paths, and cross-referencing code with external documentation. - **Absolutely No Modifications:** You are fundamentally an analysis tool. You are prohibited from any action that alters the project or system. - **Context-Aware Follow-up:** Every explanation you provide must end by proposing specific, logical next steps for a deeper dive, based on the information you just presented. # Interactive Steps 1. **Acknowledge & Decompose:** Confirm you are in **Explain Mode**. Analyze the user's initial query. If the query is broad (e.g., "explain the auth system," "how does the database work?"), your **first response must be to decompose the topic into a list of specific sub-topics.** You will then ask the user to choose which area to investigate first. Do not proceed until the user provides direction. 2. **Conduct Focused Investigation:** Based on the user's choice, perform a targeted investigation. Before presenting the full explanation, briefly summarize your investigation path (the "Investigation Footprint"). 3. **Synthesize the Technical Narrative:** Formulate a clear, structured explanation for the *specific sub-topic* the user selected. Connect concepts, explain design patterns, and clarify the responsibilities of the relevant code. 4. **Present Explanation & Propose Next Steps:** Present your focused explanation. Critically, conclude your response by offering a list of new, context-aware questions that represent logical next steps. This guides the user deeper into the system. For example, after explaining a specific API route, you might ask if they want to see the service it calls, the data model it uses, or its authentication middleware. ''' EOF

    创建的斜杠命令 (/explain) 与 explain.toml 文件的前缀相匹配。三重引号之间的提示文本与用户级上下文文件中使用的文本完全相同。提示上方的描述文本会在您输入斜杠命令时显示。

  3. 请运行以下命令启动 Gemini CLI:

    cd ~/gemini-cli gemini
  4. 请输入以下提示,获取不使用 Explain Mode 的描述:

    tell me about the code

    Gemini 在不使用 Explain Mode 上下文的情况下作答。

  5. 请输入以下自定义斜杠提示,以使用 Explain Mode:

    /explain tell me about the code

    Gemini 在 Explain Mode 下作答。现在,您可以像之前那样继续交互式对话。

  6. 请输入以下提示,向 Gemini 询问该模式的使用情况:

    What mode are you in?

    Gemini 回答说,它仍处于 Explain Mode。

  7. 请输入 /quit 斜杠命令退出 Gemini CLI。

点击检查我的进度,验证已完成以下目标: 创建自定义斜杠命令

任务 4. 使用 Gemini CLI 工具

在此任务中,您将了解 Gemini CLI 工具

Gemini CLI 的内置工具可支持 Gemini 模型与您的本地环境交互、检索信息并执行任务。正因拥有这项功能,Gemini CLI 可以帮助您完成各种活动。

Gemini CLI 工具包括:

  • 文件系统工具,用于列出目录、读写文件、使用通配符搜索文件、搜索文件中的文本以及更新文件中的文本。
  • shell 工具,用于与底层系统交互、运行脚本和执行命令行操作。
  • 网页搜索和提取工具。
  • 记忆工具,用于跨会话保存和回溯信息。

Gemini 使用内置工具从互联网检索信息,并创建包含这些信息的文件。

Gemini CLI 检查点

Gemini CLI 包含一项检查点功能,可在 AI 赋能的工具修改文件之前,自动创建项目状态的 Git 快照。借助这项功能,您可以安全地尝试让 Gemini CLI 做出更改,因为您知道必要时可以立即撤销更改。

每个检查点都位于主目录内 ~/.gemini 目录下的一个特殊影子 Git 仓库中。快照会捕获当时项目文件的完整状态,而不会对项目的 Git 仓库产生任何影响。此外,它还捕获截至这一刻您与智能体的完整对话,以及即将执行的特定工具调用。

如需恢复检查点,您可以使用 /restore 斜杠命令。此命令会将文件还原到所捕获的状态,恢复对话历史记录,并重新提出原始的工具调用方案。您可以再次运行、修改或者直接忽略该方案。

您可以使用 --checkpointing 标志为会话启用检查点功能,也可以在 settings.json 文件中启用检查点功能。

注意:在本实验中,无需使用检查点。请考虑在关键任务项目中使用该功能。

从互联网检索最新信息

  1. 请运行以下命令启动 Gemini CLI:

    cd ~/project1 gemini
  2. 要启用 shell 模式,请按 ! 键。

    Gemini CLI 会将输入的文本作为 shell 命令运行,直到 shell 模式停用。

  3. 请运行以下 shell 命令,从互联网检索 Google Cloud RSS Feed:

    wget -O ~/project1/rss.xml "https://cloudblog.withgoogle.com/rss/"

    Feed XML 存储在 ~/project1 目录下名为 rss.xml 的文件中。

    注意:如果您使用普通的提示,Gemini CLI 也可以为您检索此 Feed,但本实验使用 shell 命令是为了确保 Feed 检索的一致性。
  4. 请按 ! 停用 shell 模式。

  5. 请提交以下提示,总结 Feed 中的信息:

    summarize the content of the RSS XML file

    如果您成功检索到了 RSS Feed 文件,则返回的信息可能如下所示:

    ✦ The RSS feed from the Google Cloud Blog contains several recent articles covering various advancements and updates in Google Cloud's offerings. Key topics include: * AI Security: Model Armor is presented as a solution to protect AI applications from prompt injections, jailbreaks, sensitive data exposure, and other threats, with integration details for Apigee. * Multi-Architecture Migration: Google's journey to a multi-architecture (x86 and Arm) environment for its production services is detailed, highlighting the role of AI and automation tools like CogniPort in this large-scale migration. * Cloud Security Enhancements: Cloud Armor receives new features such as hierarchical security policies, enhanced WAF inspection, JA4 network fingerprinting, and Network Threat Intelligence for Media CDN, reinforcing its position as a strong performer in web application firewall solutions. * Data Lineage for AI Trust: Dataplex now offers column-level lineage for BigQuery data, providing granular visibility into data's journey to build trust and context for AI systems. * AI Hypercomputer Updates: Significant updates to the AI Hypercomputer include vLLM on TPU for improved LLM inference, an enhanced XProf Profiler, and new recipes for NVIDIA Dynamo and NeMo RL. * GPU Advancements: The general availability of G4 VMs powered by NVIDIA RTX PRO 6000 Blackwell GPUs is announced, offering substantial performance gains for AI, visual computing, and simulation, along with a custom P2P fabric for multi-GPU workloads. Overall, the feed highlights Google Cloud's continuous innovation in AI, machine learning, security, and infrastructure to provide high-performance, secure, and scalable solutions for enterprises. 注意:如果 Gemini CLI 无法总结 Feed 的详细信息,您需要检索暂存的文件版本。
  6. 如果 RSS XML 文件检索失败,请启用 shell 模式并使用以下命令检索暂存的 Feed 文件版本:

    wget -O ~/project1/rss.xml "https://storage.googleapis.com/cloud-training/CBL547/rss.xml"

    在下一个任务中,Feed 文件将用作 Web 应用的数据源。Gemini CLI 可以移动并重命名该文件。

  7. 如果您处于 shell 模式,请按 ! 键停用 shell 模式。

  8. 请提交以下提示,修改文件和目录结构:

    Create a directory called "data" and move the rss file into that directory and rename the file to "feed.xml".
  9. 每次 Gemini CLI 询问您是否要继续时,请按 Enter 键以允许使用该工具。

    注意:虽然本实验要求您始终接受所做的更改,但在实际的应用场景中,通常需要分析建议的更改,然后接受或拒绝更改。
  10. 请提交以下提示,查看生成的目录结构:

    return a long listing the contents of all directories and subdirectories
  11. 请输入 /quit 斜杠命令退出 Gemini CLI。

  12. 请输入 exit,然后按 EnterReturn 键,退出 SSH 会话。

点击检查我的进度,验证已完成以下目标: 使用 Gemini CLI 工具

任务 5. 使用 Gemini CLI 构建 Web 应用

在此任务中,您将使用 Gemini CLI 氛围编程 (vibe coding) 方式构建一个 Web 应用。

氛围编程是一种软件开发实践,在人工智能的帮助下按照自然语言提示生成功能代码。氛围编程不但帮助开发者更快地开发应用,还让编程经验不足的人也能构建正常运行的应用。

注意:对生成式 AI 大语言模型发出提示时,每次运行所得到的回答和创建的内容可能会有所不同。
  1. 在 Cloud Shell 中,建立与虚拟机的 SSH 连接:

    gcloud compute ssh {{{project_0.startup_script.vm_name | VM_NAME}}} --zone={{{project_0.default_zone | ZONE}}} -- -L 8080:localhost:5000 注意:请仔细观察此 SSH 命令,它与之前使用的命令不同。该命令的末尾设置了一个本地端口转发隧道,本地端口 8080 上的流量会自动转发到虚拟机上的端口 5000。此命令用于测试您在此任务中构建的 Web 应用。
  2. 请运行以下命令启动 Gemini CLI:

    cd ~/project1 gemini

    第一步是调查您已下载的 feed.xml 文件。

  3. 请提交以下提示,查看 Feed 中包含哪些内容:

    @data/feed.xml what data fields does a feed entry have?

    At (@) 命令会将 data/feed.xml 文件的内容作为提示的一部分发送给 Gemini。

    Gemini CLI 的回答可能如下所示:

    ✦ A feed entry (<item>) in the feed.xml file has the following data fields: * title * link * description * pubDate * guid * category * og:type * og:title * og:description * og:site_name * og:url * author:name * author:title * author:department * author:company

    接下来,您将构建一个 Python Flask Web 应用,以显示该 Feed 中的信息。

  4. 请提交以下提示,让 Gemini CLI 构建应用:

    Before you make any changes, please design a plan for what you will do, and ask me to approve the plan! 1. Use a virtual environment (venv) for Python3. 2. Create a Python web app which uses the Flask library and uses feed.xml as a data source. 3. The index.html page should read the feed.xml file and then show a list of the blog entries, ordered from most recent to least recent. 4. Each entry should show the date of the blog post and a link, where the link's text is the blog title and the URL opens the blog entry URL in a new tab. 5. The app should be hosted on port 5000. 6. Start the web server for me.

    第一个语句要求 Gemini CLI 设计应用创建计划并请求批准。这是使用 AI 工具进行系统更改的最佳做法。

    上述编号列表为 Gemini CLI 提供了该应用的一些要求。

    Gemini CLI 会为该应用设计一个计划。审查 Gemini CLI 提供的计划。

    回答可能如下所示:

    ✦ Here's my plan to create the Flask web application: 1. Set up Python Virtual Environment: * Create a virtual environment named venv. * Activate the virtual environment. * Install Flask and feedparser (for easy XML parsing) within the virtual environment. 2. Develop Flask Application (app.py): * Create a Python file app.py. * Initialize a Flask application. * Define a route for the root URL (/). * Inside the route function: * Read and parse the feed.xml file using feedparser. * Sort the blog entries by publication date in descending order (most recent first). * Render an HTML template (index.html), passing the sorted blog entries to it. 3. Create HTML Template (templates/index.html): * Create a directory named templates. * Inside templates, create index.html. * This template will: * Display a heading for the blog posts. * Iterate through the list of blog entries. * For each entry, display its publication date and a clickable title that links to the blog post's URL, opening in a new tab. 4. Run the Flask Web Server: * Set the FLASK_APP environment variable to app.py. * Start the Flask development server on port 5000. Do you approve this plan?
  5. 当 Gemini CLI 让您批准计划时,输入 yes,然后按 EnterReturn 键。

    Gemini CLI 开始实施更改。在本实验中,请接受 Gemini CLI 发出的每个请求。

  6. 每次 Gemini CLI 询问您是否要继续时,请按 Enter 键以允许使用该工具。

    在多个步骤得到您的确认之后,Gemini CLI 应该会创建并启动应用。

    注意:只要 Shell 工具显示 Flask 应用已开始服务,就可以继续执行下一步,无需等待命令提示符返回。
  7. 如需在网络浏览器中运行应用,请点击 Web Preview,然后选择 Preview on port 8080

    在端口 8080 上预览网页

    浏览器中会打开一个新标签页,显示应用正在运行。您可以点击一个链接,该博文将在新标签页中打开。

    这个应用可能非常简单。您可以让 Gemini CLI 改进它。

  8. 如果 Shell 工具仍在运行,请按 ESC 键取消。

  9. 请返回到 Gemini CLI 并提交以下提示,让 Gemini CLI 修改应用:

    Here are some changes I'd like to make. As before, I want to approve your plan first. 1. Links on index.html now open a new page called entry.html. 2. The new page, entry.html, takes a query parameter that contains a URL-encoded version of the entry's unique identifier and displays all of the blog entry info from the feed in a readable format. 3. A link toward the top of entry.html should open the actual blog entry in a new tab. 4. Add a back button on entry.html to take you back to index.html. 5. Add CSS to make the app look pleasing, and use the blue and green colors from the Google logo.

    Gemini 会考虑上述要求,然后制定计划。审查 Gemini CLI 提供的计划。

    回答可能如下所示:

    ✦ Here's a plan to implement the requested changes: 1. Update app.py: * Add a new route /entry: This route will handle requests for individual blog entries. * It will expect a guid query parameter, which will be the unique identifier for the blog entry. * It will re-parse feed.xml to find the entry matching the provided guid. * If the entry is found, it will render a new template entry.html, passing the found entry's data. * If the entry is not found, it will redirect to the home page or display an error. * Ensure feed.xml parsing logic is robust: The index route will continue to parse feed.xml and sort entries for the main listing. 2. Modify templates/index.html: * Update entry links: Change the href for each blog entry's title to point to the new /entry route, passing the entry's guid as a URL-encoded query parameter. * Link to CSS: Add a <link> tag in the <head> section to include the new style.css file. 3. Create templates/entry.html: * New HTML file: Create entry.html in the templates directory. * Display entry details: This page will display the full details of the selected blog entry, including: * Title * Publication Date * Author (if available) * Description (raw HTML content will be displayed, as it's already HTML in the feed) * Link to original post: Include a prominent link (e.g., "Read original article") that uses entry.link and target="_blank" to open the original blog post in a new tab. * Back button: Add a link or button that navigates back to index.html using url_for('index'). * Link to CSS: Add a <link> tag in the <head> section to include the new style.css file. 4. Create static/style.css: * New CSS file: Create a static directory and style.css inside it. * Styling: Apply CSS rules to both index.html and entry.html for a pleasing aesthetic. I will use Google's brand colors: * Google Blue: #4285F4 * Google Green: #34A853 * General layout, typography, link styles, and box models for entries will be defined. 5. Restart Flask Application: * The Flask server will need to be restarted to pick up the changes in app.py and the new template/static files. Do you approve this detailed plan?
  10. 当 Gemini CLI 让您批准计划时,输入 yes,然后按 EnterReturn 键。

  11. 每次 Gemini CLI 询问您是否要继续时,请按 Enter 键以允许使用该工具。

    Gemini CLI 将再次执行更新工作并重启 Web 服务器。

  12. 刷新 Web 应用页面,或使用“Web Preview”打开新标签页。

    现在,更新后的应用会包含每篇博文的专属页面,并使用所要求的 Google 蓝色和 Google 绿色。

    注意:如果应用看起来没有变化,则可能是因为 Gemini CLI 未成功终止 Web 服务器。使用类似“kill the web server and restart it”的提示,帮助 Gemini CLI 重启 Web 服务器。

    下面是 Gemini CLI 生成的博客页面示例:

    博文示例,使用 Google 绿色和蓝色,包含“返回首页”链接和用于查看原始博文的按钮。

点击检查我的进度,验证已完成以下目标: 使用 Gemini CLI 构建 Web 应用

可选:在确认第 5 步进展顺利之后,您可以使用 Gemini CLI 对该应用实施其他更改或修复操作。

恭喜!

您已成功安装 Gemini CLI,而且配置并提供了上下文以获得符合自身需求的回答,还使用了 Gemini CLI 解释代码和构建应用。

本手册的最后更新时间:2025 年 10 月 22 日

本实验的最后测试时间:2025 年 10 月 22 日

版权所有 2026 Google LLC 保留所有权利。Google 和 Google 徽标是 Google LLC 的商标。其他所有公司名称和产品名称可能是其各自相关公司的商标。

准备工作

  1. 实验会创建一个 Google Cloud 项目和一些资源,供您使用限定的一段时间
  2. 实验有时间限制,并且没有暂停功能。如果您中途结束实验,则必须重新开始。
  3. 在屏幕左上角,点击开始实验即可开始

使用无痕浏览模式

  1. 复制系统为实验提供的用户名密码
  2. 在无痕浏览模式下,点击打开控制台

登录控制台

  1. 使用您的实验凭证登录。使用其他凭证可能会导致错误或产生费用。
  2. 接受条款,并跳过恢复资源页面
  3. 除非您已完成此实验或想要重新开始,否则请勿点击结束实验,因为点击后系统会清除您的工作并移除该项目

此内容目前不可用

一旦可用,我们会通过电子邮件告知您

太好了!

一旦可用,我们会通过电子邮件告知您

一次一个实验

确认结束所有现有实验并开始此实验

使用无痕浏览模式运行实验

使用无痕模式或无痕浏览器窗口是运行此实验的最佳方式。这可以避免您的个人账号与学生账号之间发生冲突,这种冲突可能导致您的个人账号产生额外费用。