开始使用 Azure 的 GitHub Copilot

开始使用 GitHub Copilot for Azure,以简化您的开发工作流并在 Azure 平台上提高您的工作效率。 本指南指导你完成 Visual Studio Code 中用于 Azure 扩展的 GitHub Copilot 的先决条件和安装,以便可以编写第一个提示。

先决条件

若要完成本文中的步骤,请确保具备:

  • Azure 帐户和对 Azure 订阅的访问权限。 有关如何设置它们的详细信息,请参阅 Azure 帐户的定价页。

  • GitHub 帐户和 GitHub Copilot 订阅。 有关如何设置这些帐户的详细信息,请参阅 分别在 GitHub 上创建帐户和 GitHub Copilot 快速入门。

  • Visual Studio Code。 有关如何下载和安装它的详细信息,请参阅 “设置 Visual Studio Code”。

  • GitHub Copilot 扩展和 GitHub Copilot 聊天扩展。 有关如何安装这些扩展的说明,请参阅分别在 VS Code 中设置 GitHub Copilot 和 GitHub Copilot Chat 入门。

重要

GitHub Copilot 不包括在 Visual Studio 订阅中,并且是 GitHub 管理的单独订阅。 有关 GitHub Copilot 订阅和支持的问题,请参阅单个订阅商务订阅

默认情况下,租户中的所有用户都可以使用 Copilot in Azure。 但是,全局管理员可以管理其组织对 Azure 中 Copilot 的访问权限。 还可以选择性地向特定 Microsoft Entra 用户或组授予访问权限。 有关详细信息,请参阅管理对 Microsoft Copilot in Azure 的访问权限

安装 GitHub Copilot for Azure

  1. 在 Visual Studio Code 中,选择“ 扩展 ”图标。
  2. 在扩展市场中,搜索 GitHub Copilot for Azure。 出现用于 Azure 扩展的 GitHub Copilot 时,选择“ 安装”。
  3. 如果系统提示,请登录到 Azure 帐户。

编写第一个提示

  1. 确保已安装扩展、正确进行身份验证,并且扩展正常工作。

  2. 如果聊天窗口尚未打开,请确保在菜单栏中选择“切换聊天”按钮,或选择“切换聊天”按钮旁边的下拉列表,然后选择“打开聊天”(Ctrl+Alt+I)。

    显示“切换聊天”菜单打开并选择“打开聊天”菜单选项的屏幕截图。

  3. 在聊天窗格底部的聊天文本区域中,输入以下提示:

    @azure Do I have any resources currently running?
    

    显示聊天窗格的默认询问模式状态的屏幕截图,其中显示了示例提示。

默认情况下,GitHub Copilot 使用 ask 模式。 询问模式在聊天窗格中提供问题的答案。 代理程序模式使 GitHub Copilot 能够在工作区中执行操作。

在询问模式下,该 @azure 部分指示你想要在对话中包含 Azure 聊天参与者。 它将提示范围限定为特定的域,即 Azure 帐户。

问题的答案取决于订阅中当前在 Azure 中运行的内容。

代理模式

从 Visual Studio Code 版本 1.99 开始,GitHub Copilot 引入了 代理模式、可在整个 Visual Studio Code 工作区中执行任务的代理体验、进行编辑、执行终端命令等。

GitHub Copilot for Azure 为 GitHub copilot 提供“工具”,通过与 Azure 的深度集成来增强代理体验。

在代理模式下,无需通过 @azure 前缀指示 GitHub Copilot 使用 GitHub Copilot for Azure。

启用代理模式

可能需要启用代理模式。

  1. 在 Visual Studio Code 中,使用“管理”图标打开“设置”(齿轮)。

    通过选择“管理”按钮显示 Visual Studio Code 设置菜单的屏幕截图。

  2. 在“设置”的搜索栏中,搜索 chat.agent.enabled

  3. 选中 聊天代理:已启用> 旁的框

    显示设置对话框的屏幕截图,设置已筛选为 chat.agent.enabled。

使用代理模式

有三种模式:ask(default)、编辑和代理。 若要使用代理模式,必须在聊天窗格中选择它。

  1. 在聊天窗格中的聊天提示中,选择模式下拉列表,然后选择 “代理”。

    显示“聊天”框中的弹出菜单的屏幕截图,其中选择了“代理”。

  2. 聊天窗格确认你当前处于代理模式。

    显示聊天窗格的屏幕截图,确认你现在处于代理模式。

  3. 若要查看代理模式是否正常工作,请使用以下提示:

    What are your tools?
    

    完成响应显示可用于 GitHub Copilot 的 Azure 专用工具列表。 响应提供了现可供你使用的功能类型的良好概述:

    I can assist you with various tasks using a range of tools. Here are the main tools I have available:
    
    1. Search and Navigation
      Semantic search for code or documentation
      File search using glob patterns
      Text search (grep)
      List directory contents
      Read file contents
      List code usages/references
    
    2. Code Editing and Analysis
      Insert edits into files
      Get errors in code files
      Test search (find test files)
    
    3. Terminal and Git Operations
      Run commands in terminal
      Get terminal output
      Get Git diffs and changed files
    
    4. Project Creation
      Create new workspaces
      Get project setup information
      Install VS Code extensions
      Create new Jupyter Notebooks
    
    5. Web and API Tools
      Fetch webpage content
      Get VS Code API references
    
    6. Azure Tools
      Azure development best practices
      Microsoft Learn documentation
      Azure resource management
      Azure authentication tools
      AI model deployment and management
      Azure Bicep schemas
      Service configuration and deployment
      Azure Developer CLI tools
    
    Would you like me to explain any specific tool in more detail or help you with a particular task?
    
    
  4. 可使用以下提示来应用最后一个条目 Azure Developer CLI tools

    What is the az command to list all my storage accounts ordered by ___location?
    

    完成该提示可能会生成:

    
    I'll help you generate the Azure CLI command to list all your storage accounts ordered by ___location. Let me use the Azure CLI generator tool to get the correct syntax for this command.
    
    To list all your storage accounts ordered by ___location in Azure, you can use the following Azure CLI command:
    
    az storage account list --query "sort_by(@, &___location)"
    
    This command will:
    
    1. Retrieve all storage accounts you have access to in your Azure subscription
    2. Sort the results based on the ___location (Azure region) of each storage account
    3. Display the full details of each storage account in the sorted order
    
    If you'd prefer a more concise output focusing on specific fields, you could modify the query. For example, to show only the name and ___location in a table format:
    
    az storage account list --query "sort_by(@, &___location)[].[name,___location]" -o table
    
    Make sure you're logged into the Azure CLI before running this command. If you're not logged in yet, you can do so with az login.
    
    

在代理模式下启用和禁用工具

你可能想要禁用或重新启用 GitHub Copilot for Azure 提供的某些工具。

  1. 在聊天窗格中选择工具图标。

    聊天窗格的屏幕截图,其中突出显示了工具图标。

  2. 使用工具列表旁边的复选框来启用/禁用工具(或工具组)。

    Visual Studio Code 中工具列表的屏幕截图。

在代理模式下工作的最佳做法

若要获得更好的代理结果,请考虑以下最佳做法。

  • 出现提示时,将 copilot-instructions.md 文件添加到工作区。

    Visual Studio Code 通知的屏幕截图,用于将 Copilot 指令文件添加到工作区。

  • 尽可能细化提示。 取代像 Generate a Python web application and deploy it to Azure 这样的提示,应将其拆分为几个提示,每个提示的责任范围较小。

  • 允许 GitHub Copilot 执行工作(而不是自己这样做)。 有时,GitHub Copilot 会请求使用“继续”按钮执行任务的权限。 虽然可以在聊天窗口外执行该任务,但应该允许 GitHub Copilot 改为执行该任务。 这样,GitHub Copilot 就可以保留其较大计划的当前状态的上下文。

  • 允许 GitHub Copilot 重复自己。 有时,GitHub Copilot 会要求允许重复执行任务。 这样做可以更好地了解项目文件的状态以及接下来应执行的操作。 应允许 GitHub Copilot 重复它需要执行的任务。

  • 表达首选项。 如果 GitHub Copilot 想要执行某个动作,但你希望它执行一个不同的动作,你可以指示它按照你首选的方式进行操作。 例如,如果它想要创建一个用来存放名为 \.azure 的 bicep 文件的文件夹,你可以进行干预,并要求它使用其他文件夹,例如通过提示:

    Instead of naming the folder `.azure`, please name it `infra`
    

可选:设置默认租户

如果有多个 Microsoft Entra ID 租户,则可以使用以下提示设置默认租户:

@azure /changeTenant

从顶部中心下拉列表中可用租户的列表中进行选择。

还可以在扩展设置中设置默认租户:

  1. 在 Visual Studio Code 中的活动栏上,选择“ 扩展”。 然后向下滚动到 GitHub Copilot for Azure

    显示 Visual Studio Code 中扩展列表中的 GitHub Copilot for Azure 的屏幕截图。

  2. 选择扩展项角的齿轮图标,然后从弹出菜单中选择“设置”。

    显示 GitHub Copilot for Azure 的弹出菜单的屏幕截图。

  3. “设置” 选项卡上,将 Azure Resource Graph 租户设置为Microsoft Entra 租户 ID。 可以在Azure 门户中找到Microsoft Entra 租户 ID。

    显示“设置”选项卡的屏幕截图,其中包含用于设置 Azure Resource Graph 租户的选项。