-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSFCallGPT_ARM_template.json
272 lines (272 loc) · 16.5 KB
/
SFCallGPT_ARM_template.json
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"apiManagementServiceName": {
"type": "String"
},
"functionAppName": {
"type": "String"
},
"storageAccountName": {
"defaultValue": "[concat('sa', uniqueString(resourceGroup().id))]",
"type": "String"
},
"publisherEmail": {
"type": "String"
},
"azureadApplicationId": {
"type": "String"
}
},
"variables": {
"functionAppId": "[resourceId('Microsoft.Web/sites', parameters('functionAppName'))]"
},
"resources": [
{
"type": "Microsoft.ApiManagement/service",
"apiVersion": "2019-12-01",
"name": "[parameters('apiManagementServiceName')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Web/sites', parameters('functionAppName'))]"
],
"sku": {
"name": "Developer",
"capacity": 1
},
"properties": {
"publisherEmail": "[parameters('publisherEmail')]",
"publisherName": "[parameters('publisherEmail')]",
"notificationSenderEmail": "[email protected]",
"hostnameConfigurations": [
{
"type": "Proxy",
"hostName": "[concat(parameters('apiManagementServiceName'), '.azure-api.net')]",
"negotiateClientCertificate": false,
"defaultSslBinding": true
}
],
"customProperties": {
"Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Protocols.Tls10": "False",
"Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Protocols.Tls11": "False",
"Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Protocols.Ssl30": "False",
"Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Ciphers.TripleDes168": "False",
"Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Backend.Protocols.Tls10": "False",
"Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Backend.Protocols.Tls11": "False",
"Microsoft.WindowsAzure.ApiManagement.Gateway.Security.Backend.Protocols.Ssl30": "False",
"Microsoft.WindowsAzure.ApiManagement.Gateway.Protocols.Server.Http2": "False"
},
"virtualNetworkType": "None",
"disableGateway": false
}
},
{
"type": "Microsoft.ApiManagement/service/apis",
"apiVersion": "2019-12-01",
"name": "[concat(parameters('apiManagementServiceName'), '/', parameters('apiManagementServiceName'))]",
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service', parameters('apiManagementServiceName'))]"
],
"properties": {
"displayName": "ext-func-api",
"apiRevision": "1",
"description": "[concat('Import from \"', parameters('functionAppName'), '\" Function App')]",
"subscriptionRequired": false,
"path": "ext-func-api",
"protocols": [
"https"
],
"isCurrent": true
}
},
{
"type": "Microsoft.ApiManagement/service/backends",
"apiVersion": "2019-12-01",
"name": "[concat(parameters('apiManagementServiceName'), '/', parameters('apiManagementServiceName'))]",
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service', parameters('apiManagementServiceName'))]"
],
"properties": {
"description": "[concat('\"', parameters('functionAppName'), '\" Function App')]",
"url": "[concat('https://', parameters('functionAppName'), '.azurewebsites.net/api')]",
"protocol": "http",
"resourceId": "[concat('https://management.azure.com/subscriptions/', subscription().subscriptionId, '/resourceGroups/', resourceGroup().name, '/providers/Microsoft.Web/sites/', parameters('functionAppName'))]",
"credentials": {
"header": {
"x-functions-key": [
"[listkeys(concat(variables('functionAppId'), '/host/default/'),'2019-08-01').functionKeys.default]"
]
}
}
}
},
{
"type": "Microsoft.ApiManagement/service/policies",
"apiVersion": "2019-12-01",
"name": "[concat(parameters('apiManagementServiceName'), '/policy')]",
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service', parameters('apiManagementServiceName'))]"
],
"properties": {
"value": "<!--\r\n IMPORTANT:\r\n - Policy elements can appear only within the <inbound>, <outbound>, <backend> section elements.\r\n - Only the <forward-request> policy element can appear within the <backend> section element.\r\n - To apply a policy to the incoming request (before it is forwarded to the backend service), place a corresponding policy element within the <inbound> section element.\r\n - To apply a policy to the outgoing response (before it is sent back to the caller), place a corresponding policy element within the <outbound> section element.\r\n - To add a policy position the cursor at the desired insertion point and click on the round button associated with the policy.\r\n - To remove a policy, delete the corresponding policy statement from the policy document.\r\n - Policies are applied in the order of their appearance, from the top down.\r\n-->\r\n<policies>\r\n <inbound />\r\n <backend>\r\n <forward-request />\r\n </backend>\r\n <outbound />\r\n</policies>",
"format": "xml"
}
},
{
"type": "Microsoft.ApiManagement/service/apis/operations",
"apiVersion": "2019-12-01",
"name": "[concat(parameters('apiManagementServiceName'), '/', parameters('apiManagementServiceName'), '/post-call_gpt')]",
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service/apis', parameters('apiManagementServiceName'), parameters('apiManagementServiceName'))]",
"[resourceId('Microsoft.ApiManagement/service', parameters('apiManagementServiceName'))]"
],
"properties": {
"displayName": "call_gpt",
"method": "POST",
"urlTemplate": "/call_gpt",
"templateParameters": [],
"responses": []
}
},
{
"type": "Microsoft.ApiManagement/service/apis/operations/policies",
"apiVersion": "2019-12-01",
"name": "[concat(parameters('apiManagementServiceName'), '/', parameters('apiManagementServiceName'), '/post-call_gpt/policy')]",
"dependsOn": [
"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('apiManagementServiceName'), parameters('apiManagementServiceName'), 'post-call_gpt')]",
"[resourceId('Microsoft.ApiManagement/service/apis', parameters('apiManagementServiceName'), parameters('apiManagementServiceName'))]",
"[resourceId('Microsoft.ApiManagement/service', parameters('apiManagementServiceName'))]"
],
"properties": {
"value": "[concat('<policies>\r\n <inbound>\r\n <base />\r\n <set-backend-service id=\"apim-generated-policy\" backend-id=\"', parameters('apiManagementServiceName'), '\" />', '<validate-jwt header-name=\"Authorization\" failed-validation-httpcode=\"401\"> \r\n <openid-config url=\"https://login.microsoftonline.com/common/.well-known/openid-configuration\" /> \r\n <required-claims> \r\n <claim name=\"appid\" match=\"any\"> \r\n <value>SNOWFLAKE_SERVICE_PRINCIPAL_ID</value> \r\n </claim> \r\n <claim name=\"aud\" match=\"any\"> \r\n <value>', parameters('azureadApplicationId') ,'</value> \r\n </claim> \r\n </required-claims> \r\n </validate-jwt>' ,'\r\n </inbound>\r\n <backend>\r\n <base />\r\n </backend>\r\n <outbound>\r\n <base />\r\n </outbound>\r\n <on-error>\r\n <base />\r\n </on-error>\r\n</policies>')]",
"format": "xml"
}
},
{
"type": "Microsoft.Web/sites",
"apiVersion": "2018-11-01",
"name": "[parameters('functionAppName')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[concat('Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]"
],
"kind": "functionapp",
"properties": {
"name": "[parameters('functionAppName')]",
"siteConfig": {
"appSettings": [
{
"name": "FUNCTIONS_EXTENSION_VERSION",
"value": "~3"
},
{
"name": "FUNCTIONS_WORKER_RUNTIME",
"value": "node"
},
{
"name": "WEBSITE_NODE_DEFAULT_VERSION",
"value": "~12"
},
{
"name": "AzureWebJobsStorage",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2019-06-01').keys[0].value,';EndpointSuffix=','core.windows.net')]"
},
{
"name": "AzureWebJobsDashboard",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2019-06-01').keys[0].value,';EndpointSuffix=','core.windows.net')]"
},
{
"name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=',parameters('storageAccountName'),';AccountKey=',listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('storageAccountName')), '2019-06-01').keys[0].value,';EndpointSuffix=','core.windows.net')]"
},
{
"name": "WEBSITE_CONTENTSHARE",
"value": "[toLower(parameters('functionAppName'))]"
},
{
"name": "OPENAI_API_KEY",
"value": ""
},
{
"name": "OPENAI_HOSTNAME",
"value": ""
},
{
"name": "OPENAI_PATH",
"value": ""
}
]
},
"clientAffinityEnabled": true
},
"resources": [
{
"name": "[concat(parameters('functionAppName'), '/authsettings')]",
"apiVersion": "2018-11-01",
"type": "Microsoft.Web/sites/config",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Web/sites', parameters('functionAppName'))]"
],
"properties": {
"enabled": true,
"unauthenticatedClientAction": "RedirectToLoginPage",
"tokenStoreEnabled": true,
"defaultProvider": "AzureActiveDirectory",
"clientId": "[parameters('azureadApplicationId')]",
"issuer": "[concat('https://sts.windows.net/', subscription().tenantId, '/')]"
}
},
{
"apiVersion": "2019-08-01",
"name": "[concat(parameters('functionAppName'),'/', 'call_gpt')]",
"type": "Microsoft.Web/sites/functions",
"dependsOn": [
"[resourceId('Microsoft.Web/sites', parameters('functionAppName'))]"
],
"properties": {
"config": {
"bindings": [
{
"name": "req",
"webHookType": "genericJson",
"direction": "in",
"type": "httpTrigger",
"authLevel": "function"
},
{
"name": "res",
"direction": "out",
"type": "http"
}
]
},
"files": {
"index.js": "const https = require('https')\r\n\r\nvar OPENAI_API_KEY = process.env[\"OPENAI_API_KEY\"];\r\nvar OPENAI_HOSTNAME = process.env[\"OPENAI_HOSTNAME\"];\r\nvar OPENAI_PATH = process.env[\"OPENAI_PATH\"];\r\n\r\nfunction get_data(options, data) {\r\n return new Promise((resolve, reject) => {\r\n const req = https.request(options, res => {\r\n let data = ''\r\n res.on('data', chunk => {\r\n data += chunk\r\n })\r\n res.on('end', () => {\r\n resolve(JSON.parse(data));\r\n })\r\n })\r\n .on('error', err => {\r\n console.log('Error: ', err.message)\r\n reject(err)\r\n })\r\n req.write(data)\r\n req.end()\r\n })\r\n}\r\n\r\n//(async () => console.log(await get_data(options, data)))()\r\n\r\nmodule.exports = async function (context, req) {\r\n if (req.body) {\r\n var rows = req.body.data;\r\n var results = [];\r\n var index = 0;\r\n for (var i = 0; i < rows.length; i++){\r\n data = JSON.stringify({ \"messages\": [{ \"role\": \"system\", \"content\": rows[i][1].replace(/[^A-Za-z0-9 ?!.$%&,:\\-/\\\\;]/g, \" \") }, { \"role\": \"user\", \"content\": rows[i][2].replace(/[^A-Za-z0-9 ?!.$%&,:\\-/\\\\;]/g, \" \") }] })\r\n var res = await get_data({\r\n hostname: OPENAI_HOSTNAME,\r\n path: OPENAI_PATH,\r\n method: 'POST',\r\n headers: {\r\n 'Content-Type': 'application/json',\r\n 'Content-Length': [...data].length,\r\n 'api-key': OPENAI_API_KEY\r\n }\r\n }, data);\r\n results.push([index++, res.choices?res.choices[0].message.content:res]);\r\n }\r\n results = { \"data\": results }\r\n context.res = {\r\n body: JSON.stringify(results)\r\n };\r\n }\r\n else {\r\n context.res = {\r\n status: 400,\r\n body: \"Please pass data in the request body\"\r\n };\r\n }\r\n} "
}
}
}
]
},
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2019-06-01",
"name": "[parameters('storageAccountName')]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Standard_LRS"
}
}
],
"outputs": {
"API Management URL": {
"value": "[concat('https://', parameters('apiManagementServiceName'), '.azure-api.net')]",
"type": "string"
},
"Azure Function Http Trigger URL": {
"value": "[concat('https://', parameters('apiManagementServiceName'), '.azure-api.net', '/ext-func-api/call_gpt')]",
"type": "string"
}
}
}