如何通过Pipeline用YAML/BICEP构建Azure AI端点及多模型部署并解决报错
一、基于Bicep+YAML实现Azure AI资源自动化创建与销毁
1. 从现有门户资源生成匹配的Bicep模板
- 直接导出现有资源配置:在Azure Portal中进入已创建的工作区、端点或部署资源页面,点击「自动化」>「导出模板」,选择Bicep格式,生成的模板会完全匹配你在门户中配置的参数,无需手动填写基础配置。
- 调整模板支持多模型端点:确保端点的Bicep配置中
properties.endpointKind设为'Managed',然后在部署节点下添加多个模型的配置块,示例如下:
// 先定义在线端点 resource onlineEndpoint 'Microsoft.MachineLearningServices/workspaces/onlineEndpoints@2024-04-01' = { name: 'multi-model-endpoint' parent: workspace properties: { endpointKind: 'Managed' authMode: 'KeyBased' } } // 部署第一个模型 resource model1Deployment 'Microsoft.MachineLearningServices/workspaces/onlineEndpoints/deployments@2024-04-01' = { parent: onlineEndpoint name: 'model1-deployment' properties: { model: { referencePath: '/subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.MachineLearningServices/workspaces/{ws}/models/{model1}/versions/{v1}' } environment: { referencePath: '/subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.MachineLearningServices/workspaces/{ws}/environments/{env}/versions/{envV}' } instanceType: 'Standard_DS3_v2' instanceCount: 1 } } // 部署第二个模型 resource model2Deployment 'Microsoft.MachineLearningServices/workspaces/onlineEndpoints/deployments@2024-04-01' = { parent: onlineEndpoint name: 'model2-deployment' properties: { model: { referencePath: '/subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.MachineLearningServices/workspaces/{ws}/models/{model2}/versions/{v2}' } environment: { referencePath: '/subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.MachineLearningServices/workspaces/{ws}/environments/{env}/versions/{envV}' } instanceType: 'Standard_DS3_v2' instanceCount: 1 } }
2. 构建Pipeline实现定时创建与销毁
- 拆分Pipeline为两个阶段:
- 创建阶段:使用Azure Resource Manager模板部署任务,指定Bicep模板和参数文件,在触发器中设置每日早晨(如6:00)执行。
- 销毁阶段:使用Azure CLI任务执行资源销毁命令,示例YAML片段:
steps: - task: AzureCLI@2 inputs: azureSubscription: '你的服务连接名称' scriptType: 'bash' inlineScript: | # 销毁端点及部署 az ml online-endpoint delete --name <端点名称> --workspace-name <工作区名称> --resource-group <资源组名称> --yes # 如需销毁工作区则执行下方命令 # az ml workspace delete --name <工作区名称> --resource-group <资源组名称> --yes condition: and(succeeded(), eq(variables['Build.Reason'], 'Schedule'))
- 设置定时触发器:在Pipeline的「触发器」选项中添加计划,分别配置早晨创建、夜间(如22:00)销毁的定时规则。
二、解决ruamel.yaml编译错误(缺少longintrepr.h)
- 错误原因:
longintrepr.h是Python 3.9及以下版本的内部头文件,Python 3.10+已移除该文件,若使用的ruamel.yaml版本不兼容Python 3.10+,就会触发编译失败。 - 修复方案:
- 匹配Python与ruamel.yaml版本:要么将Python版本降级至3.9及以下,要么升级ruamel.yaml到0.17.21及以上版本(该版本开始适配Python 3.10+)。
- 在Pipeline中明确指定环境配置:
steps: - task: UsePythonVersion@0 inputs: versionSpec: '3.9' addToPath: true - script: pip install ruamel.yaml==0.17.21 displayName: '安装兼容版本的ruamel.yaml'
- 若使用Azure托管代理,选择包含目标Python版本的镜像(如
ubuntu-latest默认预装多个Python版本)。
内容的提问来源于stack exchange,提问作者ClaudeVernier
相关产品推荐
相关产品推荐

