|
| 1 | +// ------------------ |
| 2 | +// PARAMETERS |
| 3 | +// ------------------ |
| 4 | + |
| 5 | +// Typically, parameters would be decorated with appropriate metadata and attributes, but as they are very repetetive in these labs we omit them for brevity. |
| 6 | + |
| 7 | +param apimSku string |
| 8 | +param openAIConfig array = [] |
| 9 | +param openAIModelName string |
| 10 | +param openAIModelVersion string |
| 11 | +param openAIDeploymentName string |
| 12 | +param openAIModelCapacity int |
| 13 | +param openAIAPIVersion string = '2024-02-01' |
| 14 | +param policyXml string |
| 15 | + |
| 16 | +// ------------------ |
| 17 | +// VARIABLES |
| 18 | +// ------------------ |
| 19 | + |
| 20 | +var resourceSuffix = uniqueString(subscription().id, resourceGroup().id) |
| 21 | +var apiManagementName = 'apim-${resourceSuffix}' |
| 22 | +var openAISubscriptionName = 'openai-subscription' |
| 23 | +var openAISubscriptionDescription = 'OpenAI Subscription' |
| 24 | +var openAIAPIName = 'openai' |
| 25 | + |
| 26 | +// ------------------ |
| 27 | +// RESOURCES |
| 28 | +// ------------------ |
| 29 | + |
| 30 | +// 1. Log Analytics Workspace |
| 31 | +module lawModule '../../modules/operational-insights/v1/workspaces.bicep' = { |
| 32 | + name: 'lawModule' |
| 33 | +} |
| 34 | + |
| 35 | +var lawId = lawModule.outputs.id |
| 36 | + |
| 37 | +// 2. Application Insights |
| 38 | +module appInsightsModule '../../modules/monitor/v1/appinsights.bicep' = { |
| 39 | + name: 'appInsightsModule' |
| 40 | + params: { |
| 41 | + workbookJson: loadTextContent('openai-usage-analysis-workbook.json') |
| 42 | + lawId: lawId |
| 43 | + customMetricsOptedInType: 'WithDimensions' |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +var appInsightsId = appInsightsModule.outputs.id |
| 48 | +var appInsightsInstrumentationKey = appInsightsModule.outputs.instrumentationKey |
| 49 | + |
| 50 | +// 3. API Management |
| 51 | +module apimModule '../../modules/apim/v1/apim.bicep' = { |
| 52 | + name: 'apimModule' |
| 53 | + params: { |
| 54 | + apimSku: apimSku |
| 55 | + appInsightsInstrumentationKey: appInsightsInstrumentationKey |
| 56 | + appInsightsId: appInsightsId |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// 4. Cognitive Services |
| 61 | +module openAIModule '../../modules/cognitive-services/v1/openai.bicep' = { |
| 62 | + name: 'openAIModule' |
| 63 | + params: { |
| 64 | + openAIConfig: openAIConfig |
| 65 | + openAIDeploymentName: openAIDeploymentName |
| 66 | + openAIModelName: openAIModelName |
| 67 | + openAIModelVersion: openAIModelVersion |
| 68 | + openAIModelCapacity: openAIModelCapacity |
| 69 | + apimPrincipalId: apimModule.outputs.principalId |
| 70 | + lawId: lawId |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +// 5. APIM OpenAI API |
| 75 | +module openAIAPIModule '../../modules/apim/v1/openai-api.bicep' = { |
| 76 | + name: 'openAIAPIModule' |
| 77 | + params: { |
| 78 | + policyXml: policyXml |
| 79 | + openAIConfig: openAIModule.outputs.extendedOpenAIConfig |
| 80 | + openAIAPIVersion: openAIAPIVersion |
| 81 | + appInsightsInstrumentationKey: appInsightsInstrumentationKey |
| 82 | + appInsightsId: appInsightsId |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +// 6. Create New APIM Subscriptions |
| 87 | + |
| 88 | +// We presume the APIM resource has been created as part of this bicep flow. |
| 89 | +resource apim 'Microsoft.ApiManagement/service@2024-06-01-preview' existing = { |
| 90 | + name: apiManagementName |
| 91 | + dependsOn: [ |
| 92 | + apimModule |
| 93 | + ] |
| 94 | +} |
| 95 | + |
| 96 | +resource api 'Microsoft.ApiManagement/service/apis@2024-06-01-preview' existing = { |
| 97 | + parent: apim |
| 98 | + name: openAIAPIName |
| 99 | + dependsOn: [ |
| 100 | + openAIAPIModule |
| 101 | + ] |
| 102 | +} |
| 103 | + |
| 104 | +// Ignore the subscription that gets created in the APIM module and create three new ones for this lab. |
| 105 | +resource apimSubscriptions 'Microsoft.ApiManagement/service/subscriptions@2024-06-01-preview' = [for i in range(1, 3): { |
| 106 | + name: '${openAISubscriptionName}${i}' |
| 107 | + parent: apim |
| 108 | + properties: { |
| 109 | + allowTracing: true |
| 110 | + displayName: '${openAISubscriptionDescription} ${i}' |
| 111 | + scope: '/apis/${api.id}' |
| 112 | + state: 'active' |
| 113 | + } |
| 114 | + dependsOn: [ |
| 115 | + api |
| 116 | + ] |
| 117 | +}] |
| 118 | + |
| 119 | +// ------------------ |
| 120 | +// MARK: OUTPUTS |
| 121 | +// ------------------ |
| 122 | + |
| 123 | +output applicationInsightsAppId string = appInsightsModule.outputs.appId |
| 124 | +output applicationInsightsName string = appInsightsModule.outputs.applicationInsightsName |
| 125 | +output logAnalyticsWorkspaceId string = lawModule.outputs.customerId |
| 126 | +output apimServiceId string = apimModule.outputs.id |
| 127 | +output apimResourceGatewayURL string = apimModule.outputs.gatewayUrl |
| 128 | + |
| 129 | +#disable-next-line outputs-should-not-contain-secrets |
| 130 | +output apimSubscription1Key string = apimSubscriptions[0].listSecrets().primaryKey |
| 131 | +#disable-next-line outputs-should-not-contain-secrets |
| 132 | +output apimSubscription2Key string = apimSubscriptions[1].listSecrets().primaryKey |
| 133 | +#disable-next-line outputs-should-not-contain-secrets |
| 134 | +output apimSubscription3Key string = apimSubscriptions[2].listSecrets().primaryKey |
0 commit comments