建立及設定 Azure AI 模型推斷的所有資源
重要
本文中標示為 (預覽) 的項目目前處於公開預覽狀態。 此預覽版本沒有服務等級協定,不建議將其用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款。
在本文中,您將瞭解如何建立使用 Azure AI 模型推斷及取用 Azure AI 模型類別目錄中旗艦模型所需的資源。
必要條件
若要完成本文,您需要:
- Azure 訂用帳戶。 如果您使用 GitHub Models,您可以升級您的體驗,並在程式中建立 Azure 訂用帳戶。 如果您的情況,請閱讀 從 GitHub 模型升級至 Azure AI 模型推斷 。
重要
Azure AI Foundry 入口網站會使用專案和中樞來建立 Azure AI 服務帳戶,並設定 Azure AI 模型推斷。 如果您不想使用中樞和專案,您可以使用 Azure CLI、Bicep 或使用 Azure 入口網站 建立 Azure AI 服務資源來建立資源。
建立資源
若要使用 Azure AI Services 帳戶建立專案,請遵循下列步驟:
在登陸頁面上,選取 [ 建立專案]。
為專案指定名稱,例如 「my-project」。
在本教學課程中,我們會在新的 AI 中樞下建立全新的專案,因此,選取 [ 建立新中樞]。
為中樞命名,例如 「my-hub」,然後選取 [ 下一步]。
精靈會更新即將建立之資源的詳細數據。 選取 要建立 的 Azure 資源以查看詳細數據。
您可以看到已建立下列資源:
屬性 說明 資源群組 Azure 中所有資源的主要容器。 這有助於取得共同組織的資源。 它也有助於設定與整個專案相關聯的成本範圍。 Location 您要建立的資源區域。 中樞 Azure AI Foundry 中 AI 專案的主要容器。 中樞會提升共同作業,並可讓您儲存項目的資訊。 AI 服務 資源可讓您存取 Azure AI 模型目錄中的旗艦模型。 在本教學課程中,會建立新的帳戶,但 Azure AI 服務資源可以跨多個中樞和項目共用。 中樞會使用資源的連線來存取該處可用的模型部署。 若要瞭解如何建立專案與 Azure AI 服務之間的連線,以取用 Azure AI 模型推斷,您可以閱讀 連線 AI 專案。 選取 建立。 資源建立程式隨即啟動。
完成後,您的專案就準備好進行設定。
Azure AI 模型推斷是必須在 Azure AI Foundry 中開啟的預覽功能。 在頂端導覽列中,選取右上角的 [預覽功能 ] 圖示。 內容相關刀鋒視窗會顯示在畫面右側。
開啟將模型部署至 Azure AI 模型推斷服務的功能。
關閉面板。
若要使用 Azure AI 模型推斷,您必須將模型部署新增至 Azure AI 服務帳戶。
重要
本文中標示為 (預覽) 的項目目前處於公開預覽狀態。 此預覽版本沒有服務等級協定,不建議將其用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款。
您可以決定並設定哪些模型可用於推斷端點中的推斷。 設定指定的模型時,您就可以在要求上指出其模型名稱或部署名稱,從中產生預測。 您的程式代碼不需要進一步變更即可使用它。
在本文中,您將瞭解如何在 Azure AI Foundry 中將新的模型新增至 Azure AI 模型推斷。
必要條件
若要完成本文,您需要:
Azure 訂用帳戶。 如果您使用 GitHub Models,您可以升級您的體驗,並在程式中建立 Azure 訂用帳戶。 如果您的情況,請閱讀 從 GitHub 模型升級至 Azure AI 模型推斷 。
Azure AI 服務資源。
安裝 Azure CLI 和
cognitiveservices
適用於 Azure AI 服務的擴充功能:az extension add -n cognitiveservices
本教學課程中的某些命令會使用
jq
工具,而此工具可能未安裝在您的系統中。 如需安裝指示,請參閱 下載jq
。識別下列資訊:
您的 Azure 訂用帳戶識別碼。
您的 Azure AI 服務資源名稱。
部署 Azure AI 服務資源的資源群組。
加入模型
若要新增模型,您必須先識別要部署的模型。 您可以查詢可用的模型,如下所示:
登入您的 Azure 訂用帳戶:
az login
如果您有 1 個以上的訂用帳戶,請選取資源所在的訂用帳戶:
az account set --subscription $subscriptionId>
使用您計劃使用的 Azure AI 服務資源名稱和資源群組,設定下列環境變數。
accountName="<ai-services-resource-name>" resourceGroupName="<resource-group>"
如果您尚未建立 Azure AI 服務帳戶,您可以建立一個帳戶,如下所示:
az cognitiveservices account create -n $accountName -g $resourceGroupName
讓我們先看看哪些模型可供您使用,以及哪些 SKU。 下列命令會列出所有可用的模型定義:
az cognitiveservices account list-models \ -n $accountName \ -g $resourceGroupName \ | jq '.[] | { name: .name, format: .format, version: .version, sku: .skus[0].name, capacity: .skus[0].capacity.default }'
輸出如下所示:
{ "name": "Phi-3.5-vision-instruct", "format": "Microsoft", "version": "2", "sku": "GlobalStandard", "capacity": 1 }
識別您想要部署的模型。 您需要屬性
name
、format
、version
與sku
。 視部署類型而定,可能需要容量。提示
請注意,並非所有模型都可在所有 SKU 中使用。
將模型部署新增至資源。 下列範例會新增
Phi-3.5-vision-instruct
:az cognitiveservices account deployment create \ -n $accountName \ -g $resourceGroupName \ --deployment-name Phi-3.5-vision-instruct \ --model-name Phi-3.5-vision-instruct \ --model-version 2 \ --model-format Microsoft \ --sku-capacity 1 \ --sku-name GlobalStandard
模型已準備好取用。
您可以視需要多次部署相同的模型,只要它是在不同的部署名稱下即可。 如果您想要測試指定模型的不同組態,包括內容安全性,這項功能可能會很有用。
管理部署
您可以使用 CLI 檢視所有可用的部署:
執行下列命令以檢視所有作用中的部署:
az cognitiveservices account deployment list -n $accountName -g $resourceGroupName
您可以看到指定部署的詳細資料:
az cognitiveservices account deployment show \ --deployment-name "Phi-3.5-vision-instruct" \ -n $accountName \ -g $resourceGroupName
您可以刪除指定的部署,如下所示:
az cognitiveservices account deployment delete \ --deployment-name "Phi-3.5-vision-instruct" \ -n $accountName \ -g $resourceGroupName
使用模型
您可以使用 Azure AI 模型的推斷端點來取用 Azure AI 模型推斷中的模型。 建構要求時,請指出 參數 model
,並插入您已建立的模型部署名稱。 您可以使用下列程式代碼,以程式設計方式取得推斷端點的 URI:
推斷端點
az cognitiveservices account show -n $accountName -g $resourceGroupName | jq '.properties.endpoints["Azure AI Model Inference API"]'
若要對 Azure AI 模型推斷端點提出要求,請附加 路由 models
,例如 https://<resource>.services.ai.azure.com/models
。 您可以在 Azure AI 模型推斷 API 參考頁面看到端點的 API 參考。
推斷索引鍵
az cognitiveservices account keys list -n $accountName -g $resourceGroupName
重要
本文中標示為 (預覽) 的項目目前處於公開預覽狀態。 此預覽版本沒有服務等級協定,不建議將其用於生產工作負載。 可能不支援特定功能,或可能已經限制功能。 如需詳細資訊,請參閱 Microsoft Azure 預覽版增補使用條款。
在本文中,您將瞭解如何建立使用 Azure AI 模型推斷及取用 Azure AI 模型類別目錄中旗艦模型所需的資源。
必要條件
若要完成本文,您需要:
- Azure 訂用帳戶。 如果您使用 GitHub Models,您可以升級您的體驗,並在程式中建立 Azure 訂用帳戶。 如果您的情況,請閱讀 從 GitHub 模型升級至 Azure AI 模型推斷 。
安裝 Azure CLI。
識別下列資訊:
- 您的 Azure 訂用帳戶識別碼。
關於本教學課程
本文中的範例是以 Azure-Samples/azureai-model-inference-bicep 存放庫中所包含的程式代碼範例為基礎。 若要在本機執行命令,而不需要複製或貼上檔案內容,請使用下列命令來複製存放庫,並移至您程式代碼撰寫語言的資料夾:
git clone https://github.com/Azure-Samples/azureai-model-inference-bicep
此範例的檔案位於:
cd azureai-model-inference-bicep/infra
了解資源
本教學課程可協助您建立:
- Azure AI 服務 資源。
- 支援隨用隨付之每個模型的全域標準 SKU 中的模型部署。
- (選擇性)Azure AI 專案和中樞。
- (選擇性)中樞與 Azure AI 服務中的模型之間的連線。
您正使用下列資產來建立這些資源:
使用範本
modules/ai-services-template.bicep
來描述您的 Azure AI Services 資源:modules/ai-services-template.bicep
@description('Location of the resource.') param location string = resourceGroup().location @description('Name of the Azure AI Services account.') param accountName string @description('The resource model definition representing SKU') param sku string = 'S0' @description('Whether or not to allow keys for this account.') param allowKeys bool = true @allowed([ 'Enabled' 'Disabled' ]) @description('Whether or not public endpoint access is allowed for this account.') param publicNetworkAccess string = 'Enabled' @allowed([ 'Allow' 'Deny' ]) @description('The default action for network ACLs.') param networkAclsDefaultAction string = 'Allow' resource account 'Microsoft.CognitiveServices/accounts@2023-05-01' = { name: accountName location: location identity: { type: 'SystemAssigned' } sku: { name: sku } kind: 'AIServices' properties: { publicNetworkAccess: publicNetworkAccess networkAcls: { defaultAction: networkAclsDefaultAction } disableLocalAuth: allowKeys } } output endpointUri string = account.properties.endpoints['Azure AI Model Inference API'] output id string = account.id
使用樣本
modules/ai-services-deployment-template.bicep
來描述模型部署:modules/ai-services-deployment-template.bicep
@description('Name of the Azure AI services account') param accountName string @description('Name of the model to deploy') param modelName string @description('Version of the model to deploy') param modelVersion string @allowed([ 'AI21 Labs' 'Cohere' 'Core42' 'DeepSeek' 'Meta' 'Microsoft' 'Mistral AI' 'OpenAI' ]) @description('Model provider') param modelPublisherFormat string @allowed([ 'GlobalStandard' 'Standard' 'GlobalProvisioned' 'Provisioned' ]) @description('Model deployment SKU name') param skuName string = 'GlobalStandard' @description('Content filter policy name') param contentFilterPolicyName string = 'Microsoft.DefaultV2' @description('Model deployment capacity') param capacity int = 1 resource modelDeployment 'Microsoft.CognitiveServices/accounts/deployments@2024-04-01-preview' = { name: '${accountName}/${modelName}' sku: { name: skuName capacity: capacity } properties: { model: { format: modelPublisherFormat name: modelName version: modelVersion } raiPolicyName: contentFilterPolicyName == null ? 'Microsoft.Nill' : contentFilterPolicyName } }
為了方便起見,我們會定義我們想要在服務中使用 JSON 檔案取得的模型。 檔案 infra/models.json包含索引鍵
name
、version
、provider
和sku
的 JSON 物件清單,其會定義部署將布建的模型。 由於模型支援隨用隨付,因此新增模型部署不會產生額外的成本。 修改檔案,以移除/新增您想要取得的模型專案。 下列範例 只會顯示 JSON 檔案的前 7 行 :models.json
[ { "name": "AI21-Jamba-1.5-Large", "version": "1", "provider": "AI21 Labs", "sku": "GlobalStandard" },
如果您打算使用專案(建議),您需要範本來建立專案、中樞和 Azure AI Services 資源的連線:
modules/project-hub-template.bicep
param location string = resourceGroup().location @description('Name of the Azure AI hub') param hubName string = 'hub-dev' @description('Name of the Azure AI project') param projectName string = 'intelligent-apps' @description('Name of the storage account used for the workspace.') param storageAccountName string = replace(hubName, '-', '') param keyVaultName string = replace(hubName, 'hub', 'kv') param applicationInsightsName string = replace(hubName, 'hub', 'log') @description('The container registry resource id if you want to create a link to the workspace.') param containerRegistryName string = replace(hubName, '-', '') @description('The tags for the resources') param tagValues object = { owner: 'santiagxf' project: 'intelligent-apps' environment: 'dev' } var tenantId = subscription().tenantId var resourceGroupName = resourceGroup().name var storageAccountId = resourceId(resourceGroupName, 'Microsoft.Storage/storageAccounts', storageAccountName) var keyVaultId = resourceId(resourceGroupName, 'Microsoft.KeyVault/vaults', keyVaultName) var applicationInsightsId = resourceId(resourceGroupName, 'Microsoft.Insights/components', applicationInsightsName) var containerRegistryId = resourceId( resourceGroupName, 'Microsoft.ContainerRegistry/registries', containerRegistryName ) resource storageAccount 'Microsoft.Storage/storageAccounts@2019-04-01' = { name: storageAccountName location: location sku: { name: 'Standard_LRS' } kind: 'StorageV2' properties: { encryption: { services: { blob: { enabled: true } file: { enabled: true } } keySource: 'Microsoft.Storage' } supportsHttpsTrafficOnly: true } tags: tagValues } resource keyVault 'Microsoft.KeyVault/vaults@2019-09-01' = { name: keyVaultName location: location properties: { tenantId: tenantId sku: { name: 'standard' family: 'A' } enableRbacAuthorization: true accessPolicies: [] } tags: tagValues } resource applicationInsights 'Microsoft.Insights/components@2018-05-01-preview' = { name: applicationInsightsName location: location kind: 'web' properties: { Application_Type: 'web' } tags: tagValues } resource containerRegistry 'Microsoft.ContainerRegistry/registries@2019-05-01' = { name: containerRegistryName location: location sku: { name: 'Standard' } properties: { adminUserEnabled: true } tags: tagValues } resource hub 'Microsoft.MachineLearningServices/workspaces@2024-07-01-preview' = { name: hubName kind: 'Hub' location: location identity: { type: 'systemAssigned' } sku: { tier: 'Standard' name: 'standard' } properties: { description: 'Azure AI hub' friendlyName: hubName storageAccount: storageAccountId keyVault: keyVaultId applicationInsights: applicationInsightsId containerRegistry: (empty(containerRegistryName) ? null : containerRegistryId) encryption: { status: 'Disabled' keyVaultProperties: { keyVaultArmId: keyVaultId keyIdentifier: '' } } hbiWorkspace: false } tags: tagValues } resource project 'Microsoft.MachineLearningServices/workspaces@2024-07-01-preview' = { name: projectName kind: 'Project' location: location identity: { type: 'systemAssigned' } sku: { tier: 'Standard' name: 'standard' } properties: { description: 'Azure AI project' friendlyName: projectName hbiWorkspace: false hubResourceId: hub.id } tags: tagValues }
modules/ai-services-connection-template.bicep
@description('Name of the hub where the connection will be created') param hubName string @description('Name of the connection') param name string @description('Category of the connection') param category string = 'AIServices' @allowed(['AAD', 'ApiKey', 'ManagedIdentity', 'None']) param authType string = 'AAD' @description('The endpoint URI of the connected service') param endpointUri string @description('The resource ID of the connected service') param resourceId string = '' @secure() param key string = '' resource connection 'Microsoft.MachineLearningServices/workspaces/connections@2024-04-01-preview' = { name: '${hubName}/${name}' properties: { category: category target: endpointUri authType: authType isSharedToAll: true credentials: authType == 'ApiKey' ? { key: key } : null metadata: { ApiType: 'Azure' ResourceId: resourceId } } }
建立資源
在您的控制台中,遵循下列步驟:
定義主要部署:
deploy-with-project.bicep
@description('Location to create the resources in') param location string = resourceGroup().location @description('Name of the resource group to create the resources in') param resourceGroupName string = resourceGroup().name @description('Name of the AI Services account to create') param accountName string = 'azurei-models-dev' @description('Name of the project hub to create') param hubName string = 'hub-azurei-dev' @description('Name of the project to create in the project hub') param projectName string = 'intelligent-apps' @description('Path to a JSON file with the list of models to deploy. Each model is a JSON object with the following properties: name, version, provider') var models = json(loadTextContent('models.json')) module aiServicesAccount 'modules/ai-services-template.bicep' = { name: 'aiServicesAccount' scope: resourceGroup(resourceGroupName) params: { accountName: accountName location: location } } module projectHub 'modules/project-hub-template.bicep' = { name: 'projectHub' scope: resourceGroup(resourceGroupName) params: { hubName: hubName projectName: projectName } } module aiServicesConnection 'modules/ai-services-connection-template.bicep' = { name: 'aiServicesConnection' scope: resourceGroup(resourceGroupName) params: { name: accountName authType: 'AAD' endpointUri: aiServicesAccount.outputs.endpointUri resourceId: aiServicesAccount.outputs.id hubName: hubName } dependsOn: [ projectHub ] } @batchSize(1) module modelDeployments 'modules/ai-services-deployment-template.bicep' = [ for (item, i) in models: { name: 'deployment-${item.name}' scope: resourceGroup(resourceGroupName) params: { accountName: accountName modelName: item.name modelVersion: item.version modelPublisherFormat: item.provider skuName: item.sku } dependsOn: [ aiServicesAccount ] } ] output endpoint string = aiServicesAccount.outputs.endpointUri
登入 Azure:
az login
請確定您位於正確的訂用帳戶中:
az account set --subscription "<subscription-id>"
執行部署:
RESOURCE_GROUP="<resource-group-name>" az deployment group create \ --resource-group $RESOURCE_GROUP \ --template-file deploy-with-project.bicep
如果您想要只部署 Azure AI Services 資源和模型部署,請使用下列部署檔案:
deploy.bicep
@description('Location to create the resources in') param location string = resourceGroup().location @description('Name of the resource group to create the resources in') param resourceGroupName string = resourceGroup().name @description('Name of the AI Services account to create') param accountName string = 'azurei-models-dev' @description('Path to a JSON file with the list of models to deploy. Each model is a JSON object with the following properties: name, version, provider') var models = json(loadTextContent('models.json')) module aiServicesAccount 'modules/ai-services-template.bicep' = { name: 'aiServicesAccount' scope: resourceGroup(resourceGroupName) params: { accountName: accountName location: location } } @batchSize(1) module modelDeployments 'modules/ai-services-deployment-template.bicep' = [ for (item, i) in models: { name: 'deployment-${item.name}' scope: resourceGroup(resourceGroupName) params: { accountName: accountName modelName: item.name modelVersion: item.version modelPublisherFormat: item.provider skuName: item.sku } dependsOn: [ aiServicesAccount ] } ] output endpoint string = aiServicesAccount.outputs.endpointUri
執行部署:
RESOURCE_GROUP="<resource-group-name>" az deployment group create \ --resource-group $RESOURCE_GROUP \ --template-file deploy.bicep
此範本會輸出 Azure AI 模型推斷端點,讓您可用來取用您所建立的任何模型部署。