使用 Azure 命令行接口 (CLI) 创建认知服务资源Create a Cognitive Services resource using the Azure Command-Line Interface(CLI)

使用本快速入门可通过 Azure 命令行接口 (CLI) 开始使用 Azure 认知服务。Use this quickstart to get started with Azure Cognitive Services using the Azure Command Line Interface(CLI). 认知服务由你在 Azure 订阅中创建的 Azure 资源表示。Cognitive Services are represented by Azure resources that you create in your Azure subscription. 创建资源后,请使用生成的密钥和终结点对应用程序进行身份验证。After creating the resource, Use the keys and endpoint generated for you to authenticate your applications.

本快速入门介绍如何使用 Azure 命令行接口 (CLI) 注册 Azure 认知服务以及创建包含单服务或多服务订阅的帐户。In this quickstart, you'll learn how to sign up for Azure Cognitive Services and create an account that has a single-service or multi-service subscription, Using the Azure Command Line Interface(CLI). 这些服务由 Azure 资源表示,可用于连接到一个或多个 Azure 认知服务 API。These services are represented by Azure resources, which enable you to connect to one or more of the Azure Cognitive Services APIs.

可以通过两种不同的资源访问 Azure 认知服务:多服务资源或单服务资源。You can access Azure Cognitive Services through two different resources: A multi-service resource, or a single-service one.

  • 多服务资源:Multi-service resource:
    • 使用单个密钥和终结点访问多个 Azure 认知服务。Access multiple Azure Cognitive Services with a single key and endpoint.
    • 合并来自你使用的服务的计费。Consolidates billing from the services you use.
  • 单服务资源:Single-service resource:
    • 对于创建的每个服务,使用唯一密钥和终结点访问单个 Azure 认知服务。Access a single Azure Cognitive Service with a unique key and endpoint for each service created.
    • 使用免费层试用服务。Use the free tier to try out the service.

先决条件Prerequisites

安装 Azure CLI 并登录Install the Azure CLI and sign in

安装 Azure CLIInstall the Azure CLI. 若要登录到本地安装的 CLI,请运行 az login 命令:To sign into your local installation of the CLI, run the az login command:

az cloud set -n AzureChinaCloud
az login

也可以使用绿色的“尝试”按钮在浏览器中运行这些命令。You can also use the green Try It button to run these commands in your browser.

创建新的 Azure 认知服务资源组Create a new Azure Cognitive Services resource group

在创建认知服务资源之前,必须具有 Azure 资源组才能包含该资源。Before creating a Cognitive Services resource, you must have an Azure resource group to contain the resource. 在创建新资源时,可以选择新建资源组,或使用现有的资源组。When you create a new resource, you have the option to either create a new resource group, or use an existing one. 本文介绍如何创建新资源组。This article shows how to create a new resource group.

选择资源组位置Choose your resource group location

若要创建资源,需要为订阅提供一个可用的 Azure 位置。To create a resource, you'll need one of the Azure locations available for your subscription. 可以使用 az account list-locations 命令检索可用位置的列表。You can retrieve a list of available locations with the az account list-locations command. 可以从多个位置访问大部分认知服务。Most Cognitive Services can be accessed from several locations. 选择离你最近的位置,或查看哪些位置可供服务使用。Choose the one closest to you, or see which locations are available for the service.

重要

  • 请记住选择的 Azure 位置,因为在调用 Azure 认知服务时需要用到。Remember your Azure location, as you will need it when calling the Azure Cognitive Services.
  • 某些认知服务的可用性因区域而异。The availability of some Cognitive Services can vary by region. 有关详细信息,请参阅 Azure 产品在各区域中的推出情况For more information, see Azure products by region.
az account list-locations \
    --query "[].{Region:name}" \
    --out table

选择 Azure 位置后,在 Azure CLI 中使用 az group create 命令创建新的资源组。After you have your azure location, create a new resource group in the Azure CLI using the az group create command.

在以下示例中,请将 Azure 位置 chinaeast2 替换为你的订阅可用的某个 Azure 位置。In the example below, replace the azure location chinaeast2 with one of the Azure locations available for your subscription.

az group create \
    --name cognitive-services-resource-group \
    --location chinaeast2

创建认知服务资源Create a Cognitive Services resource

选择认知服务和定价层Choose a cognitive service and pricing tier

创建新资源时,需要知道你要使用哪种服务,以及所需的定价层(或 SKU)。When creating a new resource, you will need to know the "kind" of service you want to use, along with the pricing tier (or sku) you want. 创建资源时,需要使用此信息和其他信息作为参数。You will use this and other information as parameters when creating the resource.

多服务Multi-service

服务Service 种类Kind
多个服务。Multiple services. 有关更多详细信息,请参阅定价页。See the pricing page for more details. CognitiveServices

备注

下面的许多认知服务都有一个免费层,可以使用它来试用服务。Many of the Cognitive Services below have a free tier you can use to try the service. 若要使用免费层,请使用 F0 作为资源的 SKU。To use the free tier, use F0 as the sku for your resource.

影像Vision

服务Service 种类Kind
计算机视觉Computer Vision ComputerVision
人脸Face Face

语音Speech

服务Service 种类Kind
语音服务Speech Services SpeechServices
语音识别Speech Recognition SpeakerRecognition

语言Language

服务Service 种类Kind
LUISLUIS LUIS
文本分析Text Analytics TextAnalytics
文本翻译Text Translation TextTranslation

决策Decision

服务Service 种类Kind
内容审查器Content Moderator ContentModerator

可以使用 az cognitiveservices account list-kinds 命令查找可用认知服务“种类”的列表:You can find a list of available Cognitive Service "kinds" with the az cognitiveservices account list-kinds command:

az cognitiveservices account list-kinds

将新资源添加到资源组Add a new resource to your resource group

若要创建并订阅新的认知服务资源,请使用 az cognitiveservices account create 命令。To create and subscribe to a new Cognitive Services resource, use the az cognitiveservices account create command. 此命令会将新的可计费资源添加到前面创建的资源组。This command adds a new billable resource to the resource group created earlier. 创建新资源时,需要知道你要使用哪种服务,及其定价层(或 SKU)和 Azure 位置:When creating your new resource, you will need to know the "kind" of service you want to use, along with its pricing tier (or sku) and an Azure location:

可以使用以下命令为计算机视觉创建名为 computer-vision-resource 的 F0(免费)资源。You can create an F0 (free) resource for Computer Vision, named computer-vision-resource with the command below.

az cognitiveservices account create \
    --name anomaly-detector-resource \
    --resource-group cognitive-services-resource-group \
    --kind ComputerVision \
    --sku F0 \
    --location chinaeast2 \
    --yes

获取资源的密钥Get the keys for your resource

若要登录到本地安装的命令行接口 (CLI),请使用 az login 命令。To log into your local installation of the Command-Line Interface(CLI), use the az login command.

az cloud set -n AzureChinaCloud
az login

使用 az cognitiveservices account keys list 命令获取认知服务资源的密钥。Use the az cognitiveservices account keys list command to get the keys for your Cognitive Service resource.

    az cognitiveservices account keys list \
    --name anomaly-detector-resource \
    --resource-group cognitive-services-resource-group

配置用于身份验证的环境变量Configure an environment variable for authentication

应用程序需要对它们使用的认知服务的访问进行身份验证。Applications need to authenticate access to the Cognitive Services they use. 若要进行身份验证,我们建议创建一个环境变量来存储 Azure 资源的密钥。To authenticate, we recommend creating an environment variable to store the keys for your Azure Resources.

获得密钥后,将其写入运行应用程序的本地计算机上的新环境变量。After you have your key, write it to a new environment variable on the local machine running the application. 若要设置环境变量,请打开控制台窗口,并遵照适用于操作系统的说明。To set the environment variable, open a console window, and follow the instructions for your operating system. your-key 替换为资源的密钥之一。Replace your-key with one of the keys for your resource.

setx COGNITIVE_SERVICE_KEY "your-key"

添加环境变量后,可能需要重启任何正在运行的、需要读取环境变量的程序(包括控制台窗口)。After you add the environment variable, you may need to restart any running programs that will need to read the environment variable, including the console window. 例如,如果使用 Visual Studio 作为编辑器,请在运行示例之前重启 Visual Studio。For example, if you are using Visual Studio as your editor, restart Visual Studio before running the example.

定价层和计费Pricing tiers and billing

定价层(以及你收到的账单金额)基于你使用身份验证信息发送的事务数。Pricing tiers (and the amount you get billed) are based on the number of transactions you send using your authentication information. 每个定价层指定:Each pricing tier specifies the:

  • 每秒允许的最大事务数 (TPS)。maximum number of allowed transactions per second (TPS).
  • 在定价层中启用的服务功能。service features enabled within the pricing tier.
  • 预定义事务量的成本。The cost for a predefined amount of transactions. 超过此金额将产生定价详细信息中为服务指定的额外费用。Going above this amount will cause an extra charge as specified in the pricing details for your service.

获取资源的当前配额使用情况Get current quota usage for your resource

使用 az cognitiveservices account list-usage 命令获取认知服务资源的使用情况。Use the az cognitiveservices account list-usage command to get the usage for your Cognitive Service resource.

az cognitiveservices account list-usage \
    --name anomaly-detector-resource \
    --resource-group cognitive-services-resource-group \
    --subscription subscription-name

清理资源Clean up resources

如果想要清理并删除认知服务资源,可以删除资源或资源组。If you want to clean up and remove a Cognitive Services resource, you can delete it or the resource group. 删除资源组也会删除该组中包含的任何其他资源。Deleting the resource group also deletes any other resources contained in the group.

若要删除资源组及其关联的资源,请使用 az group delete 命令。To remove the resource group and its associated resources, use the az group delete command.

az group delete --name cognitive-services-resource-group

另请参阅See also