快速入門:建立 Azure AI 服務的多服務資源

瞭解如何為 Azure AI 服務建立和管理多服務資源。 多服務資源可讓您使用單一金鑰和端點來存取多個 Azure AI 服務。 它也會合並您所使用服務的帳單。

您可以透過兩個不同的資源存取 Azure AI 服務:多服務資源或單一服務資源。

  • 多服務資源:
    • 使用單一金鑰和端點存取多個 Azure AI 服務。
    • 合併您使用之服務的計費。
  • 單一服務資源:
    • 針對每個建立的服務,使用唯一的金鑰和端點來存取單一 Azure AI 服務。
    • 大部分的 Azure AI 服務都提供免費層來試用。

Azure AI 服務是您在 Azure 訂用帳戶下建立的 Azure 資源 。 建立資源之後,您可以使用產生的金鑰和端點來驗證您的應用程式。

具有多服務資源的支持服務

多服務資源可讓您使用單一密鑰和端點存取下列 Azure AI 服務。 使用這些連結來尋找快速入門文章、範例等等,以開始使用您的資源。

Service 描述
內容仲裁者圖示內容仲裁 (已淘汰) 偵測潛在的冒犯性或垃圾內容。
自訂視覺圖示自訂視覺 為您的企業自定義影像辨識。
文件智慧圖示文件智慧 將檔案轉換成智慧型手機資料驅動解決方案。
臉部圖示臉部 偵測和識別影像中的人員及表情。
語言圖示語言 使用領先業界的自然語言理解功能來建置應用程式。
語音圖示語音 語音轉換文字、文字到語音轉換、翻譯和說話者辨識。
翻譯工具圖示翻譯工具 使用 AI 支援的翻譯技術來翻譯超過 100 種使用中、高風險和瀕危的語言和方言。
視覺圖示視覺 分析影像和影片中的內容。

必要條件

  • 有效的 Azure 訂用帳戶 - 免費建立一個訂用帳戶。
  • Azure 帳戶必須已獲指派 Cognitive Services Contributor 角色,您才能同意負責任的 AI 條款並建立資源。 若要將此角色指派給您的帳戶,請遵循指派角色檔中的步驟,或連絡您的系統管理員。

建立新的多服務資源

多服務資源列在入口網站中的 Azure AI 服務>Azure AI 服務多服務帳戶之下。 若要建立多服務資源,請遵循下列指示:

  1. 選取此連結以建立多服務資源:https://portal.azure.com/#create/Microsoft.CognitiveServicesAllInOne

  2. [建立] 頁面上,提供下列資訊:

    專案詳細資料 描述
    訂用帳戶 選取您可用的一個 Azure 訂用帳戶。
    資源群組 將包含您的資源的 Azure AI 資源群組。 您可建立新的群組或新增群組至先前已有的群組。
    區域 Azure AI 服務執行個體的位置。 不同的位置可能會產生延遲,但是對於您資源的執行階段可用性沒有影響。
    名稱 描述您的 Azure AI 服務資源的名稱。 例如,MyCognitiveServicesResource
    定價層 Azure AI 服務帳戶的費用取決於您選擇的選項和使用方式。 如需詳細資訊,請參閱 API 價格詳細資料

    多服務資源建立畫面

  3. 視需要設定資源的其他設定、讀取並接受條件 (如適用),然後選取 [檢閱 + 建立]

提示

如果您的訂用帳戶不允許您建立 Azure AI 服務資源,您可能需使用 Azure 入口網站PowerShell 命令Azure CLI 命令來啟用該 Azure 資源提供者的權限。 如果您未擁有月租方案,請要求月租方案擁有者或具系統管理員角色的人員為您完成註冊,或要求向您的帳戶授與 /register/action 權限。

取得資源的金鑰

  1. 成功部署資源之後,請選取 [後續步驟>移至資源]。

    取得資源金鑰畫面

  2. 從開啟的快速入門窗格中,您可以存取資源端點和管理密鑰。

設定環境變數以進行驗證

您的應用程式必須經過驗證,才能存取 Azure AI 服務資源。 若要設定資源密鑰的環境變數,請開啟控制台視窗,並遵循作業系統和開發環境的指示。 若要設定 COGNITIVE_SERVICE_KEY 環境變數,請將 取代 your-key 為您資源的其中一個密鑰。

針對生產環境,請使用安全的方式來儲存和存取您的認證,例如密碼保護的秘密保存庫。 針對測試,寫入環境變數是標準做法,但請記住,環境變數會以純文本儲存在本機計算機上。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項 (例如 Azure Key Vault),請參閱 Azure AI 服務安全性文章。

PowerShell 包含可延伸的解決方案秘密管理,用於將安全字串儲存在秘密存放區Azure KeyVault 等平臺。

若要設定秘密存放區來裝載安全字串,請參閱開始使用秘密存放區一文。 然後使用下列範例來儲存 API 金鑰。

Set-Secret -Name COGNITIVE_SERVICE_KEY -value your-key

針對測試環境,您可以使用PowerShell的語法,只設定目前工作階段的 $Env: 環境變數,或 setx 命令在工作階段之間保留變數。 請記住,這些值會以純文本儲存在 Windows 登錄機碼中。

$Env:COGNITIVE_SERVICE_KEY = your-key
setx COGNITIVE_SERVICE_KEY your-key

使用 setx新增環境變數之後,您可能需要重新啟動任何執行中的控制台或其他需要讀取環境變數的程式。 例如,如果您使用 Visual Studio 作為編輯器,請在執行範例之前重新啟動 Visual Studio。

若要設定語音資源區域的環境變數,請遵循相同的步驟。 設定 COGNITIVE_SERVICE_REGION 為資源的區域。 例如: westus

清除資源

如果您想要清除和移除 Azure AI 服務訂用帳戶,則可以刪除資源或資源群組。 刪除資源群組也會刪除群組中包含的任何其他資源。

  1. 在 Azure 入口網站 中,展開左側的功能表以開啟服務的功能表,然後選擇 [資源群組] 以顯示資源群組的清單。
  2. 找出包含要刪除之資源的資源群組。
  3. 如果您想要刪除整個資源群組,請選擇資源群組名。 在下一個頁面上,選取 [刪除資源群組],然後確認。
  4. 如果您想要只刪除 Azure AI 服務資源,選取資源群組以查看其中的所有資源。 在下一個頁面上,選取您要刪除的資源、選取該數據列的省略號功能表,然後選取 [ 刪除]。

使用此快速入門,使用 Azure 命令列介面 (CLI) 命令來建立 Azure AI 服務資源。 建立資源之後,請使用為您產生的金鑰和端點來驗證應用程式。

Azure AI 服務是雲端式的人工智慧 (AI) 服務,可協助開發人員建置認知智慧應用程式,且無須具備直接的 AI 或資料科學技能或知識。 它們可透過熱門開發語言的 REST API 和用戶端連結庫 SDK 取得。 Azure AI 服務可讓開發人員使用認知解決方案,輕鬆地將認知功能新增至其應用程式,以便查看、聆聽、說話和分析。

必要條件

  • 有效的 Azure 訂用帳戶 - 免費建立一個 訂用帳戶。
  • Azure CLI
  • Azure 帳戶必須已獲指派 Cognitive Services Contributor 角色,您才能同意負責任的 AI 條款並建立資源。 若要將此角色指派給您的帳戶,請遵循指派角色檔中的步驟,或連絡您的系統管理員。
  • 如果您打算在 Azure AI 視覺中使用空間分析,或在 Azure AI 語言中使用適用於健康情況的 文字分析,則必須從 Azure 入口網站 建立您的第一個視覺或語言資源,以便檢閱及確認條款和條件。 您可以在這裏執行此動作: Azure AI 語言Azure AI 視覺。 之後,您可以使用相同 Azure 訂用帳戶下的任何部署工具(例如 SDK、CLI 或 ARM 範本)來建立後續資源。

安裝 Azure CLI 並登入

安裝 Azure CLI。 若要登入 CLI 的本機安裝,請執行 az login 命令:

az login

您也可以使用綠色 的 [試用] 按鈕,在瀏覽器中執行這些命令。

建立新的 Azure AI 服務資源群組

建立 Azure AI 服務資源之前,您必須有包含資源的 Azure 資源群組。 當您建立新的資源時,您可以建立新的資源群組,或使用現有的資源群組。 本文說明如何建立新資源群組。

選擇您的資源群組位置

若要建立資源,您需要訂用帳戶可用的其中一個 Azure 位置。 您可以使用 az account list-locations 命令來擷取可用位置的清單。 大部分的 Azure AI 服務都可以從數個位置存取。 選擇最接近您的位置,或查看服務可用的位置。

重要

  • 請記住您的 Azure 位置,因為您在呼叫 Azure AI 服務資源時將會用到。
  • 部分 Azure AI 服務的可用性會因區域而異。 如需詳細資訊,請參閱 依區域排序的 Azure 產品。
az account list-locations --query "[].{Region:name}" --out table

取得 Azure 位置之後,請使用 az group create 命令,在 Azure CLI 中建立新的資源群組。 在下列範例中,將 Azure 位置 westus2 取代為您訂用帳戶可用的其中一個 Azure 位置。

az group create --name ai-services-resource-group --location westus2

建立 Azure AI 服務資源

選擇服務和定價層

當您建立新的資源時,您必須知道您想要使用的服務種類,以及您想要的 定價層 (或 SKU)。 當您建立資源時,您將使用此和其他資訊做為參數。

下表提供 Azure AI 服務產品和定價的相關信息。

多服務

服務 種類
多個服務。 如需詳細資訊,請參閱價格網頁 CognitiveServices

視覺

服務 種類
視覺 ComputerVision
自訂視覺 - 預測 CustomVision.Prediction
自訂視覺 - 訓練 CustomVision.Training
臉部 Face
文件智慧服務 FormRecognizer

Speech

服務 種類
Speech SpeechServices

語言

服務 種類
Language Understanding (LUIS) LUIS
QnA Maker QnAMaker
語言 TextAnalytics
文字翻譯 TextTranslation

Decision

服務 種類
異常偵測器 AnomalyDetector
內容仲裁 ContentModerator
個人化服務工具 Personalizer

Azure OpenAI

服務 種類
Azure OpenAI OpenAI

定價層和計費

定價層(以及您計費的金額)是以您使用驗證資訊所傳送的交易數目為基礎。 每個定價層都會指定:

  • 每秒允許的交易數目上限(TPS)。
  • 定價層內啟用的服務功能。
  • 預先定義交易數目的成本。 高於此號碼會造成額外費用,如服務定價詳細數據中所指定。

注意

許多 Azure AI 服務都有一個免費層,可供您用來試用服務。 若要使用免費層,請使用 F0 作為您資源的定價層。

您可以使用 az cognitiveservices account list-kinds 命令來尋找可用 Azure AI 服務「種類」的清單:

az cognitiveservices account list-kinds

將新的資源新增至您的資源群組

若要建立並訂閱新的 Azure AI 服務資源,請使用 az cognitiveservices account create 命令。 此命令會將新的可計費資源新增至您稍早建立的資源群組。 當您建立新的資源時,您必須知道您想要使用的「種類」服務,以及其定價層(或 SKU)和 Azure 位置:

您可以使用下列命令建立名為 multi-service-resource 的標準 S0 多服務資源。

az cognitiveservices account create --name multi-service-resource --resource-group ai-services-resource-group  --kind CognitiveServices --sku F0 --location westus2 --yes

提示

如果您的訂用帳戶不允許您建立 Azure AI 服務資源,您可能需使用 Azure 入口網站PowerShell 命令Azure CLI 命令來啟用該 Azure 資源提供者的權限。 如果您未擁有月租方案,請要求月租方案擁有者或具系統管理員角色的人員為您完成註冊,或要求向您的帳戶授與 /register/action 權限。

取得資源的金鑰

若要登入命令行介面(CLI)的本機安裝,請使用 az login 命令。

az login

使用 az cognitiveservices account keys list 命令來取得資源的金鑰。

az cognitiveservices account keys list  --name multi-service-resource --resource-group ai-services-resource-group

設定環境變數以進行驗證

您的應用程式必須經過驗證,才能存取 Azure AI 服務資源。 若要設定資源密鑰的環境變數,請開啟控制台視窗,並遵循作業系統和開發環境的指示。 若要設定 COGNITIVE_SERVICE_KEY 環境變數,請將 取代 your-key 為您資源的其中一個密鑰。

針對生產環境,請使用安全的方式來儲存和存取您的認證,例如密碼保護的秘密保存庫。 針對測試,寫入環境變數是標準做法,但請記住,環境變數會以純文本儲存在本機計算機上。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項 (例如 Azure Key Vault),請參閱 Azure AI 服務安全性文章。

PowerShell 包含可延伸的解決方案秘密管理,用於將安全字串儲存在秘密存放區Azure KeyVault 等平臺。

若要設定秘密存放區來裝載安全字串,請參閱開始使用秘密存放區一文。 然後使用下列範例來儲存 API 金鑰。

Set-Secret -Name COGNITIVE_SERVICE_KEY -value your-key

針對測試環境,您可以使用PowerShell的語法,只設定目前工作階段的 $Env: 環境變數,或 setx 命令在工作階段之間保留變數。 請記住,這些值會以純文本儲存在 Windows 登錄機碼中。

$Env:COGNITIVE_SERVICE_KEY = your-key
setx COGNITIVE_SERVICE_KEY your-key

使用 setx新增環境變數之後,您可能需要重新啟動任何執行中的控制台或其他需要讀取環境變數的程式。 例如,如果您使用 Visual Studio 作為編輯器,請在執行範例之前重新啟動 Visual Studio。

若要設定語音資源區域的環境變數,請遵循相同的步驟。 設定 COGNITIVE_SERVICE_REGION 為資源的區域。 例如: westus

定價層和計費

定價層(以及您支付的金額)是以您使用驗證資訊傳送的交易數目為基礎。 每個定價層都會指定:

  • 每秒允許的交易數目上限 (TPS)。
  • 定價層內啟用的服務功能。
  • 預先定義交易數目的成本。 超過此金額會導致您的服務定價詳細數據中指定的額外費用。

取得資源目前的配額使用量

使用 az cognitiveservices account list-usage 命令來取得資源的使用量。

az cognitiveservices account list-usage --name multi-service-resource --resource-group ai-services-resource-group --subscription subscription-name

清除資源

如果您想要清除和移除 Azure AI 服務資源,則可以刪除該資源或資源群組。 刪除資源群組也會刪除群組中包含的任何其他資源。

若要移除資源群組及其相關聯的資源,請使用 az group delete 命令。

az group delete --name ai-services-resource-group

使用此快速入門,使用 Azure PowerShell 命令建立 Azure AI 服務資源。 建立資源之後,請使用為您產生的金鑰和端點來驗證應用程式。

Azure AI 服務是雲端式的人工智慧 (AI) 服務,可協助開發人員建置認知智慧應用程式,且無須具備直接的 AI 或資料科學技能或知識。 它們可透過熱門開發語言的 REST API 和用戶端連結庫 SDK 取得。 Azure AI 服務可讓開發人員使用認知解決方案,輕鬆地將認知功能新增至其應用程式,以便查看、聆聽、說話和分析。

必要條件

  • 有效的 Azure 訂用帳戶 - 免費建立一個 訂用帳戶。
  • Azure PowerShell
  • Azure 帳戶必須已獲指派 Cognitive Services Contributor 角色,您才能同意負責任的 AI 條款並建立資源。 若要將此角色指派給您的帳戶,請遵循指派角色檔中的步驟,或連絡您的系統管理員。
  • 如果您打算在 Azure AI 視覺中使用空間分析,或在 Azure AI 語言中使用適用於健康情況 文字分析,則必須從 Azure 入口網站 建立您的第一個視覺或語言資源,以便檢閱及確認條款和條件。 您可以在這裏執行此動作: Azure AI 語言Azure AI 視覺。 之後,您可以使用相同 Azure 訂用帳戶下的任何部署工具(例如 SDK、CLI 或 ARM 範本)來建立後續資源。

安裝 Azure PowerShell 並登入

安裝 Azure PowerShell。 若要登入,請執行 連線-AzAccount 命令:

Connect-AzAccount

您也可以使用綠色 的 [試用] 按鈕,在瀏覽器中執行這些命令。

建立新的 Azure AI 服務資源群組

建立 Azure AI 服務資源之前,您必須有包含資源的 Azure 資源群組。 當您建立新的資源時,您可以建立新的資源群組,或使用現有的資源群組。 本文說明如何建立新資源群組。

選擇您的資源群組位置

若要建立資源,您需要訂用帳戶可用的其中一個 Azure 位置。 您可以使用 Get-AzLocation 命令來擷取可用位置的清單。 大部分的 Azure AI 服務都可以從數個位置存取。 選擇最接近您的位置,或查看服務可用的位置。

重要

  • 請記住您的 Azure 位置,因為您在呼叫 Azure AI 服務資源時將會用到。
  • 部分 Azure AI 服務的可用性會因區域而異。 如需詳細資訊,請參閱 依區域排序的 Azure 產品。
Get-AzLocation | Select-Object -Property Location, DisplayName

取得 Azure 位置之後,請使用 New-AzResourceGroup 命令在 Azure PowerShell 中建立新的資源群組。 在下列範例中,將 Azure 位置 westus2 取代為您訂用帳戶可用的其中一個 Azure 位置。

New-AzResourceGroup -Name ai-services-resource-group -Location westus2

建立 Azure AI 服務資源

選擇服務和定價層

當您建立新的資源時,您必須知道您想要使用的服務種類,以及您想要的 定價層 (或 SKU)。 當您建立資源時,您將使用此和其他資訊做為參數。

下表提供 Azure AI 服務產品和定價的相關信息。

多服務

服務 種類
多個服務。 如需詳細資訊,請參閱價格網頁 CognitiveServices

視覺

服務 種類
視覺 ComputerVision
自訂視覺 - 預測 CustomVision.Prediction
自訂視覺 - 訓練 CustomVision.Training
臉部 Face
文件智慧服務 FormRecognizer

Speech

服務 種類
Speech SpeechServices

語言

服務 種類
Language Understanding (LUIS) LUIS
QnA Maker QnAMaker
語言 TextAnalytics
文字翻譯 TextTranslation

Decision

服務 種類
異常偵測器 AnomalyDetector
內容仲裁 ContentModerator
個人化服務工具 Personalizer

Azure OpenAI

服務 種類
Azure OpenAI OpenAI

定價層和計費

定價層(以及您計費的金額)是以您使用驗證資訊所傳送的交易數目為基礎。 每個定價層都會指定:

  • 每秒允許的交易數目上限(TPS)。
  • 定價層內啟用的服務功能。
  • 預先定義交易數目的成本。 高於此號碼會造成額外費用,如服務定價詳細數據中所指定。

注意

許多 Azure AI 服務都有一個免費層,可供您用來試用服務。 若要使用免費層,請使用 F0 作為您資源的定價層。

您可以使用 Get-AzCognitiveServicesAccountType 命令,找到可用的 Azure AI 服務「種類」清單:

Get-AzCognitiveServicesAccountType

將新的資源新增至您的資源群組

若要建立及訂閱新的 Azure AI 服務資源,請使用 New-AzCognitiveServicesAccount 命令。 此命令會將新的可計費資源新增至您稍早建立的資源群組。 當您建立新的資源時,您必須知道您想要使用的「種類」服務,以及其定價層(或 SKU)和 Azure 位置:

您可以使用下列命令建立名為 multi-service-resource 的標準 S0 多服務資源。

New-AzCognitiveServicesAccount -ResourceGroupName ai-services-resource-group -Name multi-service-resource -Type CognitiveServices -SkuName F0 -Location westus2

提示

如果您的訂用帳戶不允許您建立 Azure AI 服務資源,您可能需要使用 Azure 入口網站、Azure PowerShell 命令或 Azure CLI 命令來啟用該 Azure 資源提供者的許可權。 如果您未擁有月租方案,請要求月租方案擁有者或具系統管理員角色的人員為您完成註冊,或要求向您的帳戶授與 /register/action 權限。

取得資源的金鑰

使用 Get-AzCognitiveServicesAccountKey 命令來取得資源的密鑰。

Get-AzCognitiveServicesAccountKey -Name multi-service-resource -ResourceGroupName ai-services-resource-group

設定環境變數以進行驗證

您的應用程式必須經過驗證,才能存取 Azure AI 服務資源。 若要設定資源密鑰的環境變數,請開啟控制台視窗,並遵循作業系統和開發環境的指示。 若要設定 COGNITIVE_SERVICE_KEY 環境變數,請將 取代 your-key 為您資源的其中一個密鑰。

針對生產環境,請使用安全的方式來儲存和存取您的認證,例如密碼保護的秘密保存庫。 針對測試,寫入環境變數是標準做法,但請記住,環境變數會以純文本儲存在本機計算機上。

提示

請勿將金鑰直接包含在您的程式代碼中,且絕不會公開發佈。 如需更多驗證選項 (例如 Azure Key Vault),請參閱 Azure AI 服務安全性文章。

PowerShell 包含可延伸的解決方案秘密管理,用於將安全字串儲存在秘密存放區Azure KeyVault 等平臺。

若要設定秘密存放區來裝載安全字串,請參閱開始使用秘密存放區一文。 然後使用下列範例來儲存 API 金鑰。

Set-Secret -Name COGNITIVE_SERVICE_KEY -value your-key

針對測試環境,您可以使用PowerShell的語法,只設定目前工作階段的 $Env: 環境變數,或 setx 命令在工作階段之間保留變數。 請記住,這些值會以純文本儲存在 Windows 登錄機碼中。

$Env:COGNITIVE_SERVICE_KEY = your-key
setx COGNITIVE_SERVICE_KEY your-key

使用 setx新增環境變數之後,您可能需要重新啟動任何執行中的控制台或其他需要讀取環境變數的程式。 例如,如果您使用 Visual Studio 作為編輯器,請在執行範例之前重新啟動 Visual Studio。

若要設定語音資源區域的環境變數,請遵循相同的步驟。 設定 COGNITIVE_SERVICE_REGION 為資源的區域。 例如: westus

定價層和計費

定價層(以及您支付的金額)是以您使用驗證資訊傳送的交易數目為基礎。 每個定價層都會指定:

  • 每秒允許的交易數目上限 (TPS)。
  • 定價層內啟用的服務功能。
  • 預先定義交易數目的成本。 超過此金額會導致您的服務定價詳細數據中指定的額外費用。

取得資源目前的配額使用量

使用 Get-AzCognitiveServicesAccountUsage 命令來取得資源的使用量。

Get-AzCognitiveServicesAccountUsage -ResourceGroupName ai-services-resource-group -Name multi-service-resource

清除資源

如果您想要清除和移除 Azure AI 服務資源,則可以刪除該資源或資源群組。 刪除資源群組也會刪除群組中包含的任何其他資源。

若要移除資源群組及其相關聯的資源,請使用 Remove-AzResourceGroup 命令。

Remove-AzResourceGroup -Name ai-services-resource-group

參考文檔 | 庫原始程式碼 | 套件 (NuGet)範例 |

C# 必要條件

  • 有效的 Azure 訂用帳戶 - 免費建立一個訂用帳戶。
  • .NET Core目前版本。
  • Azure 帳戶必須已獲指派 Cognitive Services Contributor 角色,您才能同意負責任的 AI 條款並建立資源。 若要將此角色指派給您的帳戶,請遵循指派角色檔中的步驟,或連絡您的系統管理員。
  • 如果您打算在 Azure AI 視覺中使用空間分析,或在 Azure AI 語言中 文字分析 健康情況,則必須從 Azure 入口網站 建立您的第一個視覺或語言資源,以便檢閱並確認條款和條件。 您可以在這裏執行此動作: Azure AI 語言Azure AI 視覺。 之後,您可以使用相同 Azure 訂用帳戶下的任何部署工具(例如 SDK、CLI 或 ARM 範本)來建立後續資源。

建立 Azure 服務主體

若要讓應用程式與 Azure 帳戶互動,您需要 Azure 服務主體來管理許可權。 遵循建立 Azure 服務主體中的指示。

當您建立服務主體時,您會看到它具有秘密值、標識碼和應用程式識別碼。 將應用程式識別碼和秘密儲存到暫存位置,以供後續步驟使用。

建立資源群組

建立 Azure AI 服務資源之前,您的帳戶必須有包含資源的 Azure 資源群組。 如果您還沒有資源群組,請在 Azure 入口網站 中建立一個資源群組,再繼續。

建立新的 C# 應用程式

建立新的 .NET Core 應用程式。 在主控台視窗中 (例如 cmd、PowerShell 或 Bash),使用 dotnet new 命令建立名為 azure-management-quickstart 的新主控台應用程式。 此命令會建立具有單一原始程式檔的簡單 「Hello World」 C# 專案: program.cs

dotnet new console -n azure-management-quickstart

將目錄變更為新建立的應用程式資料夾。 您可以使用下列命令來建置應用程式:

dotnet build

建置輸出應該不會有警告或錯誤。

...
Build succeeded.
 0 Warning(s)
 0 Error(s)
...

安裝用戶端程式庫

在應用程式目錄中,使用下列命令安裝適用於 .NET 的 Azure 管理用戶端連結庫:

dotnet add package Azure.ResourceManager.CognitiveServices
dotnet add package Microsoft.Azure.Management.Fluent
dotnet add package Microsoft.Azure.Management.ResourceManager.Fluent

如果您使用 Visual Studio IDE,用戶端連結庫會以可下載的 NuGet 套件的形式提供。

匯入程式庫

開啟 program.cs ,並將下列 using 語句新增至檔案頂端:

using System;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.CognitiveServices;
using Microsoft.Azure.Management.CognitiveServices.Models;

驗證用戶端

使用您建立的服務主體和 Azure 帳戶資訊,將下列欄位新增至program.cs根目錄,並填入其值。

const string  service_principal_application_id = "PASTE_YOUR_SERVICE_PRINCIPAL_APPLICATION_ID_HERE";
const string  service_principal_secret = "PASTE_YOUR_SERVICE_PRINCIPAL_SECRET_HERE";

/* The ID of your Azure subscription. You can find this in the Azure Dashboard under Home > Subscriptions. */
const string  subscription_id = "PASTE_YOUR_SUBSCRIPTION_ID_HERE";

/* The Active Directory tenant ID. You can find this in the Azure Dashboard under Home > Azure Active Directory. */
const string  tenant_id = "PASTE_YOUR_TENANT_ID_HERE";

/* The name of the Azure resource group in which you want to create the resource.
You can find resource groups in the Azure Dashboard under Home > Resource groups. */
const string  resource_group_name = "PASTE_YOUR_RESOURCE_GROUP_NAME_HERE";

/* The name of the custom subdomain to use when you create the resource. This is optional.
For example, if you create a Bing Search v7 resource with the custom subdomain name 'my-search-resource',
your resource would have the endpoint https://my-search-resource.cognitiveservices.azure.com/.
Note not all Cognitive Services allow custom subdomain names. */
const string subdomain_name = "PASTE_YOUR_SUBDOMAIN_NAME_HERE";

然後,在您的 Main 方法中,使用這些值來建構 CognitiveServicesManagementClient 物件。 所有 Azure 管理作業都需要此物件。

var service_principal_credentials = new ServicePrincipalLoginInformation ();
service_principal_credentials.ClientId = service_principal_application_id;
service_principal_credentials.ClientSecret = service_principal_secret;

var credentials = SdkContext.AzureCredentialsFactory.FromServicePrincipal(service_principal_application_id, service_principal_secret, tenant_id, AzureEnvironment.AzureGlobalCloud);
var client = new CognitiveServicesManagementClient(credentials);
client.SubscriptionId = subscription_id;

呼叫管理方法

將下列程式代碼新增至 Main 方法以列出可用的資源、建立範例資源、列出您自己的資源,然後刪除範例資源。 您將在後續步驟中定義這些方法。

    // Uncomment to list all available resource kinds, SKUs, and locations for your Azure account:
    //list_available_kinds_skus_locations(client);

    // Create a resource with kind TextTranslation, F0 (free tier), location global.
    create_resource(client, "test_resource", "TextTranslation", "F0", "Global");

    // List all resources for your Azure account and resource group:
    list_resources(client);

    // Delete the resource.
    delete_resource(client, "test_resource");

    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();

建立 Azure AI 服務資源 (C#)

若要建立並訂閱新的 Azure AI 服務資源,請使用 Create 方法。 這個方法會將新的可計費資源新增至您傳入的資源群組。 建立新的資源時,您必須知道您想要使用的「種類」服務,以及其定價層(或 SKU)和 Azure 位置。 下列方法會採用所有這些 作為自變數,並建立資源。

static void create_resource(CognitiveServicesManagementClient client, string resource_name, string kind, string account_tier, string location)
{
    Console.WriteLine("Creating resource: " + resource_name + "...");
    /* NOTE If you do not want to use a custom subdomain name, remove the customSubDomainName
    property from CognitiveServicesAccountProperties. */
    CognitiveServicesAccount parameters = 
        new CognitiveServicesAccount(null, null, kind, location, resource_name, new CognitiveServicesAccountProperties(customSubDomainName : subdomain_name), new Sku(account_tier));
    var result = client.Accounts.Create(resource_group_name, resource_name, parameters);
    Console.WriteLine("Resource created.");
    Console.WriteLine("ID: " + result.Id);
    Console.WriteLine("Kind: " + result.Kind);
    Console.WriteLine();
}

選擇服務和定價層

當您建立新的資源時,您必須知道您想要使用的「種類」服務,以及您想要的 定價層 (或 SKU)。 建立資源時,您將使用此和其他資訊做為參數。 您可以在指令碼中呼叫下列方法,以尋找可用的 Azure AI 服務「種類」清單:

static void list_available_kinds_skus_locations(CognitiveServicesManagementClient client)
{

    Console.WriteLine("Available SKUs:");
    var result = client.ResourceSkus.List();
    Console.WriteLine("Kind\tSKU Name\tSKU Tier\tLocations");
    foreach (var x in result) {
        var locations = "";
        foreach (var region in x.Locations)
        {
            locations += region;
        }
        Console.WriteLine(x.Kind + "\t" + x.Name + "\t" + x.Tier + "\t" + locations);
    };
}

下表提供 Azure AI 服務產品和定價的相關信息。

多服務

服務 種類
多個服務。 如需詳細資訊,請參閱價格網頁 CognitiveServices

視覺

服務 種類
視覺 ComputerVision
自訂視覺 - 預測 CustomVision.Prediction
自訂視覺 - 訓練 CustomVision.Training
臉部 Face
文件智慧服務 FormRecognizer

Speech

服務 種類
Speech SpeechServices

語言

服務 種類
Language Understanding (LUIS) LUIS
QnA Maker QnAMaker
語言 TextAnalytics
文字翻譯 TextTranslation

Decision

服務 種類
異常偵測器 AnomalyDetector
內容仲裁 ContentModerator
個人化服務工具 Personalizer

Azure OpenAI

服務 種類
Azure OpenAI OpenAI

定價層和計費

定價層(以及您計費的金額)是以您使用驗證資訊所傳送的交易數目為基礎。 每個定價層都會指定:

  • 每秒允許的交易數目上限(TPS)。
  • 定價層內啟用的服務功能。
  • 預先定義交易數目的成本。 高於此號碼會造成額外費用,如服務定價詳細數據中所指定。

注意

許多 Azure AI 服務都有一個免費層,可供您用來試用服務。 若要使用免費層,請使用 F0 作為您資源的定價層。

檢視您的資源

若要檢視 Azure 帳戶下的所有資源(跨所有資源群組),請使用下列方法:

static void list_resources(CognitiveServicesManagementClient client)
{
    Console.WriteLine("Resources in resource group: " + resource_group_name);
    var result = client.Accounts.ListByResourceGroup(resource_group_name);
    foreach (var x in result)
    {
        Console.WriteLine("ID: " + x.Id);
        Console.WriteLine("Name: " + x.Name);
        Console.WriteLine("Type: " + x.Type);
        Console.WriteLine("Kind: " + x.Kind);
        Console.WriteLine();
    }
}

刪除資源

下列方法會從指定的資源群組中刪除指定的資源。

static void delete_resource(CognitiveServicesManagementClient client, string resource_name)
{
    Console.WriteLine("Deleting resource: " + resource_name + "...");
    client.Accounts.Delete (resource_group_name, resource_name);

    Console.WriteLine("Resource deleted.");
    Console.WriteLine();
}

執行應用程式

使用 dotnet run 命令從應用程式目錄執行應用程式。

dotnet run

參考文件 | 庫原始碼 | 套件 (Maven)

Java 必要條件

  • 有效的 Azure 訂用帳戶 - 免費建立一個訂用帳戶。
  • Java 開發工具套件的 目前版本(JDK)
  • Gradle 建置工具或其他相依性管理員。
  • Azure 帳戶必須已獲指派 Cognitive Services Contributor 角色,您才能同意負責任的 AI 條款並建立資源。 若要將此角色指派給您的帳戶,請遵循指派角色檔中的步驟,或連絡您的系統管理員。
  • 如果您打算在 Azure AI 視覺中使用空間分析,或在 Azure AI 語言中使用 文字分析 for Health,則必須從 Azure 入口網站 建立您的第一個視覺或語言資源,以便檢閱及確認條款和條件。 您可以在這裏執行此動作: Azure AI 語言Azure AI 視覺。 之後,您可以使用相同 Azure 訂用帳戶下的任何部署工具(例如 SDK、CLI 或 ARM 範本)來建立後續資源。

建立 Azure 服務主體

若要讓應用程式與 Azure 帳戶互動,您需要 Azure 服務主體來管理許可權。 遵循建立 Azure 服務主體中的指示。

當您建立服務主體時,您會看到它具有秘密值、標識碼和應用程式識別碼。 將應用程式識別碼和秘密儲存到暫存位置,以供後續步驟使用。

建立資源群組

建立 Azure AI 服務資源之前,您的帳戶必須有包含資源的 Azure 資源群組。 如果您還沒有資源群組,請在 Azure 入口網站建立一個資源群組,然後再繼續。

建立新的 Java 應用程式

在主控台視窗中(例如 cmd、PowerShell 或 Bash),為您的應用程式建立新的目錄,然後流覽至它。

mkdir myapp && cd myapp

gradle init從工作目錄執行 命令。 此命令會建立 Gradle 的基本組建檔案,包括 用於運行時間建立和設定應用程式的 build.gradle.kts

gradle init --type basic

當系統提示您選擇 DSL 時,請選取 [Kotlin]。

從您的工作目錄中,執行下列命令:

mkdir -p src/main/java

安裝用戶端程式庫

本快速入門使用 Gradle 相依性管理員。 您可以在 Maven 中央存放庫找到其他相依性管理員的用戶端連結庫和資訊。

在專案的 build.gradle.kts 檔案中,包含客戶端連結庫做為implementation語句,以及必要的外掛程式和設定。

plugins {
    java
    application
}
application {
    mainClass.set("FormRecognizer")
}
repositories {
    mavenCentral()
}
dependencies {
    implementation(group = "com.microsoft.azure", name = "azure-mgmt-cognitiveservices", version = "1.10.0-beta")
}

匯入程式庫

流覽至新的 src/main/java 資料夾,並建立名為 Management.java 的檔案。 在慣用的編輯器或 IDE 中開啟它,並新增下列 import 語句:

import com.azure.core.management.*;
import com.azure.core.management.profile.*;
import com.azure.identity.*;
import com.azure.resourcemanager.cognitiveservices.*;
import com.azure.resourcemanager.cognitiveservices.implementation.*;
import com.azure.resourcemanager.cognitiveservices.models.*;

import java.io.*;
import java.lang.Object.*;
import java.util.*;
import java.net.*;

驗證用戶端

在 Management.java新增類別,然後在其中新增下列欄位及其值。 使用您建立的服務主體和其他 Azure 帳戶資訊,填入其值。

/*
Be sure to use the service pricipal application ID, not simply the ID. 
*/

private static String applicationId = "PASTE_YOUR_SERVICE_PRINCIPAL_APPLICATION_ID_HERE";
private static String applicationSecret = "PASTE_YOUR_SERVICE_PRINCIPAL_SECRET_HERE";

/* The ID of your Azure subscription. You can find this in the Azure Dashboard under Home > Subscriptions. */
private static String subscriptionId = "PASTE_YOUR_SUBSCRIPTION_ID_HERE";

/* The Active Directory tenant ID. You can find this in the Azure Dashboard under Home > Azure Active Directory. */
private static String tenantId = "PASTE_YOUR_TENANT_ID_HERE";

/* The name of the Azure resource group in which you want to create the resource.
You can find resource groups in the Azure Dashboard under Home > Resource groups. */
private static String resourceGroupName = "PASTE_YOUR_RESOURCE_GROUP_NAME_HERE";

/* The name of the custom subdomain to use when you create the resource. This is optional.
For example, if you create a Bing Search v7 resource with the custom subdomain name 'my-search-resource',
your resource would have the endpoint https://my-search-resource.cognitiveservices.azure.com/.
Note not all Cognitive Services allow custom subdomain names. */
private static String subDomainName = "PASTE_YOUR_SUBDOMAIN_NAME_HERE";

然後,在您的 main 方法中,使用這些值來建構 CognitiveServicesManager 物件。 所有 Azure 管理作業都需要此物件。

/* For more information see:
https://github.com/Azure/azure-sdk-for-java/blob/main/sdk/resourcemanager/docs/AUTH.md
*/

ClientSecretCredential credential = new ClientSecretCredentialBuilder()
    .clientId(applicationId)
    .clientSecret(applicationSecret)
    .tenantId(tenantId)
    .build();
AzureProfile profile = new AzureProfile(tenantId, subscriptionId, AzureEnvironment.AZURE);

CognitiveServicesManager client = CognitiveServicesManager.authenticate(credential, profile);

呼叫管理方法

將下列程式代碼新增至 Main 方法以列出可用的資源、建立範例資源、列出您自己的資源,然後刪除範例資源。 您將在後續步驟中定義這些方法。

String resourceName = "test_resource";
String resourceKind = "TextTranslation";
String resourceSku = "F0";
Region resourceRegion = Region.US_WEST;

// Uncomment to list all available resource kinds, SKUs, and locations for your Azure account.
// list_available_kinds_skus_locations (client);

// Create a resource with kind Text Translation, SKU F0 (free tier), location US West.
String resourceId = create_resource (client, resourceName, resourceGroupName, resourceKind, resourceSku, resourceRegion);

// Uncomment this to list all resources for your Azure account.
// list_resources (client, resourceGroupName);

// Delete the resource.
delete_resource (client, resourceId);

/* NOTE: When you delete a resource, it is only soft-deleted. You must also purge it. Otherwise, if you try to create another
resource with the same name or custom subdomain, you will receive an error stating that such a resource already exists. */
purge_resource (client, resourceName, resourceGroupName, resourceRegion);

建立 Azure AI 服務資源 (Java)

若要建立並訂閱新的 Azure AI 服務資源,請使用 create 方法。 這個方法會將新的可計費資源新增至您傳入的資源群組。 建立新的資源時,您必須知道您想要使用的「種類」服務,以及其定價層(或 SKU)和 Azure 位置。 下列方法會採用所有這些 作為自變數,並建立資源。

public static String create_resource (CognitiveServicesManager client, String resourceName, String resourceGroupName, String resourceKind, String resourceSku, Region resourceRegion) {
    System.out.println ("Creating resource: " + resourceName + "...");

    /* NOTE: If you do not want to use a custom subdomain name, remove the withCustomSubDomainName
    setter from the AccountProperties object. */
    Account result = client.accounts().define(resourceName)
        .withExistingResourceGroup(resourceGroupName)
        // Note: Do not call withRegion() first, as it does not exist on the Blank interface returned by define().
        .withRegion(resourceRegion)
        .withKind(resourceKind)
        .withSku(new Sku().withName(resourceSku))
        .withProperties(new AccountProperties().withCustomSubDomainName(subDomainName))
        .create();

    System.out.println ("Resource created.");
    System.out.println ("ID: " + result.id());
    System.out.println ("Provisioning state: " + result.properties().provisioningState().toString());
    System.out.println ();

    return result.id();
}

選擇服務和定價層

當您建立新的資源時,您必須知道您想要使用的「種類」服務,以及您想要的 定價層 (或 SKU)。 建立資源時,您將使用此和其他資訊做為參數。 您可以呼叫下列方法,以尋找可用的 Azure AI 服務「種類」清單:

public static void list_available_kinds_skus_locations (CognitiveServicesManager client) {
    System.out.println ("Available SKUs:");
    System.out.println("Kind\tSKU Name\tSKU Tier\tLocations");
    ResourceSkus skus = client.resourceSkus();
    for (ResourceSku sku : skus.list()) {
        String locations = String.join (",", sku.locations());
        System.out.println (sku.kind() + "\t" + sku.name() + "\t" + sku.tier() + "\t" + locations);
    }
}

下表提供 Azure AI 服務產品和定價的相關信息。

多服務

服務 種類
多個服務。 如需詳細資訊,請參閱價格網頁 CognitiveServices

視覺

服務 種類
視覺 ComputerVision
自訂視覺 - 預測 CustomVision.Prediction
自訂視覺 - 訓練 CustomVision.Training
臉部 Face
文件智慧服務 FormRecognizer

Speech

服務 種類
Speech SpeechServices

語言

服務 種類
Language Understanding (LUIS) LUIS
QnA Maker QnAMaker
語言 TextAnalytics
文字翻譯 TextTranslation

Decision

服務 種類
異常偵測器 AnomalyDetector
內容仲裁 ContentModerator
個人化服務工具 Personalizer

Azure OpenAI

服務 種類
Azure OpenAI OpenAI

定價層和計費

定價層(以及您計費的金額)是以您使用驗證資訊所傳送的交易數目為基礎。 每個定價層都會指定:

  • 每秒允許的交易數目上限(TPS)。
  • 定價層內啟用的服務功能。
  • 預先定義交易數目的成本。 高於此號碼會造成額外費用,如服務定價詳細數據中所指定。

注意

許多 Azure AI 服務都有一個免費層,可供您用來試用服務。 若要使用免費層,請使用 F0 作為您資源的定價層。

檢視您的資源

若要檢視 Azure 帳戶下的所有資源(跨所有資源群組),請使用下列方法:

public static void list_resources (CognitiveServicesManager client, String resourceGroupName) {
    System.out.println ("Resources in resource group: " + resourceGroupName);
    // Note Azure resources are also sometimes referred to as accounts.
    Accounts accounts = client.accounts();
    for (Account account : accounts.listByResourceGroup(resourceGroupName)) {
        System.out.println ("ID: " + account.id());
        System.out.println ("Kind: " + account.kind ());
        System.out.println ("SKU Name: " + account.sku().name());
        System.out.println ("Custom subdomain name: " + account.properties().customSubDomainName());
        System.out.println ();
    }
}

刪除資源

下列方法會從指定的資源群組中刪除指定的資源。

public static void delete_resource (CognitiveServicesManager client, String resourceId) {
    System.out.println ("Deleting resource: " + resourceId + "...");
    client.accounts().deleteById (resourceId);
    System.out.println ("Resource deleted.");
    System.out.println ();
}

參考文檔 | 庫原始程式碼 | 套件 (npm)範例 |

JavaScript 必要條件

  • 有效的 Azure 訂用帳戶 - 免費建立一個訂用帳戶。
  • 目前版本的 Node.js
  • Azure 帳戶必須已獲指派 Cognitive Services Contributor 角色,您才能同意負責任的 AI 條款並建立資源。 若要將此角色指派給您的帳戶,請遵循指派角色檔中的步驟,或連絡您的系統管理員。
  • 如果您打算在 Azure AI 視覺中使用空間分析,或在 Azure AI 語言中使用 文字分析 for Health,則必須從 Azure 入口網站 建立您的第一個視覺或語言資源,以便檢閱及確認條款和條件。 您可以在這裏執行此動作: Azure AI 語言Azure AI 視覺。 之後,您可以使用相同 Azure 訂用帳戶下的任何部署工具(例如 SDK、CLI 或 ARM 範本)來建立後續資源。

建立 Azure 服務主體

若要讓應用程式與 Azure 帳戶互動,您需要 Azure 服務主體來管理許可權。 遵循建立 Azure 服務主體中的指示。

當您建立服務主體時,您會看到它具有秘密值、標識碼和應用程式識別碼。 將應用程式識別碼和秘密儲存到暫存位置,以供後續步驟使用。

建立資源群組

建立 Azure AI 服務資源之前,您的帳戶必須有包含資源的 Azure 資源群組。 如果您還沒有資源群組,請在 Azure 入口網站 中建立一個資源群組,再繼續。

建立新的 Node.js 應用程式

在主控台視窗中(例如 cmd、PowerShell 或 Bash),為您的應用程式建立新的目錄,然後流覽至它。

mkdir myapp && cd myapp

init執行 命令以使用 package.json 檔案建立節點應用程式。

 init

在繼續之前,請先建立名為 index.js 的 檔案。

安裝用戶端程式庫

安裝下列 npm 套件:

npm install @azure/arm-cognitiveservices
npm install @azure/identity

您的應用程式檔案 package.json 將會隨著相依性更新。

匯入程式庫

開啟您的 index.js 腳本並匯入下列連結庫。

"use strict";

/* To run this sample, install the following modules.
 * npm install @azure/arm-cognitiveservices @azure/identity
 */
var Arm = require("@azure/arm-cognitiveservices");
var Identity = require("@azure/identity");

驗證用戶端

使用您建立的服務主體和您的 Azure 帳戶資訊,將下列欄位新增至腳本的根目錄,並填入其值。

const service_principal_application_id =
  "PASTE_YOUR_SERVICE_PRINCIPAL_APPLICATION_ID_HERE";
const service_principal_secret = "PASTE_YOUR_SERVICE_PRINCIPAL_SECRET_HERE";

/* The ID of your Azure subscription. You can find this in the Azure Dashboard under Home > Subscriptions. */
const subscription_id = "PASTE_YOUR_SUBSCRIPTION_ID_HERE";

/* The Active Directory tenant ID. You can find this in the Azure Dashboard under Home > Azure Active Directory. */
const tenant_id = "PASTE_YOUR_TENANT_ID_HERE";

/* The name of the Azure resource group in which you want to create the resource.
You can find resource groups in the Azure Dashboard under Home > Resource groups. */
const resource_group_name = "PASTE_YOUR_RESOURCE_GROUP_NAME_HERE";

/* The name of the custom subdomain to use when you create the resource. This is optional.
For example, if you create a Bing Search v7 resource with the custom subdomain name 'my-search-resource',
your resource would have the endpoint https://my-search-resource.cognitiveservices.azure.com/.
Note not all Cognitive Services allow custom subdomain names.
*/
const subdomain_name = "PASTE_YOUR_SUBDOMAIN_NAME_HERE";

接下來,新增下列 quickstart 函式來處理程式的主要工作。 程序代碼的第一個 區塊會使用您在上面輸入的認證變數來建構 CognitiveServicesManagementClient 物件。 所有 Azure 管理作業都需要此物件。

async function quickstart() {
  /* For more information see:
https://www.npmjs.com/package/@azure/arm-cognitiveservices/v/6.0.0
https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/samples/AzureIdentityExamples.md#authenticating-a-service-principal-with-a-client-secret
*/
  const credentials = new Identity.ClientSecretCredential(
    tenant_id,
    service_principal_application_id,
    service_principal_secret
  );
  const client = new Arm.CognitiveServicesManagementClient(
    credentials,
    subscription_id
  );
  // Note Azure resources are also sometimes referred to as accounts.
  const accounts_client = client.accounts;
  const resource_skus_client = client.resourceSkus;
  const deleted_accounts_client = client.deletedAccounts;

呼叫管理功能

將下列程式代碼新增至函 quickstart 式結尾以列出可用的資源、建立範例資源、列出您自己的資源,然後刪除範例資源。 您將在後續步驟中定義這些函式。

建立 Azure AI 服務資源 (Node.js)

若要建立並訂閱新的 Azure AI 服務資源,請使用 Create 函式。 此函式會將新的可計費資源新增至您傳入的資源群組。 當您建立新的資源時,您必須知道您想要使用的「種類」服務,以及其定價層(或 SKU)和 Azure 位置。 下列函式會採用所有這些自變數,並建立資源。

async function create_resource(
  client,
  resource_name,
  resource_kind,
  resource_sku,
  resource_region
) {
  console.log("Creating resource: " + resource_name + "...");
  /* NOTE If you do not want to use a custom subdomain name, remove the customSubDomainName
property from the properties object. */
  var parameters = {
    sku: { name: resource_sku },
    kind: resource_kind,
    location: resource_region,
    properties: { customSubDomainName: subdomain_name },
  };
  return client
    .beginCreateAndWait(resource_group_name, resource_name, parameters)
    .then((result) => {
      console.log("Resource created.");
      console.log();
      console.log("ID: " + result.id);
      console.log("Kind: " + result.kind);
      console.log();
    })
    .catch((err) => {
      console.log(err);
    });
}

選擇服務和定價層

當您建立新的資源時,您必須知道您想要使用的「種類」服務,以及您想要的 定價層 (或 SKU)。 建立資源時,您將使用此和其他資訊做為參數。 下列函式會列出可用的 Azure AI 服務「種類」。

async function list_available_kinds_skus_locations(client) {
  console.log("Available SKUs:");
  var result = client.list();
  console.log("Kind\tSKU Name\tSKU Tier\tLocations");
  for await (let item of result) {
    var locations = item.locations.join(",");
    console.log(item.kind + "\t" + item.name + "\t" + item.tier + "\t" + locations);
  }
}

下表提供 Azure AI 服務產品和定價的相關信息。

多服務

服務 種類
多個服務。 如需詳細資訊,請參閱價格網頁 CognitiveServices

視覺

服務 種類
視覺 ComputerVision
自訂視覺 - 預測 CustomVision.Prediction
自訂視覺 - 訓練 CustomVision.Training
臉部 Face
文件智慧服務 FormRecognizer

Speech

服務 種類
Speech SpeechServices

語言

服務 種類
Language Understanding (LUIS) LUIS
QnA Maker QnAMaker
語言 TextAnalytics
文字翻譯 TextTranslation

Decision

服務 種類
異常偵測器 AnomalyDetector
內容仲裁 ContentModerator
個人化服務工具 Personalizer

Azure OpenAI

服務 種類
Azure OpenAI OpenAI

定價層和計費

定價層(以及您計費的金額)是以您使用驗證資訊所傳送的交易數目為基礎。 每個定價層都會指定:

  • 每秒允許的交易數目上限(TPS)。
  • 定價層內啟用的服務功能。
  • 預先定義交易數目的成本。 高於此號碼會造成額外費用,如服務定價詳細數據中所指定。

注意

許多 Azure AI 服務都有一個免費層,可供您用來試用服務。 若要使用免費層,請使用 F0 作為您資源的定價層。

檢視您的資源

若要檢視 Azure 帳戶下的所有資源(跨所有資源群組),請使用下列函式:

async function list_resources(client) {
  console.log("Resources in resource group: " + resource_group_name);
  var result = client.listByResourceGroup(resource_group_name);
  for await (let item of result) {
    console.log(item);
    console.log();
  }
}

刪除資源

下列函式會從指定的資源群組中刪除指定的資源。

async function delete_resource(client, resource_name) {
  console.log("Deleting resource: " + resource_name + "...");
  await client.beginDeleteAndWait(resource_group_name, resource_name);
  console.log("Resource deleted.");
  console.log();
}

執行應用程式

將下列程式代碼新增至腳本底部,以呼叫具有錯誤處理的main quickstart 函式。

try {
  quickstart();
} catch (error) {
  console.log(error);
}

然後,在您的主控台視窗中,使用 node 命令執行應用程式。

node index.js

參考文件 | 連結庫原始程式碼 | 套件 (PyPi)範例 |

Python 必要條件

  • 有效的 Azure 訂用帳戶 - 免費建立一個訂用帳戶。
  • Python 3.x
  • Azure 帳戶必須已獲指派 Cognitive Services Contributor 角色,您才能同意負責任的 AI 條款並建立資源。 若要將此角色指派給您的帳戶,請遵循指派角色檔中的步驟,或連絡您的系統管理員。
  • 如果您打算在 Azure AI 視覺中使用空間分析,或在 Azure AI 語言中使用 文字分析 健康情況,則必須從 Azure 入口網站 建立您的第一個視覺或語言資源,以便檢閱並確認條款和條件。 您可以在這裏執行此動作: Azure AI 語言Azure AI 視覺。 之後,您可以使用相同 Azure 訂用帳戶下的任何部署工具(例如 SDK、CLI 或 ARM 範本)來建立後續資源。

建立 Azure 服務主體

若要讓應用程式與 Azure 帳戶互動,您需要 Azure 服務主體來管理許可權。 遵循建立 Azure 服務主體中的指示。

當您建立服務主體時,您會看到它具有秘密值、標識碼和應用程式識別碼。 將應用程式識別碼和秘密儲存到暫存位置,以供後續步驟使用。

建立資源群組

建立 Azure AI 服務資源之前,您的帳戶必須有包含資源的 Azure 資源群組。 如果您還沒有資源群組,請在 Azure 入口網站 中建立一個資源群組,然後再繼續。

建立新的 Python 應用程式

在慣用的編輯器或 IDE 中建立新的 Python 應用程式,並在控制台視窗中瀏覽至您的專案。

安裝用戶端程式庫

您可以使用下列項目來安裝客戶端連結庫:

pip install azure-mgmt-cognitiveservices

同時安裝 適用於 Microsoft Entra 令牌驗證支援的 Azure 身分識別連結庫

pip install azure-identity

匯入程式庫

開啟 Python 腳本並匯入下列連結庫。

import time
from azure.identity import ClientSecretCredential
from azure.mgmt.cognitiveservices import CognitiveServicesManagementClient
from azure.mgmt.cognitiveservices.models import Account, Sku

驗證用戶端

使用您建立的服務主體和您的 Azure 帳戶資訊,將下列欄位新增至腳本的根目錄,並填入其值。

# Be sure to use the service pricipal application ID, not simply the ID. 
service_principal_application_id = "PASTE_YOUR_SERVICE_PRINCIPAL_APPLICATION_ID_HERE"
service_principal_secret = "PASTE_YOUR_SERVICE_PRINCIPAL_SECRET_HERE"

# The ID of your Azure subscription. You can find this in the Azure Dashboard under Home > Subscriptions.
subscription_id = "PASTE_YOUR_SUBSCRIPTION_ID_HERE"

# The Active Directory tenant ID. You can find this in the Azure Dashboard under Home > Azure Active Directory.
tenant_id = "PASTE_YOUR_TENANT_ID_HERE"

# The name of the Azure resource group in which you want to create the resource.
# You can find resource groups in the Azure Dashboard under Home > Resource groups.
resource_group_name = "PASTE_YOUR_RESOURCE_GROUP_NAME_HERE"

# The name of the custom subdomain to use when you create the resource. This is optional.
# For example, if you create a Bing Search v7 resource with the custom subdomain name 'my-search-resource',
# your resource would have the endpoint https://my-search-resource.cognitiveservices.azure.com/.
# Note not all Cognitive Services allow custom subdomain names.
subdomain_name = "PASTE_YOUR_SUBDOMAIN_NAME_HERE"

# How many seconds to wait between checking the status of an async operation.
wait_time = 10

然後新增下列程式代碼來建構 CognitiveServicesManagementClient 物件。 所有 Azure 管理作業都需要此物件。

credential = ClientSecretCredential(tenant_id, service_principal_application_id, service_principal_secret)
client = CognitiveServicesManagementClient(credential, subscription_id)

建立 Azure AI 服務資源 (Python)

若要建立並訂閱新的 Azure AI 服務資源,請使用 Create 函式。 此函式會將新的可計費資源新增至您傳入的資源群組。 當您建立新的資源時,您必須知道您想要使用的「種類」服務,以及其定價層(或 SKU)和 Azure 位置。 下列函式會採用所有這些自變數,並建立資源。

def create_resource (resource_name, kind, sku_name, location) :
    print("Creating resource: " + resource_name + "...")

# NOTE If you do not want to use a custom subdomain name, remove the customSubDomainName
# property from the properties object.
    parameters = Account(sku=Sku(name=sku_name), kind=kind, location=location, properties={ 'custom_sub_domain_name' : subdomain_name })

    poller = client.accounts.begin_create(resource_group_name, resource_name, parameters)
    while (False == poller.done ()) :
        print ("Waiting {wait_time} seconds for operation to finish.".format (wait_time = wait_time))
        time.sleep (wait_time)
# This will raise an exception if the server responded with an error.
    result = poller.result ()

    print("Resource created.")
    print()
    print("ID: " + result.id)
    print("Name: " + result.name)
    print("Type: " + result.type)
    print()

選擇服務和定價層

當您建立新的資源時,您必須知道您想要使用的「種類」服務,以及您想要的 定價層 (或 SKU)。 建立資源時,您將使用此和其他資訊做為參數。 下列函式會列出可用的 Azure AI 服務「種類」。

def list_available_kinds_skus_locations():
    print("Available SKUs:")
    result = client.resource_skus.list()
    print("Kind\tSKU Name\tSKU Tier\tLocations")
    for x in result:
        locations = ",".join(x.locations)
        print(x.kind + "\t" + x.name + "\t" + x.tier + "\t" + locations)

下表提供 Azure AI 服務產品和定價的相關信息。

多服務

服務 種類
多個服務。 如需詳細資訊,請參閱價格網頁 CognitiveServices

視覺

服務 種類
視覺 ComputerVision
自訂視覺 - 預測 CustomVision.Prediction
自訂視覺 - 訓練 CustomVision.Training
臉部 Face
文件智慧服務 FormRecognizer

Speech

服務 種類
Speech SpeechServices

語言

服務 種類
Language Understanding (LUIS) LUIS
QnA Maker QnAMaker
語言 TextAnalytics
文字翻譯 TextTranslation

Decision

服務 種類
異常偵測器 AnomalyDetector
內容仲裁 ContentModerator
個人化服務工具 Personalizer

Azure OpenAI

服務 種類
Azure OpenAI OpenAI

定價層和計費

定價層(以及您計費的金額)是以您使用驗證資訊所傳送的交易數目為基礎。 每個定價層都會指定:

  • 每秒允許的交易數目上限(TPS)。
  • 定價層內啟用的服務功能。
  • 預先定義交易數目的成本。 高於此號碼會造成額外費用,如服務定價詳細數據中所指定。

注意

許多 Azure AI 服務都有一個免費層,可供您用來試用服務。 若要使用免費層,請使用 F0 作為您資源的定價層。

檢視您的資源

若要檢視 Azure 帳戶下的所有資源(跨所有資源群組),請使用下列函式:

def list_resources():
    print("Resources in resource group: " + resource_group_name)
    result = client.accounts.list_by_resource_group(resource_group_name)
    for x in result:
        print(x.name)
        print(x)
        print()

刪除資源

下列函式會從指定的資源群組中刪除指定的資源。

def delete_resource(resource_name) :
    print("Deleting resource: " + resource_name + "...")

    poller = client.accounts.begin_delete(resource_group_name, resource_name)
    while (False == poller.done ()) :
        print ("Waiting {wait_time} seconds for operation to finish.".format (wait_time = wait_time))
        time.sleep (wait_time)
# This will raise an exception if the server responded with an error.
    result = poller.result ()

    print("Resource deleted.")

呼叫管理功能

將下列程式代碼新增至腳本底部,以呼叫上述函式。 此程式代碼會列出可用的資源、建立範例資源、列出您自己的資源,然後刪除範例資源。

resource_name = "test_resource"
resource_kind = "TextTranslation"
resource_sku = "F0"
resource_location = "Global"

# Uncomment this to list all available resource kinds, SKUs, and locations for your Azure account.
#list_available_kinds_skus_locations ()

# Create a resource with kind Text Translation, SKU F0 (free tier), location global.
create_resource(resource_name, resource_kind, resource_sku, resource_location)

# Uncomment this to list all resources for your Azure account.
#list_resources()

# Delete the resource.
delete_resource(resource_name)

# NOTE: Deleting a resource only soft-deletes it. To delete it permanently, you must purge it.
# Otherwise, if you later try to create a resource with the same name, you will receive the following error:
# azure.core.exceptions.ResourceExistsError: (FlagMustBeSetForRestore) An existing resource with ID '<your resource ID>' has been soft-deleted. To restore the resource, you must specify 'restore' to be 'true' in the property. If you don't want to restore existing resource, please purge it first.
# Code: FlagMustBeSetForRestore

# Purge the resource.
purge_resource(resource_name, resource_location)

執行應用程式

使用 python 命令從命令行執行您的應用程式。

python <your-script-name>.py

下一步