在 Google Cloud 专用 Gemini 的协助下开发 Python 应用

本教程将介绍如何使用 AI 赋能的 Google Cloud 中的 Gemini for Google Cloud 探索、创建、修改、测试和部署 Python 应用示例。

在以下示例中,假设您是一位开发者, 开发产品目录应用的团队您负责 基本服务,其中包含一些 API 方法,可作为 。您想要在本地 VS Code 环境中开发应用 然后将其部署到 Google Cloud但您不确定 哪种 Google Cloud 服务最适合您的使用场景。

在本教程中,您将使用 Gemini 和 Visual Studio Code 作为 IDE,用于构建包含两个 API 方法的示例 Inventory 应用。

本教程适用于任何经验水平且已 曾参与构建应用,但可能不熟悉 云技术。本文档假定您具有使用 VS Code 的一定经验, 并且熟悉 Python 和 Flask 框架。

目标

  • 探索可用于部署应用的各种 Google 服务,方法为: 询问 Gemini 基于情境的问题。

  • 提示 Gemini 提供可用于 在 Cloud Run 中开发基本的 Python 应用。

  • 使用 Gemini 创建、探索和修改应用,以便: 并生成代码

  • 在本地运行并测试应用,然后将其部署到 Google Cloud 生成步骤。

使用的 Google Cloud 产品

本教程使用以下可计费的 Google Cloud 产品。 请使用价格计算器根据您的预计使用情况来估算费用。

  • Cloud Run。Cloud Run 是一个代管式计算平台 让你可以直接在 Google 可伸缩的基础架构上运行容器, 基础架构您可以将使用任何编程语言编写的代码部署到 Cloud Run(如果您可以基于它构建容器映像)。 如需了解价格信息,请参阅 Cloud Run

除前面提到的产品外,本教程还使用了 以下产品:

  • Gemini。Gemini 始终是你的好帮手 提供基于生成式 AI 的辅助功能, 广泛的用户,包括开发者和数据科学家。 为了提供综合性辅助体验, Gemini 嵌入在许多 Google Cloud 产品中。

  • Cloud Code for VS Code 扩展程序。此扩展程序是一个 IDE 为 Kubernetes 的开发周期提供支持的插件, Cloud Run 应用。如需详细了解 Cloud Code 扩展程序 请参阅 Cloud Code for VS Code 功能

准备工作

  1. 安装 VS Code 的本地副本。 。

  2. 安装适用于 VS Code 的 Cloud Code 扩展程序。

    按照安装指南中的说明进行安装 PythonGit、 和 Docker 客户端。 此外,请按照相应步骤创建 Google Cloud 项目。

  3. 确保已为您的 Google Cloud 用户账号和项目设置了 Gemini。
  4. 为 IDE 启用 Gemini。

    按照相应步骤在 IDE 中连接到 Google Cloud,并选择 为 Google Cloud 设置的另一个项目。

探索 Google Cloud 服务

如果您刚开始接触 Google Cloud,Gemini 可以助您一臂之力 选择符合您组织要求的 Google Cloud 服务 应用架构

在 IDE 中,您可以与 Gemini 聊天以获取帮助。使用 在 Gemini 窗格中,输入提示(问题或陈述) 描述所需帮助的内容),Gemini 会返回回答。提示 可以包含 Google Cloud 分析的现有代码中的上下文 以提供更实用或更完整的回答。 如需详细了解如何撰写提示以生成优质回答,请参阅以下内容: 请参阅针对 Gemini 撰写更好的提示

如需向 Gemini 询问 Google Cloud 服务相关事宜,请按以下步骤操作: 请按以下步骤操作:

  1. 在 IDE 的活动栏中,点击 spark Gemini

    如需查找 VS Code 的活动栏,请参阅 界面

  2. Gemini 窗格中,输入以下提示,然后点击发送

    I am new to Google Cloud and I want to use the Cloud Code extension.
    Give me some examples of Google services that I can use to build and deploy a
    sample app using containers.
    

    Gemini 会在回答中列出 Google Cloud 服务和说明。

    在本示例中,假设 Gemini 建议 Cloud Run 和 Cloud Functions,作为 Google Cloud 的 这些服务可以帮助您构建和部署示例应用,但您还需要更多 这些服务的相关信息

    了解 Gemini for Google Cloud 如何以及何时使用您的数据

  3. 输入后续问题,如下所示:

    What is the difference between Cloud Run and Cloud Functions? Explain to me
    in simple terms as I am new to Google Cloud.
    

    Gemini 会在回答中详细指出 两项 Google Cloud 服务

  4. 可选:如要重置聊天记录,请在 Gemini 窗格中点击 delete 重置聊天

创建一个 Hello World Python 应用

假设您希望使用 Cloud Run 创建和部署 基本 Python 应用。因为您刚开始接触 Cloud Run Cloud Code,您需要有关创建应用步骤的帮助。 在本部分中,您需要提示 Gemini 输入以下内容: 可帮助您在 Cloud Run 中构建 Hello World Python 应用。

  1. 启动 IDE(如果尚未启动)。

  2. 在 IDE 的活动栏中,点击 spark Gemini

  3. Gemini 窗格中,输入以下提示,然后点击发送

    How to create a new Cloud Run app in Cloud Code using the command palette?
    What languages are supported?
    

    VS Code 中的命令面板提供了所有命令的列表, 包括 Cloud Code 命令

    输出类似于以下内容:

    To create a new Cloud Run app in Cloud Code using the command palette,
    follow these steps:
    1. Open the command palette (press Ctrl/Cmd+Shift+P or click View > Command Palette).
    2. Run the "Cloud Code: New Cloud Run App" command.
    ...
    

    Gemini 还会显示 Cloud Run 应用。

    在此示例中,您选择使用基于 Python 的 Cloud Run 模板创建示例应用。

  4. 如需了解如何使用 Cloud Run 模板创建 Python 应用, 输入以下提示:

    How do I create a Python app from a Cloud Run template using the
    command palette? I am using Cloud Code.
    

    输出类似于以下内容:

    To create a Python app from a Cloud Run template using the command palette,
    follow these steps:
    
    1. Open Cloud Code.
    
    2. Click the Command Palette
       (press Ctrl/Cmd+Shift+P or click View > Command Palette).
    
    3. Run the "Cloud Code: New Application" command.
    
    ...
    

    完成 Gemini 在回答以下问题时提供的步骤 在 Cloud Run 中创建一个基本的 Hello World Python 应用。 您还可以参考以下部分来创建 Hello World Python 应用。

创建 Hello World Python 应用的替代步骤

  1. 启动 IDE(如果尚未启动)。

  2. 打开命令面板:按 Command+Shift+P(适用于 MacOS)或 Ctrl+Shift+P(适用于 Windows 和 Linux)。

  3. 在命令面板字段中,输入 Cloud Code: New Application,以及 然后点击相应结果

  4. 从可用示例列表中,选择 Cloud Run 应用

  5. 从可用模板列表中,选择 Python (Flask): Cloud Run

  6. 将新应用保存到首选位置。

系统会显示一条通知,确认您的应用已创建。 系统会在 IDE 中打开所选服务的 README 文件的预览, 如图 1 所示:

<ph type="x-smartling-placeholder">
</ph> 屏幕截图
图 1 为在 Cloud Run 中创建的 Hello World Python 应用显示的 README 文件。

探索 Python 版 Hello World 应用

现在,您已经在 Cloud Run 中创建了 Hello World 应用, 你可以使用 Gemini 来解释 部署在 IDE 中的项目。探索示例应用的代码 请按照以下步骤操作:

  1. 启动 IDE。

  2. 打开探索器:按 Command+Shift+E(适用于 MacOS)或 Ctrl+Shift+E(适用于 Windows 和 Linux)。

    您可以看到与示例应用相关的文件。

  3. 在文件列表中,点击 Dockerfile 以显示其内容。

  4. 选择 Dockerfile 的全部内容,点击灯泡图标,然后点击 点击 Explain this,如图 2 所示:

    dockerfile 的屏幕截图
    图 2. 当您选择代码时,可以使用代码说明功能。

    Gemini 会用自然语言生成关于 Dockerfile 的内容和功能。如果您不确定某些 回答中的几个部分,您可以提出后续问题。

  5. 如需详细了解 Dockerfile 中提到的 app.py 文件,请查看 在 Gemini 窗格中输入以下提示:

    What is the function of the app.py file in Dockerfile?
    

    Gemini 会生成类似于以下内容的回答:

    The app.py file is the entrypoint for the container. It is the file that
    will be executed when the container is launched. In this case, the app.py
    file will run the Python code that is contained within it.
    

    或者,您也可以在 Dockerfile 中选择文本 app.py,请点击 灯泡图标,然后点击 Explain this

  6. 打开探索器:按 Command+Shift+E(适用于 MacOS)或 Ctrl+Shift+E(适用于 Windows 和 Linux)。

  7. 打开 app.py 文件。 您会看到两个变量:K_SERVICEK_REVISION

  8. 如需详细了解 app.py 文件中的变量,请参阅 在 Gemini 窗格中,输入以下提示:

    What is the function of `K_SERVICE` and `K_REVISION` in the `app.py` file?
    

    或者,您也可以在 Dockerfile 中选择以下文本 点击灯泡图标,然后点击 Explain this

    service = os.environ.get('K_SERVICE', 'Unknown service')
    revision = os.environ.get('K_REVISION', 'Unknown revision')
    

    响应类似于以下示例:

    The code above is using the os.environ module to get the values of the
    K_SERVICE and K_REVISION environment variables. These variables are set by
    Cloud Run when it deploys the service, and they contain the name of the
    service and the revision number, respectively.
    ...
    

为应用生成示例数据

您在 Cloud Run 中创建了一个基本的 Flask 应用。在您之前 可以添加功能来构建 Inventory 应用,因此您需要一个 包含示例商品目录商品的列表。使用 Gemini 执行以下操作: 生成相关的示例数据,请按以下步骤操作:

  1. 要显示与示例应用相关的文件,请在 IDE 中点击 探索器图标。

  2. 点击 New file 图标,然后创建一个名为 inventory.py 的文件。

  3. 若要让 Gemini 生成示例数据,请在 在 Gemini 窗格中,输入以下提示:

    Create a variable called inventory which is a list of 3 JSON objects.
    Each JSON object has 2 attributes: productid and onhandqty.
    Both attributes are strings.
    

    Gemini 会为三个 JSON 对象生成示例代码。

  4. 在回复中,点击 addInsert in current file,将代码示例插入 inventory.py 文件,如图 3 所示:

    <ph type="x-smartling-placeholder">
    </ph> 插入代码
    图 3.您可以插入 Gemini 回答中的代码。

    inventory.py 文件类似于以下内容:

    inventory = [
       {
          "productid": "12345",
          "onhandqty": "10"
       },
       {
          "productid": "23456",
          "onhandqty": "20"
       },
       {
          "productid": "34567",
          "onhandqty": "30"
       }
    ]
    

    您已成功创建 inventory.py 文件,其中包含 一个目录项列表

修改 Hello World Python 应用

创建 inventory.py 文件后,现在引入一些 API 方法 位于可对商品目录数据执行操作的 app.py 文件中。 为实现此目标,您可以使用代码生成功能 。

  1. 如需显示与示例应用相关的文件,请在 IDE 中 点击探索器图标。

  2. 如需显示内容,请点击 app.py 文件。

  3. 修改 import 语句,使 app.py 文件包含 inventory.py 文件:

    import os
    from flask import Flask, render_template
    from inventory import inventory
    
  4. 若要让 Gemini 为第一种 API 方法生成代码, 在 app.py 文件中,输入以下注释,以及 按 Ctrl+Enter

     # Generate an app route to display a list of inventory
     # items in the JSON format from the
     # inventory.py file. Use the GET method.
    
    

    Gemini 在 app.py 文件中生成回答。

    根据系统配置, 可能不同于 Ctrl+Enter 组合键。

  5. 将指针悬停在回答的任意部分上。

    系统随即会显示 Gemini 工具栏。

    Gemini 可能会生成多条回答。 在工具栏中,您可以使用 按 <> 键。在以下示例中, Gemini 只会生成一个回答,如图 4 所示:

    <ph type="x-smartling-placeholder">
    </ph> 接受代码
    图 4.您可以接受 Gemini 给出的代码建议。
  6. 要接受建议的代码示例,请在工具栏中点击接受

    输出类似于以下内容:

    # Generate an App route to display a list of inventory
    # items in the JSON format from the
    # inventory.py file. Use the GET method.
    @app.route('/inventory', methods=['GET'])
    def get_inventory():
        """Return a list of inventory items."""
        return jsonify(inventory)
    
  7. 可选:如需详细了解 jsonify(inventory) 函数,请突出显示 并提示 Gemini 向您解释代码。

  8. 修改 import 语句,使 app.py 文件包含 jsonify 函数:

    import os
    from flask import Flask, render_template, jsonify
    from inventory import inventory
    
  9. 如需添加其他 API 方法,请在 app.py 文件中输入 以下提示,然后按 Ctrl+Enter 键:

    # Generate an App route to get an inventory item
    # given the productid. Use the GET method.
    # If there is an invalid productid,
    # display a 404 error.
    

    Gemini 在 app.py 文件中生成回答。

  10. 将指针悬停在回答的任意部分上。

    系统随即会显示 Gemini 工具栏。

  11. 要接受某个建议的代码示例,请在工具栏中点击接受

    输出类似于以下内容:

    # Generate an App route to get an inventory item
    # given the productid. Use the GET method.
    # If there is an invalid productid,
    # display a 404 error.
    @app.route('/inventory/<productid>', methods=['GET'])
    def get_inventory_item(productid):
        """Return an inventory item given the productid."""
        for item in inventory:
            if item["productid"] == productid:
               return jsonify(item)
        return jsonify({"error": "Item not found"}), 404
    

    您已成功添加了两个用于创建 Inventory 应用的 API 方法。

从 IDE 在本地运行应用

您已准备好通过 VS Code 在本地部署应用。 如需部署应用,请按以下步骤操作:

  1. 启动 IDE(如果尚未启动)。

  2. 在 IDE 的活动栏中,点击 spark Gemini

  3. Gemini 窗格中,输入以下提示,然后点击发送

    How do I run a Cloud Run app locally within Cloud Code?
    Is there an emulator?
    

    按照 Gemini 回答中的步骤运行该应用 从 VS Code 本地运行您还可以参阅以下部分 VS Code 中的本地 Cloud Run 应用示例。

从 IDE 在本地运行应用的其他步骤

如果你没有按照 Gemini 回答中的步骤进行操作 您可以按照以下步骤部署示例应用:

  1. 启动 IDE。

  2. 打开命令面板:按 Command+Shift+P(适用于 MacOS) 或 Ctrl+Shift+P(适用于 Windows 和 Linux)。

  3. 在命令面板字段中,输入 Run on Cloud Run Emulator,以及 然后点击相应结果

  4. Build 环境文件中,保留默认值,然后点击 Run

    本教程使用 Docker 作为构建器选项。

    在 IDE 的 Output 窗格中,您可以查看构建进度。 实际的部署过程可能需要一些时间才能完成。

    Starting to run the app using configuration 'Cloud Run:
    Run/Debug Locally' from .vscode/launch.json...
    To view more detailed logs, go to Output channel : "Cloud Run: Run/Debug Locally - Detailed"
    Dependency check started
    Dependency check succeeded
    Starting minikube, this may take a while......
    ...
    

要查看您的实时应用,请在部署完成后点击 输出窗格。对于 VS Code 的本地副本,如果您指定了 localhost 8080, 您的示例应用位于以下网址:http://localhost:8080

测试两种 API 方法

如果您的示例应用已发布,您还可以检查两个应用路由是否正常运行。 在此示例中,假设应用网址为 http://localhost:8080

  1. 要查看应用的着陆页示例,请前往 http://localhost:8080

  2. 若要获取所有商品目录项的列表,请前往 http://localhost:8080/inventory.

    app.py 文件中,为了列出所有商品目录商品,函数 @app.route('/inventory', methods=['GET'])

  3. 要显示按 productid 属性过滤的特定产品目录项, 点击 http://localhost:8080/inventory/productid

    例如,http://localhost:8080/inventory/1,其中 productid 为 1。

    app.py 文件中,若要按 productid 属性,则 已使用 @app.route('/inventory/<productid>', methods=['GET'])

将 Cloud Run 应用部署到 Google Cloud

您现在可以将 Cloud Run 应用部署到 Google Cloud 了。 你可以提示 Gemini 提供相关步骤,也可以 请按以下步骤操作:

  1. 打开命令面板:按 Command+Shift+P(适用于 MacOS) 或 Ctrl+Shift+P(适用于 Windows 和 Linux)。

  2. 在命令面板字段中,输入 Cloud Code: Deploy to Cloud Run。 然后点击相应结果

  3. 服务设置窗格中,设置您的 Google Cloud 项目。

  4. 接受其余默认设置,然后点击部署

  5. Cloud Code 会构建您的映像,将其推送到注册表,并将您的 Cloud Run

  6. 如需查看正在运行的服务,请打开 部署到 Cloud Run 对话框中。

    也可以按照 测试两种 API 方法,以 Google Cloud请相应调整您的网址。

清理

为避免系统因 您可以删除 Google Cloud 项目 为本教程创建的容器或者,您也可以删除 单个资源

  1. 在 Google Cloud 控制台中,进入管理资源页面。

    转到“管理资源”

  2. 在项目列表中,选择要删除的项目,然后点击删除
  3. 在对话框中输入项目 ID,然后点击关闭以删除项目。

后续步骤