AI工作坊实验:让您的AI解决方案支持AZD部署 章节导航: 课程主页:AZD入门 当前章节:第2章 - AI优先开发 ⬅️ 上一章:AI模型部署 ➡️ 下一章:生产环境AI最佳实践 下一章节:第3章:配置 工作坊概述 本动手实验指导开发者如何使用Azure Developer CLI (AZD)部署现有的AI模板。您将学习使用Microsoft Foundry服务进行生产环境AI部署的关键模式。
章节导航:
本动手实验指导开发者如何使用Azure Developer CLI (AZD)部署现有的AI模板。您将学习使用Microsoft Foundry服务进行生产环境AI部署的关键模式。
时长: 2-3小时
难度: 中级
前提条件: 基本的Azure知识,熟悉AI/ML概念
完成本实验后,您将能够:
# Check AZD installation azd version # Check Azure CLI az --version # Login to Azure az login azd auth login
git clone https://github.com/Azure-Samples/azure-search-openai-demo cd azure-search-openai-demo
探索AI就绪AZD模板中的关键文件:
azure-search-openai-demo/ ├── azure.yaml # AZD configuration ├── infra/ # Infrastructure as Code │ ├── main.bicep # Main infrastructure template │ ├── main.parameters.json # Environment parameters │ └── modules/ # Reusable Bicep modules │ ├── openai.bicep # Azure OpenAI configuration │ ├── search.bicep # Cognitive Search setup │ └── webapp.bicep # Web app configuration ├── app/ # Application code ├── scripts/ # Deployment scripts └── .azure/ # AZD environment files
cat azure.yaml
需要关注的内容:
cat infra/main.bicep
需要识别的关键AI模式:
azd env new myai-workshop
# Set your preferred Azure region azd env set AZURE_LOCATION eastus # Optional: Set specific OpenAI model azd env set AZURE_OPENAI_MODEL gpt-35-turbo
azd up
azd up期间发生的事情:
azd show
azd show --output json | grep "webAppUrl"
场景:您的部署成功,但AI没有响应。
需要检查的常见问题:
调试命令:
# Check environment variables azd env get-values # View deployment logs az webapp log tail --name YOUR_APP_NAME --resource-group YOUR_RG # Check OpenAI deployment status az cognitiveservices account deployment list --name YOUR_OPENAI_NAME --resource-group YOUR_RG
# Change to a different model (if available in your region) azd env set AZURE_OPENAI_MODEL gpt-4 # Redeploy with the new configuration azd deploy
编辑infra/main.bicep以添加Document Intelligence:
// Add to main.bicep resource documentIntelligence 'Microsoft.CognitiveServices/accounts@2023-05-01' = { name: 'doc-intel-${uniqueString(resourceGroup().id)}' location: location kind: 'FormRecognizer' sku: { name: 'F0' // Free tier for workshop } properties: { customSubDomainName: 'doc-intel-${uniqueString(resourceGroup().id)}' } }
最佳实践:开发与生产环境使用不同的配置。
azd env new myai-production
# Production typically uses higher SKUs azd env set AZURE_OPENAI_SKU S0 azd env set AZURE_SEARCH_SKU standard # Enable additional security features azd env set ENABLE_PRIVATE_ENDPOINTS true
挑战:配置模板以实现成本效益的开发。
任务:
解决提示:
当前挑战:许多AI应用硬编码API密钥或使用不安全的存储。
AZD解决方案:托管身份+Key Vault集成。
# Look for Key Vault and Managed Identity configuration grep -r "keyVault\|managedIdentity" infra/
# Check if the web app has the correct identity configuration az webapp identity show --name YOUR_APP_NAME --resource-group YOUR_RG
添加到您的bicep模板:
// Private endpoint for OpenAI resource openAIPrivateEndpoint 'Microsoft.Network/privateEndpoints@2023-04-01' = { name: 'pe-openai-${uniqueString(resourceGroup().id)}' location: location properties: { subnet: { id: vnet.properties.subnets[0].id } privateLinkServiceConnections: [ { name: 'openai-connection' properties: { privateLinkServiceId: openAIAccount.id groupIds: ['account'] } } ] } }
# Application Insights should be automatically configured # Check the configuration: az monitor app-insights component show --app YOUR_APP_NAME --resource-group YOUR_RG
为AI操作添加自定义指标:
// In your web app configuration resource webApp 'Microsoft.Web/sites@2023-01-01' = { properties: { siteConfig: { appSettings: [ { name: 'APPLICATIONINSIGHTS_CONNECTION_STRING' value: applicationInsights.properties.ConnectionString } { name: 'OPENAI_MONITOR_ENABLED' value: 'true' } ] } } }
任务:审查您的部署是否符合安全最佳实践。
检查清单:
在转换您的应用之前,回答以下问题:
应用架构:
安全需求:
扩展需求:
按照以下模式转换您的应用:
mkdir my-ai-app-azd cd my-ai-app-azd # Initialize AZD template azd init --template minimal
# Metadata name: my-ai-app metadata: template: my-ai-app-template@0.0.1-beta # Services definition services: api: project: ./api host: containerapp web: project: ./web host: staticwebapp # Hooks for custom deployment logic hooks: predeploy: shell: sh run: echo "Preparing AI models..."
infra/main.bicep - 主模板:
@description('Primary location for all resources') param location string = resourceGroup().location @description('Name of the OpenAI service') param openAIServiceName string = 'openai-${uniqueString(resourceGroup().id)}' // Your AI services here module openAI 'modules/openai.bicep' = { name: 'openai' params: { name: openAIServiceName location: location } }
infra/modules/openai.bicep - OpenAI模块:
@description('Name of the OpenAI service') param name string @description('Location for the OpenAI service') param location string resource openAIAccount 'Microsoft.CognitiveServices/accounts@2023-05-01' = { name: name location: location kind: 'OpenAI' sku: { name: 'S0' } properties: { customSubDomainName: name } } output endpoint string = openAIAccount.properties.endpoint output name string = openAIAccount.name
挑战:为文档处理AI应用创建AZD模板。
需求:
加分项:
症状: 部署失败并显示配额错误
解决方案:
# Check current quotas az cognitiveservices usage list --location eastus # Request quota increase or try different region azd env set AZURE_LOCATION westus2 azd up
症状: AI响应失败或模型部署错误
解决方案:
# Check model availability by region az cognitiveservices model list --location eastus # Update to available model azd env set AZURE_OPENAI_MODEL gpt-35-turbo-16k azd deploy
症状: 调用AI服务时出现403 Forbidden错误
解决方案:
# Check role assignments az role assignment list --scope /subscriptions/YOUR_SUB/resourceGroups/YOUR_RG # Add missing roles az role assignment create \ --assignee YOUR_PRINCIPAL_ID \ --role "Cognitive Services OpenAI User" \ --scope /subscriptions/YOUR_SUB/resourceGroups/YOUR_RG
调查步骤:
解决方案:
场景:您的部署成功,但应用返回500错误。
调试任务:
使用工具:
azd show查看部署概览导航到Azure门户并创建一个仪表板,包含:
# Alert for high error rate az monitor metrics alert create \ --name "AI-App-High-Error-Rate" \ --resource-group YOUR_RG \ --target-resource-id YOUR_APP_ID \ --condition "avg Http5xx greater than 10" \ --description "Alert when error rate is high"
# Use Azure CLI to get cost data az consumption usage list --start-date 2024-01-01 --end-date 2024-01-31
任务:优化您的AI应用以提高性能并降低成本。
需要改进的指标:
可尝试的策略:
您需要创建一个生产级AI驱动的客户服务聊天机器人,要求如下:
功能需求:
非功能需求:
恭喜!您已完成 AI 工作坊实验室。现在您应该能够:
工作坊反馈:通过在 Microsoft Foundry Discord #Azure 频道 分享您的体验,帮助我们改进此工作坊。
章节导航:
需要帮助? 加入我们的社区,参与关于 AZD 和 AI 部署的支持与讨论。
免责声明:
本文档使用AI翻译服务Co-op Translator进行翻译。尽管我们努力确保翻译的准确性,但请注意,自动翻译可能包含错误或不准确之处。原始语言的文档应被视为权威来源。对于重要信息,建议使用专业人工翻译。我们不对因使用此翻译而产生的任何误解或误读承担责任。