You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -33,7 +33,7 @@ During this tutorial, you learn how to accomplish these tasks:
33
33
34
34
## Create your Azure OpenAI resources
35
35
36
-
Use these steps to create an Azure OpenAI data model:
36
+
Use these steps to create a data model resource in Azure OpenAI:
37
37
38
38
### [Azure portal](#tab/azure-portal)
39
39
@@ -65,34 +65,85 @@ Use these steps to create an Azure OpenAI data model:
65
65
66
66
### [Azure CLI](#tab/azure-cli)
67
67
68
-
1. Use the command to
68
+
1. Use this [`az cognitiveservices account create`](/cli/azure/cognitiveservices/account#az-cognitiveservices-account-create) command to create your Azure OpenAI account:
69
+
70
+
```azurecli
71
+
az cognitiveservices account create --name <OPENAI_RESOURCE_NAME> \
In this command, replace `<OPENAI_RESOURCE_NAME>` with a friendly name for your Azure OpenAI resource, along with `<RESOURCE_GROUP>` and `<REGION>`, which are the same resource group and region as your existing function app, respectively. You can also pass `--yes` to automatically accept the terms.
69
77
70
78
---
71
79
72
80
Next, you grant access to the user-assigned managed identity to be able to connect to your Azure OpenAI resource.
73
81
74
-
## 4. Grant user access to Azure OpenAI
82
+
## Grant access to the Azure OpenAI resource
83
+
84
+
Use these steps to grant access in your new Azure OpenAI resource to these identities:
85
+
86
+
- The user-assigned manage identity associated with your app.
87
+
- Your Azure account, required to connect using Microsoft Entra ID when running locally.
75
88
76
-
Use these steps to grant access in your new Azure OpenAI resource both to the user-assigned manage identity associated with your app and to your Azure account.
89
+
### [Azure portal](#tab/azure-portal)
77
90
78
91
1. In your Azure OpenAI resource, select **Access control (IAM)** on the left pane.
79
92
80
93
1. Select **Add** > **Add role assignment**.
81
94
82
-
1. On the **Role** tab on the next screen, select **Cognitive Services OpenAI User** and then **Next**.
95
+
1. On the **Role** tab on the next screen, search for and select **Cognitive Services OpenAI User** and then select **Next**.
83
96
84
97
1. On the **Members** tab, select **Assign access to** > **Managed identity** then **Select members**.
85
98
86
99
1. In the **Select managed identities** pane, select **Managed identity** > **User assigned managed identity**.
87
100
88
-
1. Choose your user-assigned managed identity from the list and then **Select** > **Review + assign**.
101
+
1. Choose your user-assigned managed identity from the list and then **Select**.
102
+
103
+
1. Switch **Assign access to** to **User, group, or service principal** and choose **Select members**.
104
+
105
+
1. Choose your Azure account used during local development and then **Select** > **Review + assign**.
89
106
90
107
1. On the **Review + assign** tab, select **Review + assign** to assign the role.
91
108
92
-
## 5. Deploy a model
109
+
### [Azure CLI](#tab/azure-cli)
110
+
111
+
1. Use the [`az role assignment create`](/cli/azure/role/assignment#az-role-assignment-create) command to add your user-assigned managed identity to the `Cognitive Services OpenAI User` role:
112
+
113
+
```azurecli
114
+
# Get the principal ID for the user-assigned managed identity.
115
+
principalId=$(az identity show --name <USER_NAME> --resource-group `<RESOURCE_GROUP>` \
116
+
--query "principalId" -o tsv)
117
+
# Get the fully-qualified ID of the Azure OpenAI resource.
118
+
openaiId=$(az cognitiveservices account show --name `<OPENAI_RESOURCE_NAME>` \
In these commands, replace `<OPENAI_RESOURCE_NAME>` with the name of your new Azure OpenAI resource and `<RESOURCE_GROUP>` the resource group. The [`az identity show`](/cli/azure/identity#az-identity-show) and [`az cognitiveservices account show`](/cli/azure/cognitiveservices/account#az-cognitiveservices-account-show) commands are used to obtain the principal ID of the user-assigned managed identity and the fully-qualified ID of the Azure OpenAI resource, respectively.
126
+
127
+
1. Use the [`az role assignment create`](/cli/azure/role/assignment#az-role-assignment-create) command again to create the same role assignment for your Azure account, so you can connect during local development:
128
+
129
+
```azurecli
130
+
# Get your current Azure account ID.
131
+
accountId=$(az ad signed-in-user show --query id -o tsv)
132
+
# Add your current Azure account to the 'Cognitive Services OpenAI User' role.
The [`az ad signed-in-user show`](/cli/azure/ad/signed-in-user?view=azure-cli-latest#az-ad-signed-in-user-show) command is used to obtain your Azure account ID. You reuse `$openaiId` from the previous step, which is the fully-qualified ID of the Azure OpenAI resource .
138
+
139
+
---
140
+
141
+
## Deploy a model
93
142
94
143
Now you can deploy a model. You can select from one of several available models in Azure OpenAI Studio.
95
144
145
+
### [Azure portal](#tab/azure-portal)
146
+
96
147
To deploy a model, follow these steps:
97
148
98
149
1. Sign in to [Azure OpenAI Studio](https://oai.azure.com).
@@ -107,30 +158,51 @@ To deploy a model, follow these steps:
107
158
108
159
1. Confirm the **Deployment name** for your model deployment and select **Deploy**.
109
160
110
-
Choose a deployment name carefully. The deployment name is used in your code to call the model by using the client libraries and the REST APIs, so you must save it for use later on.
111
161
112
-
> [!IMPORTANT]
113
-
> When you access the model via the API, you need to refer to the deployment name rather than the underlying model name in API calls, which is one of the key differences between OpenAI and Azure OpenAI. OpenAI only requires the model name. Azure OpenAI always requires deployment name, even when using the model parameter. In our docs, we often have examples where deployment names are represented as identical to model names to help indicate which model works with a particular API endpoint. Ultimately your deployment names can follow whatever naming convention is best for your use case.
162
+
### [Azure CLI](#tab/azure-cli)
163
+
164
+
Use the [`az cognitiveservices account deployment create`](/cli/azure/cognitiveservices/account/deployment#az-cognitiveservices-account-deployment-create) command to select and deploy a model in your Azure OpenAI resource:
165
+
166
+
```azurecli
167
+
az cognitiveservices account deployment create --name <OPENAI_RESOURCE_NAME> \
|`<OPENAI_RESOURCE_NAME>`| Your Azure OpenAI resource name |
177
+
|`<RESOURCE_GROUP>`| Your resource group name |
178
+
|`<DEPLOYMENT_NAME>`| The name you want for this deployment, which is used in your code |
179
+
|`<MODEL_NAME>`| The model to deploy, such as `gpt-4o`|
180
+
|`<MODEL_VERSION>`| The version of your chosen model, such as `2024-05-01`|
181
+
182
+
---
183
+
184
+
Choose a deployment name carefully. The deployment name is used in your code to call the model by using the client libraries and the REST APIs, so you must save it for use later on.
185
+
186
+
> [!IMPORTANT]
187
+
> When you access the model via the API, you need to refer to the deployment name rather than the underlying model name in API calls, which is one of the key differences between OpenAI and Azure OpenAI. OpenAI only requires the model name. Azure OpenAI always requires deployment name, even when using the model parameter. In our docs, we often have examples where deployment names are represented as identical to model names to help indicate which model works with a particular API endpoint. Ultimately your deployment names can follow whatever naming convention is best for your use case.
114
188
115
-
You now have everything you need to add Azure OpenAI-based text completion to your function app.
189
+
You now have everything you need to add Azure OpenAI-enabled behaviors to your function app.
116
190
117
-
## 6. Update application settings
191
+
## Update local settings
118
192
119
193
1. In Visual Studio Code, open the local code project you created when you completed the [previous article](./create-first-function-vs-code-csharp.md).
120
194
121
-
1. In the local.settings.json file in the project root folder, update the `AzureWebJobsStorage` setting to `UseDevelopmentStorage=true`. You can skip this step if the `AzureWebJobsStorage` setting in *local.settings.json* is set to the connection string for an existing Azure Storage account instead of `UseDevelopmentStorage=true`.
195
+
1. In the local.settings.json file in the project root folder, update the `AzureWebJobsStorage` setting to `UseDevelopmentStorage=true`. You can skip this step if the `AzureWebJobsStorage` setting in _local.settings.json_ is set to the connection string for an existing Azure Storage account instead of `UseDevelopmentStorage=true`.
122
196
123
197
1. In the local.settings.json file, add these settings values:
124
198
125
199
+**`AzureOpenAI__endpoint`**: required by the binding extension. Set this value to the endpoint of the Azure OpenAI resource you created earlier.
126
-
+**`"AzureOpenAI__tenantId"`**: required when running locally to connect to Azure OpenAI with the user-assigned managed identity. Set this value to the **Tenant ID** value you saved earlier.
127
-
+**`AzureOpenAI__clientId`**: required when running locally and in Azure. Set this value to the **Client ID** value you saved earlier.
128
200
+**`CHAT_MODEL_DEPLOYMENT_NAME`**: used by input binding settings. Set this value to the name you chose for your model deployment.
129
201
130
202
1. Save the file. When you deploy to Azure, you must also add these settings to your function app.
131
203
132
204
:::zone pivot="programming-language-csharp"
133
-
## 7. Register binding extensions
205
+
## Register binding extensions
134
206
135
207
Because you're using an Azure OpenAI output binding, you must have the corresponding bindings extension installed before you run the project.
<!---NOTE: Update this after preview to `## Verify the extension bundle`-->
145
-
## 7. Update the extension bundle
217
+
## Update the extension bundle
146
218
147
219
To access the preview Azure OpenAI bindings, you must use a preview version of the extension bundle that contains this extension.
148
220
@@ -157,7 +229,7 @@ Replace the `extensionBundle` setting in your current `host.json` file with this
157
229
:::zone-end
158
230
Now, you can use the Azure OpenAI output binding in your project.
159
231
160
-
## 8. Return text completion from the model
232
+
## Return text completion from the model
161
233
162
234
The code you add creates a `whois` HTTP function endpoint in your existing project. In this function, data passed in a URL `name` parameter of a GET request is used to dynamically create a completion prompt. This dynamic prompt is bound to a text completion input binding, which returns a response from the model based on the prompt. The completion from the model is returned in the HTTP response.
163
235
:::zone pivot="programming-language-csharp"
@@ -167,21 +239,7 @@ The code you add creates a `whois` HTTP function endpoint in your existing proje
167
239
168
240
1. In the same file, add this code that defines a new HTTP trigger endpoint named `whois`:
@@ -240,7 +298,7 @@ The code you add creates a `whois` HTTP function endpoint in your existing proje
240
298
241
299
:::zone-end
242
300
243
-
## 9. Run the function
301
+
## Run the function
244
302
245
303
1. In Visual Studio Code, Press F1 and in the command palette type `Azurite: Start` and press Enter to start the Azurite storage emulator.
246
304
@@ -256,10 +314,106 @@ The code you add creates a `whois` HTTP function endpoint in your existing proje
256
314
257
315
1. After a response is returned, press <kbd>Ctrl + C</kbd> to stop Core Tools.
258
316
259
-
## 10. Deploy to Azure
317
+
## Redeploy to Azure
318
+
319
+
In [the previous article](./create-first-function-azure-developer-cli.md), you deployed your project to a function app in Azure. You can redeploy your updated app the same way by using `azd`, or you can publish to the same app using Visual Studio Code or the [Azure Functions Core Tools](./functions-run-local.md).
320
+
321
+
### [azd](#tab/azd)
322
+
323
+
In an `azd`-based project, rou can run the `azd up` command as many times as you need to both provision your Azure resources and deploy code updates to your function app.
>Deployed code files are always overwritten by the latest deployment package.
341
+
342
+
## Update application settings
343
+
344
+
You need to also update the app settings in your function app to be able to connect to your Azure OpenAI resource and use the correct model. Your function app in Azure needs to have these settings to be able to run:
|`AzureOpenAI__managedIdentityResourceId`| The resource ID of your managed identity. |
351
+
|`AzureOpenAI__clientId`| The client ID of your managed identity. |
352
+
353
+
You can add these settings in one of these ways:
354
+
355
+
### [Azure portal](#tab/azure-portal)
356
+
357
+
1. In your function app page, select **Settings** > **Environment variables** in the left pane.
358
+
359
+
1. In the **App settings** tab, select **+ Add**, and then enter the **Name** and **Value** of the new key-value pair, and select **Apply**.
360
+
361
+
1. Repeat the previous step to add all of the required settings, and then select **Apply**.
362
+
363
+
### [Azure CLI](#azure-cli)
364
+
365
+
The [`az functionapp config appsettings set`](/cli/azure/functionapp/config/appsettings#az-functionapp-config-appsettings-set) command adds or updates application settings in your function app.
|`<OPENAI_RESOURCE_NAME>`| The name of your Azure OpenAI resource |
384
+
|`<USER_RESOURCE_ID>`| The fully-qualified ID of your user-assigned managed identity |
385
+
|`<USER_CLIENT_ID>`| The client ID of your user-assigned managed identity |
386
+
387
+
### [Visual Studio Code](#tab/vs-code)
388
+
389
+
1. In the command palette, enter or search for **Azure Functions: Add New Setting**, select your function app, and follow these prompts:
390
+
391
+
| Prompt | Value |
392
+
| ----- | ----- |
393
+
|`Enter a new app setting name`| The key name of your application setting |
394
+
|`Enter a value for setting`| The value of the setting |
395
+
396
+
1. Repeat the previous step to add all of the required settings.
397
+
398
+
---
399
+
400
+
## Verify in Azure
401
+
402
+
You can run the `whois` function in Azure directly from Visual Studio Code:
403
+
404
+
1. In the command palette, enter **Azure Functions: Execute function now**, and select your Azure subscription.
405
+
406
+
1. From the list, choose your function app in Azure. If you don't see your function app, make sure you're signed in to the correct subscription.
407
+
408
+
Send a GET request to the `whois` endpoint function, with a name in the path as before. This time, you must use the URL of your function app in Azure, which looks like this URL:
409
+
410
+
`http://localhost:7071/api/whois/<NAME>`
411
+
412
+
Replace the `<NAME>` string with the value you want passed to the `"Who is {name}?"` prompt. The `<NAME>` must be the URL-encoded name of a public figure, like `Abraham%20Lincoln`.
413
+
414
+
The response you see is the text completion response from your Azure OpenAI model.
261
415
262
-
## 11. Clean up resources
416
+
## Clean up resources
263
417
264
418
In Azure, *resources* refer to function apps, functions, storage accounts, and so forth. They're grouped into *resource groups*, and you can delete everything in a group by deleting the group.
::: zone pivot="programming-language-csharp,programming-language-javascript,programming-language-powershell,programming-language-python,programming-language-typescript"
373
-
To publish your local code to a function app in Azure, use the [`func azure functionapp publish`](./functions-core-tools-reference.md#func-azure-functionapp-publish) command, as in the following example:
374
-
375
-
```
376
-
func azure functionapp publish <FunctionAppName>
377
-
```
378
-
379
-
This command publishes project files from the current directory to the `<FunctionAppName>` as a .zip deployment package. If the project requires compilation, it's done remotely during deployment.
380
-
::: zone-end
381
-
::: zone pivot="programming-language-java"
382
-
Java uses Maven to publish your local project to Azure instead of Core Tools. Use the following Maven command to publish your project to Azure:
383
-
384
-
```
385
-
mvn azure-functions:deploy
386
-
```
387
-
388
-
When you run this command, Azure resources are created during the initial deployment based on the settings in your _pom.xml_ file. For more information, see [Deploy the function project to Azure](create-first-function-cli-java.md#deploy-the-function-project-to-azure).
::: zone pivot="programming-language-csharp,programming-language-javascript,programming-language-powershell,programming-language-python,programming-language-typescript"
391
375
The following considerations apply to this kind of deployment:
0 commit comments