Microsoft Foundry 与 AZD 的集成 章节导航: 课程主页:AZD 初学者指南 当前章节:第 2 章 - AI 优先开发 ⬅️ 上一章:第 1 章:你的第一个项目 ➡️ 下一步:AI 模型部署 下一章:第 3 章:配置 概述 本指南展示了如何将 Microsoft Foundry 服务与 Azure Developer CLI (AZD) 集成,以简化 AI 应用程序的部署流程。Microsoft Foundry 提供了一个全面的平台,用于构建、部署和管理 AI 应用程序,而 AZD 则简化了基础设施和部署过程。 什么是 Microsoft Foundry?
章节导航:
本指南展示了如何将 Microsoft Foundry 服务与 Azure Developer CLI (AZD) 集成,以简化 AI 应用程序的部署流程。Microsoft Foundry 提供了一个全面的平台,用于构建、部署和管理 AI 应用程序,而 AZD 则简化了基础设施和部署过程。
Microsoft Foundry 是微软统一的 AI 开发平台,包含以下功能:
| 功能 | Microsoft Foundry | AZD 集成优势 |
|---|---|---|
| 模型部署 | 手动门户部署 | 自动化、可重复的部署 |
| 基础设施 | 点击式配置 | 基础设施即代码 (Bicep) |
| 环境管理 | 单一环境 | 多环境(开发/测试/生产) |
| CI/CD 集成 | 有限 | 原生支持 GitHub Actions |
| 成本管理 | 基本监控 | 环境特定的成本优化 |
用例:使用 Azure OpenAI 模型部署聊天应用程序
# azure.yaml name: ai-chat-app services: api: project: ./api host: containerapp env: - AZURE_OPENAI_ENDPOINT - AZURE_OPENAI_API_KEY
基础设施 (main.bicep):
// Azure OpenAI Account resource openAIAccount 'Microsoft.CognitiveServices/accounts@2023-05-01' = { name: openAIAccountName location: location kind: 'OpenAI' sku: { name: 'S0' } properties: { customSubDomainName: openAIAccountName disableLocalAuth: false } } // Deploy GPT model resource gptDeployment 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = { parent: openAIAccount name: 'gpt-35-turbo' properties: { model: { format: 'OpenAI' name: 'gpt-35-turbo' version: '0613' } scaleSettings: { scaleType: 'Standard' capacity: 30 } } }
用例:部署检索增强生成 (RAG) 应用程序
// Azure AI Search resource searchService 'Microsoft.Search/searchServices@2023-11-01' = { name: searchServiceName location: location sku: { name: 'basic' } properties: { replicaCount: 1 partitionCount: 1 hostingMode: 'default' } } // Connect Search with OpenAI resource searchConnection 'Microsoft.Search/searchServices/dataConnections@2023-11-01' = { parent: searchService name: 'openai-connection' properties: { targetResourceId: openAIAccount.id authenticationMethod: 'managedIdentity' } }
用例:文档处理和分析工作流
// Document Intelligence service resource documentIntelligence 'Microsoft.CognitiveServices/accounts@2023-05-01' = { name: documentIntelligenceName location: location kind: 'FormRecognizer' sku: { name: 'S0' } properties: { customSubDomainName: documentIntelligenceName } } // Storage for document processing resource storageAccount 'Microsoft.Storage/storageAccounts@2023-01-01' = { name: storageAccountName location: location sku: { name: 'Standard_LRS' } kind: 'StorageV2' properties: { accessTier: 'Hot' allowBlobPublicAccess: false } }
生产配置:
# Core AI services azd env set AZURE_OPENAI_ENDPOINT "https://your-openai.openai.azure.com/" azd env set AZURE_SEARCH_ENDPOINT "https://your-search.search.windows.net" azd env set AZURE_DOCUMENT_INTELLIGENCE_ENDPOINT "https://your-formrec.cognitiveservices.azure.com/" # Model configurations azd env set AZURE_OPENAI_MODEL "gpt-35-turbo" azd env set AZURE_OPENAI_EMBEDDING_MODEL "text-embedding-ada-002" # Performance settings azd env set AZURE_OPENAI_CAPACITY 30 azd env set AZURE_SEARCH_SKU "standard"
开发配置:
# Cost-optimized settings for development azd env set AZURE_OPENAI_CAPACITY 10 azd env set AZURE_SEARCH_SKU "basic" azd env set AZURE_DOCUMENT_INTELLIGENCE_SKU "F0" # Free tier
// Key Vault for secrets resource keyVault 'Microsoft.KeyVault/vaults@2023-02-01' = { name: keyVaultName location: location properties: { tenantId: tenant().tenantId sku: { family: 'A' name: 'standard' } accessPolicies: [ { tenantId: tenant().tenantId objectId: webAppIdentity.properties.principalId permissions: { secrets: ['get'] } } ] } } // Store OpenAI key securely resource openAIKeySecret 'Microsoft.KeyVault/vaults/secrets@2023-02-01' = { parent: keyVault name: 'openai-api-key' properties: { value: openAIAccount.listKeys().key1 } }
# Deploy everything with one command azd up # Or deploy incrementally azd provision # Infrastructure only azd deploy # Application only
# Development environment azd env new development azd env set AZURE_LOCATION eastus azd env set ENVIRONMENT_TYPE dev azd up # Production environment azd env new production azd env set AZURE_LOCATION westus2 azd env set ENVIRONMENT_TYPE prod azd env set AZURE_OPENAI_CAPACITY 100 azd up
// Application Insights for AI application monitoring resource applicationInsights 'Microsoft.Insights/components@2020-02-02' = { name: applicationInsightsName location: location kind: 'web' properties: { Application_Type: 'web' WorkspaceResourceId: logAnalyticsWorkspace.id } } // Custom metrics for AI operations resource customMetrics 'Microsoft.Insights/components/analyticsItems@2015-05-01' = { parent: applicationInsights name: 'AI-Metrics' properties: { name: 'AI Operations Metrics' content: ''' requests | where name contains "openai" | summarize RequestCount = count(), AvgDuration = avg(duration), SuccessRate = countif(success == true) * 100.0 / count() by bin(timestamp, 5m) ''' } }
// Budget alert for AI services resource budget 'Microsoft.Consumption/budgets@2023-05-01' = { name: 'ai-services-budget' properties: { timePeriod: { startDate: '2024-01-01' endDate: '2024-12-31' } timeGrain: 'Monthly' amount: 500 category: 'Cost' notifications: { notification1: { enabled: true operator: 'GreaterThan' threshold: 80 contactEmails: [ 'admin@company.com' ] } } } }
// Managed identity for the web application resource webAppIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = { name: '${appName}-identity' location: location } // Assign OpenAI User role resource openAIRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = { scope: openAIAccount name: guid(openAIAccount.id, webAppIdentity.id, 'Cognitive Services OpenAI User') properties: { roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd') principalId: webAppIdentity.properties.principalId principalType: 'ServicePrincipal' } }
// Private endpoints for AI services resource openAIPrivateEndpoint 'Microsoft.Network/privateEndpoints@2023-04-01' = { name: '${openAIAccountName}-pe' location: location properties: { subnet: { id: virtualNetwork.properties.subnets[0].id } privateLinkServiceConnections: [ { name: 'openai-connection' properties: { privateLinkServiceId: openAIAccount.id groupIds: ['account'] } } ] } }
# azure.yaml - Redis cache integration services: api: project: ./api host: containerapp env: - REDIS_CONNECTION_STRING - CACHE_TTL=3600
// Redis cache for AI responses resource redisCache 'Microsoft.Cache/redis@2023-04-01' = { name: redisCacheName location: location properties: { sku: { name: 'Basic' family: 'C' capacity: 1 } enableNonSslPort: false minimumTlsVersion: '1.2' } }
// Container App with auto-scaling resource containerApp 'Microsoft.App/containerApps@2023-05-01' = { name: containerAppName location: location properties: { configuration: { ingress: { external: true targetPort: 8000 } } template: { scale: { minReplicas: 1 maxReplicas: 10 rules: [ { name: 'http-scaling' http: { metadata: { concurrentRequests: '30' } } } ] } } } }
症状:
解决方案:
# Check current quota usage az cognitiveservices usage list --location eastus # Try different region azd env set AZURE_LOCATION westus2 azd up # Reduce capacity temporarily azd env set AZURE_OPENAI_CAPACITY 10 azd deploy
症状:
解决方案:
# Verify role assignments az role assignment list --scope /subscriptions/YOUR_SUB/resourceGroups/YOUR_RG # Check managed identity configuration az webapp identity show --name YOUR_APP --resource-group YOUR_RG # Validate Key Vault access az keyvault secret show --vault-name YOUR_KV --name openai-api-key
症状:
解决方案:
# List available models by region az cognitiveservices model list --location eastus # Update model version in bicep template # Check model capacity requirements
服务:Azure OpenAI + 认知搜索 + 应用服务
快速开始:
azd init --template azure-search-openai-demo azd up
服务:文档智能 + 存储 + 函数
快速开始:
azd init --template ai-document-processing azd up
代码库:contoso-chat
服务:Azure OpenAI + 搜索 + 容器应用 + Cosmos DB
快速开始:
azd init --template contoso-chat azd up
目标:部署并测试一个生产就绪的 AI 聊天应用
# Initialize template mkdir ai-chat-demo && cd ai-chat-demo azd init --template azure-search-openai-demo # Set environment variables azd env set AZURE_LOCATION eastus2 azd env set AZURE_OPENAI_CAPACITY 30 # Deploy azd up # Test the application WEB_URL=$(azd show --output json | jq -r '.services.web.endpoint') echo "Chat app: $WEB_URL" # Monitor AI operations azd monitor # Clean up azd down --force --purge
成功标准:
预计成本:测试 30 分钟约 $5-10
目标:部署具有不同配置的多个 AI 模型
# Create custom Bicep configuration cat > infra/ai-models.bicep << 'EOF' param openAiAccountName string param location string resource openAi 'Microsoft.CognitiveServices/accounts@2023-05-01' existing = { name: openAiAccountName } // GPT-4o-mini for general chat resource gpt4omini 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = { parent: openAi name: 'gpt-4o-mini' properties: { model: { format: 'OpenAI' name: 'gpt-4o-mini' version: '2024-07-18' } scaleSettings: { scaleType: 'Standard' capacity: 30 } } } // Text embedding for search resource embedding 'Microsoft.CognitiveServices/accounts/deployments@2023-05-01' = { parent: openAi name: 'text-embedding-ada-002' properties: { model: { format: 'OpenAI' name: 'text-embedding-ada-002' version: '2' } scaleSettings: { scaleType: 'Standard' capacity: 50 } } dependsOn: [gpt4omini] } EOF # Deploy and verify azd provision azd show
成功标准:
目标:设置预算警报和成本跟踪
# Add budget alert to Bicep cat >> infra/main.bicep << 'EOF' resource budget 'Microsoft.Consumption/budgets@2023-05-01' = { name: 'ai-monthly-budget' properties: { timePeriod: { startDate: '2024-01-01' endDate: '2025-12-31' } timeGrain: 'Monthly' amount: 200 category: 'Cost' notifications: { notification1: { enabled: true operator: 'GreaterThan' threshold: 80 contactEmails: ['your-email@example.com'] } notification2: { enabled: true operator: 'GreaterThan' threshold: 100 contactEmails: ['your-email@example.com'] } } } } EOF # Deploy budget alert azd provision # Check current costs az consumption usage list --start-date $(date -d '7 days ago' +%Y-%m-%d) --end-date $(date +%Y-%m-%d)
成功标准:
# Development configuration azd env set AZURE_OPENAI_CAPACITY 10 azd env set ENABLE_RESPONSE_CACHE true
Azure OpenAI:
OpenAI API:
对于生产应用,推荐使用 Azure OpenAI。
# Check current quota az cognitiveservices usage list --location eastus2 # Try different region azd env set AZURE_LOCATION westus2 azd up # Reduce capacity temporarily azd env set AZURE_OPENAI_CAPACITY 10 azd provision # Request quota increase # Go to Azure Portal > Quotas > Request increase
可以!使用 Azure AI Search 实现 RAG(检索增强生成):
# azure.yaml services: ai: env: - AZURE_SEARCH_ENDPOINT - AZURE_SEARCH_INDEX - AZURE_OPENAI_ENDPOINT
参见 azure-search-openai-demo 模板。
最佳实践:
// Managed Identity authentication resource webAppIdentity 'Microsoft.ManagedIdentity/userAssignedIdentities@2023-01-31' = { name: 'web-identity' location: location } resource openAIRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = { scope: openAIAccount name: guid(openAIAccount.id, webAppIdentity.id) properties: { roleDefinitionId: subscriptionResourceId('Microsoft.Authorization/roleDefinitions', '5e0bd9bd-7b93-4f28-af87-19fc36ad61bd') principalId: webAppIdentity.properties.principalId } }
章节导航:
需要帮助? 加入我们的社区讨论或在代码库中提交问题。Azure AI + AZD 社区随时为您提供支持!
免责声明:
本文档使用AI翻译服务Co-op Translator进行翻译。尽管我们努力确保翻译的准确性,但请注意,自动翻译可能包含错误或不准确之处。原始语言的文档应被视为权威来源。对于关键信息,建议使用专业人工翻译。我们对因使用此翻译而产生的任何误解或误读不承担责任。