你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
创建和配置 Azure AI 模型推理的所有资源
重要
本文中标记了“(预览版)”的项目目前为公共预览版。 此预览版未提供服务级别协议,不建议将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款。
本文介绍如何创建使用 Azure AI 模型推理和使用 Azure AI 模型目录中的旗舰模型所需的资源。
先决条件
若要完成本文,需要做好以下准备:
- Azure 订阅。 如果你正在使用 GitHub 模型,则可以升级体验并在此过程中创建 Azure 订阅。 如果是这种情况,请参阅从 GitHub 模型升级到 Azure AI 模型推理。
重要
Azure AI Foundry 门户使用项目和中心来创建 Azure AI 服务帐户并配置 Azure AI 模型推理。 如果不想使用中心和项目,可以使用 Azure CLI、Bicep 或使用 Azure 门户创建 Azure AI 服务资源。
创建资源
若要使用 Azure AI 服务帐户创建项目,请执行以下步骤:
在登陆页上,选择“创建项目”。
为项目命名,例如“my-project”。
在本教程中,我们将在新的 AI 中心下创建一个新的项目,因此选择“创建新中心”。
为中心命名,例如“my-hub”,然后选择“下一步”。
向导会更新有关要创建的资源的详细信息。 选择要创建的 Azure 资源以查看详细信息。
可以看到已创建以下资源:
properties 说明 资源组 Azure 中所有资源的主容器。 这有助于获取协同工作的资源。 它还有助于确定与整个项目相关的成本的范围。 位置 要创建的资源的区域。 中心 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 模型,则可以升级体验并在此过程中创建 Azure 订阅。 如果是这种情况,请阅读从 GitHub 模型升级到 Azure AI 模型推理。
Azure AI 服务资源。
安装适用于 Azure AI 服务的 Azure CLI 和
cognitiveservices
扩展:az extension add -n cognitiveservices
本教程中的某些命令使用
jq
工具,该工具可能未安装在系统中。 有关安装说明,请参阅下载jq
。标识以下信息:
Azure 订阅 ID。
你的 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 --custom-domain $accountName
首先,让我们看看哪些模型可供你使用,以及有哪些 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 模型,则可以升级体验并在此过程中创建 Azure 订阅。 如果是这种情况,请参阅从 GitHub 模型升级到 Azure AI 模型推理。
安装 Azure CLI。
标识以下信息:
- Azure 订阅 ID。
关于本教程
本文中的示例基于 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 服务中的模型之间的连接。
请注意,如果计划通过 Azure AI Foundry 门户来管理资源、使用操场或使用门户中的任何其他功能,则必须部署 Azure AI 项目和中心。
会使用以下资产创建这些资源:
使用模板
modules/ai-services-template.bicep
描述 Azure AI 服务资源: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: { customSubDomainName: accountName publicNetworkAccess: publicNetworkAccess networkAcls: { defaultAction: networkAclsDefaultAction } disableLocalAuth: allowKeys } } output endpointUri string = 'https://${account.outputs.name}.services.ai.azure.com/models' 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 文件提供的模型。 文件 基础结构/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 服务资源的模板:
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 服务资源和模型部署,请使用以下部署文件:
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 模型推理终结点,可用于使用你创建的任何模型部署。