重要
本文中标记了“(预览版)”的项目目前为公共预览版。 此预览版未提供服务级别协议,不建议将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款。
本文介绍如何在项目中创建使用 Azure AI Foundry 模型所需的资源。
了解资源
Azure AI Foundry 模型是 Azure AI Foundry 服务(以前已知的 Azure AI 服务)中的一项功能。 可以在该资源下创建模型部署,以使用其预测。 还可以将资源连接到 Azure AI Foundry 中的 Azure AI 中心和项目,以根据需要创建智能应用程序。 下图显示了高级体系结构。
Azure AI Foundry 服务不需要 AI 项目或 AI 中心才能运行,并且可以创建它们来使用应用程序中的旗舰模型。 但是,如果你部署 Azure AI 项目和中心(包括操场或代理),则会有更多功能可用。
本教程可帮助你创建:
- Azure AI Foundry 资源。
- 每个支持标准部署的模型的模型部署。
- (可选)Azure AI 项目和中心。
- (可选)中心与 Azure AI Foundry 中的模型之间的连接。
先决条件
若要完成本文,需要做好以下准备:
- Azure 订阅。 如果你正在使用 GitHub 模型,则可以升级体验并在此过程中创建 Azure 订阅。 如果是这种情况,请阅读 从 GitHub 模型升级到 Azure AI Foundry。
创建资源
若要使用 Azure AI Foundry(以前已知的 Azure AI 服务)资源创建项目,请执行以下步骤:
在登陆页上,选择“创建项目”。
为项目命名,例如“my-project”。
在本教程中,我们将在新的 AI 中心下创建一个新的项目,因此选择“创建新中心”。
为中心命名,例如“my-hub”,然后选择“下一步”。
向导会更新有关要创建的资源的详细信息。 选择要创建的 Azure 资源以查看详细信息。
可以看到已创建以下资源:
属性 说明 资源组 Azure 中所有资源的主容器。 这有助于获取协同工作的资源。 它还有助于确定与整个项目相关的成本的范围。 位置 要创建的资源的区域。 Hub Azure AI Foundry 中 AI 项目的主要容器。 中心可促进协作,并让你可以存储项目的信息。 AI Foundry 在本教程中,将创建一个新帐户,但 Azure AI Foundry 服务可以在多个中心和项目中共享。 中心使用与资源的连接来访问该资源可用的模型部署。 若要了解如何在项目与 Azure AI Foundry 之间创建连接,以使用 Azure AI Foundry 模型,可以阅读 “连接 AI 项目”。 选择创建。 资源创建过程开始。
完成后,即可配置项目。
若要使用 Azure AI Foundry 模型,需要添加模型部署。
后续步骤
可以决定和配置哪些模型可用于 Azure AI Foundry 资源中的推理。 配置给定模型后,可以通过在请求中指明其模型名称或部署名称来从中生成预测。 无需在代码中进行进一步更改即可使用它。
本文介绍如何向 Azure AI Foundry 添加新模型。
先决条件
若要完成本文,需要做好以下准备:
Azure 订阅。 如果你正在使用 GitHub 模型,则可以升级体验并在此过程中创建 Azure 订阅。 如果是这种情况,请阅读 从 GitHub 模型升级到 Azure AI Foundry 模型 。
Azure AI Foundry 资源(以前称为 Azure AI 服务)。 有关详细信息,请参阅 创建和配置 Azure AI Foundry 模型的所有资源。
来自合作伙伴和社区的模型 需要访问 Azure 市场。 确保你拥有 订阅模型产品/服务所需的权限。 Azure 直接销售的模型 没有此要求。
安装适用于 Azure AI 服务的 Azure CLI 和
cognitiveservices
扩展:az extension add -n cognitiveservices
本教程中的某些命令使用
jq
工具,该工具可能未安装在系统中。 有关安装说明,请参阅下载jq
。标识以下信息:
Azure 订阅 ID。
你的 Azure AI 服务资源名称。
在其中部署 Azure AI 服务资源的资源组。
添加模型
若要添加模型,首先需要标识要部署的模型。 可以按如下所示的方法查询可用模型:
登录到 Azure 订阅:
az login
如果您有多个订阅,请选择您的资源所在的订阅。
az account set --subscription $subscriptionId
使用您计划使用的 Azure AI 服务资源的名称和资源组来设置以下环境变量。
accountName="<ai-services-resource-name>" resourceGroupName="<resource-group>" ___location="eastus2"
如果尚未创建 Azure AI 服务帐户,可以按如下所示创建一个帐户:
az cognitiveservices account create -n $accountName -g $resourceGroupName --custom-___domain $accountName --___location $___location --kind AIServices --sku S0
首先,让我们看看哪些模型可供你使用,以及有哪些 SKU 可用。 SKU(也称为 部署类型)定义如何使用 Azure 基础结构来处理请求。 模型可以提供不同的部署类型。 以下命令列出了所有可用的模型定义:
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
。 该属性format
指示提供模型的提供程序。 可能还需要容量,具体取决于部署类型。将模型部署添加到资源。 以下示例添加
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
模型已准备就绪,可供使用。
如果需要,只要它使用不同的部署名称,就可以多次部署同一模型。 如果想要测试给定模型的不同配置(包括内容筛选器),此功能可能很有用。
使用该模型
可以通过资源的 Azure AI 模型推理终结点使用部署的模型。 构造请求时,指示参数 model
并插入已创建的模型部署名称。 可以使用以下代码以编程方式获取推理终结点的 URI:
推理终结点
az cognitiveservices account show -n $accountName -g $resourceGroupName | jq '.properties.endpoints["Azure AI Model Inference API"]'
若要向 Azure AI Foundry 模型终结点发出请求,请追加路由 models
,例如 https://<resource>.services.ai.azure.com/models
。 可以在 Azure AI Foundry 模型 API 参考页查看终结点的 API 参考。
推理键
az cognitiveservices account keys list -n $accountName -g $resourceGroupName
管理部署
可以使用 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
重要
本文中标记了“(预览版)”的项目目前为公共预览版。 此预览版未提供服务级别协议,不建议将其用于生产工作负载。 某些功能可能不受支持或者受限。 有关详细信息,请参阅 Microsoft Azure 预览版补充使用条款。
本文介绍如何在项目中创建使用 Azure AI Foundry 模型所需的资源。
了解资源
Azure AI Foundry 模型是 Azure AI Foundry 服务(以前已知的 Azure AI 服务)中的一项功能。 可以在该资源下创建模型部署,以使用其预测。 还可以将资源连接到 Azure AI Foundry 中的 Azure AI 中心和项目,以根据需要创建智能应用程序。 下图显示了高级体系结构。
Azure AI Foundry 服务不需要 AI 项目或 AI 中心才能运行,并且可以创建它们来使用应用程序中的旗舰模型。 但是,如果你部署 Azure AI 项目和中心(包括操场或代理),则会有更多功能可用。
本教程可帮助你创建:
- Azure AI Foundry 资源。
- 每个支持标准部署的模型的模型部署。
- (可选)Azure AI 项目和中心。
- (可选)中心与 Azure AI Foundry 中的模型之间的连接。
先决条件
若要完成本文,需要做好以下准备:
- Azure 订阅。 如果你正在使用 GitHub 模型,则可以升级体验并在此过程中创建 Azure 订阅。 如果是这种情况,请阅读 从 GitHub 模型升级到 Azure AI Foundry。
安装 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
订阅“合作伙伴和社区模型”所需的权限
可用于部署的合作伙伴和社区提供的模型(例如 Cohere 模型)需要通过 Azure 市场。 模型提供程序定义许可条款,并设置其模型使用 Azure 市场的价格。
部署第三方模型时,请确保帐户中具有以下权限:
- 关于 Azure 订阅服务:
Microsoft.MarketplaceOrdering/agreements/offers/plans/read
Microsoft.MarketplaceOrdering/agreements/offers/plans/sign/action
Microsoft.MarketplaceOrdering/offerTypes/publishers/offers/plans/agreements/read
Microsoft.Marketplace/offerTypes/publishers/offers/plans/agreements/read
Microsoft.SaaS/register/action
- 在资源组上,创建和使用 SaaS 资源的权限:
Microsoft.SaaS/resources/read
Microsoft.SaaS/resources/write
创建资源
执行以下步骤:
使用模板
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 Foundry 模型终结点,可用于访问您已创建的任意模型部署。