|
| 1 | +--- |
| 2 | +title: 'Tutorial: Connect to OpenAI from Azure Functions in Visual Studio Code' |
| 3 | +description: Learn how to connect Azure Functions to OpenAI by adding an output binding to your Visual Studio Code project. |
| 4 | +ms.date: 5/22/2024 |
| 5 | +ms.topic: tutorial |
| 6 | +author: dbandaru |
| 7 | +ms.author: dbandaru |
| 8 | +zone_pivot_groups: programming-languages-set-functions |
| 9 | +#customer intent: As an Azure developer, I want learn how to integrate Azure OpenAI capabilities in my function code to leverage AI benefits in my colud-based code executions. |
| 10 | +--- |
| 11 | + |
| 12 | +# Tutorial: Connect to OpenAI from Azure Functions using Visual Studio Code |
| 13 | + |
| 14 | +This article shows you how to use Visual Studio Code to connect Azure OpenAi to the function you created in the previous quickstart article. The output binding that you add to this HTTP trigger function {{does somethign cool}}. |
| 15 | + |
| 16 | +[!INCLUDE [functions-add-storage-binding-intro](../../includes/functions-add-storage-binding-intro.md)] |
| 17 | + |
| 18 | +> [!div class="checklist"] |
| 19 | +> * Create resources in Azure OpenAI. |
| 20 | +> * Deploy a model in OpenAI the resource. |
| 21 | +> * Set access permissions to the model resource. |
| 22 | +> * Enable your function app to connect to OpenAI. |
| 23 | +> * Add OpenAI bindings to your HTTP triggered function. |
| 24 | +
|
| 25 | +## Prerequisites |
| 26 | +:::zone pivot="programming-language-csharp" |
| 27 | +* Complete the steps in [part 1 of the Visual Studio Code quickstart](create-first-function-vs-code-csharp.md). |
| 28 | +:::zone-end |
| 29 | +:::zone pivot="programming-language-java" |
| 30 | +* Complete the steps in [part 1 of the Visual Studio Code quickstart](create-first-function-vs-code-java.md). |
| 31 | +:::zone-end |
| 32 | +:::zone pivot="programming-language-javascript" |
| 33 | +* Complete the steps in [part 1 of the Visual Studio Code quickstart](create-first-function-vs-code-javascript.md). |
| 34 | +:::zone-end |
| 35 | +:::zone pivot="programming-language-typescript" |
| 36 | +* Complete the steps in [part 1 of the Visual Studio Code quickstart](create-first-function-vs-code-typescript.md). |
| 37 | +:::zone-end |
| 38 | +:::zone pivot="programming-language-python" |
| 39 | +* Complete the steps in [part 1 of the Visual Studio Code quickstart](create-first-function-vs-code-python.md). |
| 40 | +:::zone-end |
| 41 | +:::zone pivot="programming-language-powershell" |
| 42 | +* Complete the steps in [part 1 of the Visual Studio Code quickstart](create-first-function-vs-code-powershell.md). |
| 43 | +:::zone-end |
| 44 | +* Obtain access to Azure OpenAI in your Azure subscription. If you haven't already been granted access, complete [this form](https://aka.ms/oai/access) to request access. |
| 45 | +:::zone pivot="programming-language-csharp" |
| 46 | +* Install [.NET Core CLI tools](/dotnet/core/tools/?tabs=netcore2x). |
| 47 | +:::zone-end |
| 48 | +## 1. Create your Azure OpenAI resources |
| 49 | + |
| 50 | +The following steps show how to create an Azure OpenAI data model in the Azure portal. |
| 51 | + |
| 52 | +1. Sign in with your Azure subscription in the Azure portal. |
| 53 | + |
| 54 | +1. Select **Create a resource** and search for the **Azure OpenAI**. When you locate the service, select **Create**. |
| 55 | + |
| 56 | +1. On the **Create Azure OpenAI** page, provide the following information for the fields on the **Basics** tab: |
| 57 | + |
| 58 | + | Field | Description | |
| 59 | + |---|---| |
| 60 | + | **Subscription** | Your subscription, which has been onboarded to use Azure OpenAI. | |
| 61 | + | **Resource group** | The resource group you created for the function app in the previous article. | |
| 62 | + | **Region** | Ideally, the same location as the function app. | |
| 63 | + | **Name** | A descriptive name for your Azure OpenAI Service resource, such as _mySampleOpenAI_. | |
| 64 | + | **Pricing Tier** | The pricing tier for the resource. Currently, only the Standard tier is available for the Azure OpenAI Service. For more info on pricing visit the [Azure OpenAI pricing page](https://azure.microsoft.com/pricing/details/cognitive-services/openai-service/) | |
| 65 | + |
| 66 | + :::image type="content" source="../ai-services/openai/media/create-resource/create-resource-basic-settings.png" alt-text="Screenshot that shows how to configure an Azure OpenAI resource in the Azure portal."::: |
| 67 | + |
| 68 | +1. Select **Next** twice to accept the default values for both the **Network** and **Tags** tabs. The service you create does't have any network restrictions, including from the internet. |
| 69 | + |
| 70 | +1. Select **Next** a final time to move to the final stage in the process: **Review + submit**. |
| 71 | + |
| 72 | +1. Confirm your configuration settings, and select **Create**. |
| 73 | + |
| 74 | +The Azure portal displays a notification when the new resource is available. |
| 75 | + |
| 76 | +## 2. Deploy a model |
| 77 | + |
| 78 | +Now you can deploy a model. You can select from one of several available models in Azure OpenAI Studio. |
| 79 | + |
| 80 | +To deploy a model, follow these steps: |
| 81 | + |
| 82 | +1. Sign in to [Azure OpenAI Studio](https://oai.azure.com). |
| 83 | + |
| 84 | +1. Choose the subscription and the Azure OpenAI resource you just created, and select **Use resource**. |
| 85 | + |
| 86 | +1. Under **Management** select **Deployments**. |
| 87 | + |
| 88 | +1. Select **Create new deployment** and configure the following fields: |
| 89 | + |
| 90 | + | Field | Description | |
| 91 | + |---|---| |
| 92 | + | **Select a model** | Model availability varies by region. For a list of available models per region, see [Model summary table and region availability](../concepts/models.md#model-summary-table-and-region-availability). | |
| 93 | + | **Deployment name** | Choose a name carefully. The deployment name is used in your code to call the model by using the client libraries and the REST APIs. | |
| 94 | + |
| 95 | + > [!IMPORTANT] |
| 96 | + > 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. |
| 97 | +
|
| 98 | +1. Leave the **Advanced options** set to the defaults, and select **Create**. |
| 99 | + |
| 100 | + The deployments table shows a new entry that corresponds to your newly created model. |
| 101 | + |
| 102 | +1. AFter the deployment completes, navigate to the Azure OpenAI resource blade in the Azure portal, and, under **Essentials**, select **Click here to view endpoints**. Copy the **endpoint** URL and the **keys**. Save these values, you'll need them later. |
| 103 | + |
| 104 | +## 3. Update application settings |
| 105 | + |
| 106 | +Now that you have the credentials to connect to your model in Azure OpenAI, you need to set these access credentials in application settings. |
| 107 | + |
| 108 | +1. In Visual Studio Code, open the code project you created when you completed the [previous article](./create-first-function-vs-code-csharp.md) and add these values to the local.settings.json file: |
| 109 | + |
| 110 | + |
| 111 | + + **`AZURE_OPENAI_ENDPOINT`**: required by the binding extension. Set this value to the endpoint of the Azure OpenAI resource you created earlier. |
| 112 | + + **`AZURE_OPENAI_KEY`**: required by the binding extension. Set this value to the key for the Azure OpenAI resource. |
| 113 | + + **`CHAT_MODEL_DEPLOYMENT_NAME`**: used to define the input binding. Set this value to `gpt-3.5-turbo`, unless you used a different name for your model deployment. |
| 114 | + |
| 115 | +1. Save the file. When you deploy to Azure, you must also add these settings to your function app. |
| 116 | + |
| 117 | +:::zone pivot="programming-language-csharp" |
| 118 | +## 4. Register binding extensions |
| 119 | + |
| 120 | +Because you're using an Azure OpenAI output binding, you must have the corresponding bindings extension installed before you run the project. |
| 121 | + |
| 122 | +With the exception of HTTP and timer triggers, bindings are implemented as extension packages. Run this [dotnet add package](/dotnet/core/tools/dotnet-add-package) command in the Terminal window to add the Azure OpenAI extension package to your project: |
| 123 | + |
| 124 | +```bash |
| 125 | +dotnet add package Microsoft.Azure.Functions.Worker.Extensions.OpenAI --prerelease |
| 126 | +``` |
| 127 | +:::zone-end |
| 128 | +:::zone pivot="programming-language-java,programming-language-javascript,programming-language-typescript,programming-language-python,programming-language-powershell" |
| 129 | +<!---NOTE: Update this after preview to `## Verify the extension bundle`--> |
| 130 | +## 4. Update the extension bundle |
| 131 | + |
| 132 | +To access the preview Azure OpenAI bindings, you must use a preview version of the extension bundle that contains this extension. |
| 133 | + |
| 134 | +Replace the contents of your current `host.json` file with this JSON: |
| 135 | + |
| 136 | +```json |
| 137 | +{ |
| 138 | + "version": "2.0", |
| 139 | + "extensionBundle": { |
| 140 | + "id": "Microsoft.Azure.Functions.ExtensionBundle.Preview", |
| 141 | + "version": "[4.*, 5.0.0)" |
| 142 | + } |
| 143 | +} |
| 144 | +``` |
| 145 | +:::zone-end |
| 146 | +Now, you can use the Azure OpenAI output binding to your project. |
| 147 | + |
| 148 | +## 5. Return text completion from the model |
| 149 | + |
| 150 | +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. |
| 151 | +:::zone pivot="programming-language-csharp" |
| 152 | +1. In your existing `HttpExample` class file, add this `using` statement: |
| 153 | + |
| 154 | + :::code language="csharp" source="~/functions-openai-extension/samples/textcompletion/csharp-ooproc/TextCompletions.cs" range="5" ::: |
| 155 | + |
| 156 | +1. In the same file, add this code that defines a new HTTP trigger endpoint named `whois`: |
| 157 | + |
| 158 | + ```csharp |
| 159 | + [Function(nameof(WhoIs))] |
| 160 | + public IActionResult WhoIs([HttpTrigger(AuthorizationLevel.Function, Route = "whois/{name}")] HttpRequest req, |
| 161 | + [TextCompletionInput("Who is {name}?", Model = "%CHAT_MODEL_DEPLOYMENT_NAME%")] TextCompletionResponse response) |
| 162 | + { |
| 163 | + if(!String.IsNullOrEmpty(response.Content)) |
| 164 | + { |
| 165 | + return new OkObjectResult(response.Content); |
| 166 | + } |
| 167 | + else |
| 168 | + { |
| 169 | + return new NotFoundObjectResult("Something went wrong."); |
| 170 | + } |
| 171 | + } |
| 172 | + ``` |
| 173 | + |
| 174 | +:::zone-end |
| 175 | +:::zone pivot="programming-language-java" |
| 176 | +1. Update the `pom.xml` project file to add this reference to the `properties` collection: |
| 177 | + |
| 178 | + :::code language="xml" source="~/functions-openai-extension/samples/textcompletion/java/pom.xml" range="18" ::: |
| 179 | + |
| 180 | +1. In the same file, add this dependency to the `dependencies` collection: |
| 181 | + |
| 182 | + :::code language="xml" source="~/functions-openai-extension/samples/textcompletion/java/pom.xml" range="29-33" ::: |
| 183 | + |
| 184 | +1. In the existing `Function.java` project file, add these `import` statements: |
| 185 | + |
| 186 | + :::code language="java" source="~/functions-openai-extension/samples/textcompletion/java/src/main/java/com/azfs/TextCompletions.java" range="19-20" ::: |
| 187 | + |
| 188 | +1. In the same file, add this code that defines a new HTTP trigger endpoint named `whois`: |
| 189 | + |
| 190 | + :::code language="java" source="~/functions-openai-extension/samples/textcompletion/java/src/main/java/com/azfs/TextCompletions.java" range="31-46" ::: |
| 191 | + |
| 192 | +:::zone-end |
| 193 | +:::zone pivot="programming-language-javascript" |
| 194 | +1. In Visual Studio Code, Press F1 and in the command palette type `Azure Functions: Create Function...`, select **HTTP trigger**, type the function name `whois`, select **Anonymous**, and press Enter. |
| 195 | + |
| 196 | +1. In the new `whois.js` code file, replace the contents of the file with this code: |
| 197 | + |
| 198 | + ```javascript |
| 199 | + const { app } = require('@azure/functions'); |
| 200 | + |
| 201 | + const openAICompletionInput = app.generic({ |
| 202 | + prompt: 'Who is {name}?', |
| 203 | + maxTokens: '100', |
| 204 | + type: 'textCompletion', |
| 205 | + model: '%CHAT_MODEL_DEPLOYMENT_NAME%' |
| 206 | + }); |
| 207 | + app.http('whois', { |
| 208 | + methods: ['GET'], |
| 209 | + route: 'whois/{name}', |
| 210 | + authLevel: 'anonymous', |
| 211 | + extraInputs: [openAICompletionInput], |
| 212 | + handler: async (request, context) => { |
| 213 | + var response = context.extraInputs.get(openAICompletionInput); |
| 214 | + return { body: response.content.trim() }; |
| 215 | + } |
| 216 | + }); |
| 217 | + ``` |
| 218 | + |
| 219 | +:::zone-end |
| 220 | +:::zone pivot="programming-language-typescript" |
| 221 | +1. In Visual Studio Code, Press F1 and in the command palette type `Azure Functions: Create Function...`, select **HTTP trigger**, type the function name `whois`, select **Anonymous**, and press Enter. |
| 222 | + |
| 223 | +1. In the new `whois.ts` code file, replace the contents of the file with this code: |
| 224 | + |
| 225 | + :::code language="typescript" source="~/functions-openai-extension/samples/textcompletion/nodejs/src/functions/whois.ts" ::: |
| 226 | + |
| 227 | +:::zone-end |
| 228 | +:::zone pivot="programming-language-python" |
| 229 | +1. In the existing `function_app.py` project file, add this `import` statement: |
| 230 | + |
| 231 | + :::code language="python" source="~/functions-openai-extension/samples/textcompletion/python/function_app.py" range="1" ::: |
| 232 | + |
| 233 | +1. In the same file, add this code that defines a new HTTP trigger endpoint named `whois`: |
| 234 | + :::code language="python" source="~/functions-openai-extension/samples/textcompletion/python/function_app.py" range="7-18" ::: |
| 235 | + |
| 236 | +:::zone-end |
| 237 | +:::zone pivot="programming-language-powershell" |
| 238 | +1. In Visual Studio Code, Press F1 and in the command palette type `Azure Functions: Create Function...`, select **HTTP trigger**, type the function name `whois`, select **Anonymous**, and press Enter. |
| 239 | + |
| 240 | +1. Open the new `whois/function.json` code file and replace its contents with this code, which adds a definition for the `TextCompletionResponse` input binding: |
| 241 | + |
| 242 | + :::code language="json" source="~/functions-openai-extension/samples/textcompletion/powershell/WhoIs/function.json" ::: |
| 243 | + |
| 244 | +1. Replace the content of the `whois/run.ps1` code file a with this code, which returns the input binding response: |
| 245 | + |
| 246 | + :::code language="powershell" source="~/functions-openai-extension/samples/textcompletion/powershell/WhoIs/run.ps1" ::: |
| 247 | + |
| 248 | +:::zone-end |
| 249 | + |
| 250 | +## 6. Run the function |
| 251 | + |
| 252 | +<!--- 1. Install and start Azurite for local development storage. For instructions on how to work with Azurite: https://learn.microsoft.com/azure/storage/common/storage-use-azurite |
| 253 | +--> |
| 254 | + |
| 255 | +1. As in the previous article, press <kbd>F5</kbd> to start the function app project and Core Tools. |
| 256 | + |
| 257 | +1. With the Core Tools running, send a GET request to the `whois` endpoint function, with a name in the path, like this URL: |
| 258 | + |
| 259 | + `http://localhost:7071/api/whois/<NAME>` |
| 260 | + |
| 261 | + Replace the `<NAME>` string with the value you want passed to the `"Who is {name}?"` prompt. |
| 262 | + |
| 263 | + The response you see is the text completion response from your Azure OpenAI model. |
| 264 | + |
| 265 | +1. After a response is returned, press <kbd>Ctrl + C</kbd> to stop Core Tools. |
| 266 | + |
| 267 | +<!-- enable managed identities |
| 268 | +## 8. Set access permissions |
| 269 | +{{move this info into the main article}} |
| 270 | +[create Azure OpenAI resources and to deploy models](../ai-services/openai/how-to/role-based-access-control.md). |
| 271 | + |
| 272 | +## 9. Deploy to Azure |
| 273 | +--> |
| 274 | + |
| 275 | +## Clean up resources |
| 276 | + |
| 277 | +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. |
| 278 | + |
| 279 | +You created resources to complete these quickstarts. You may be billed for these resources, depending on your [account status](https://azure.microsoft.com/account/) and [service pricing](https://azure.microsoft.com/pricing/). If you don't need the resources anymore, here's how to delete them: |
| 280 | +
|
| 281 | +[!INCLUDE [functions-cleanup-resources-vs-code-inner.md](../../includes/functions-cleanup-resources-vs-code-inner.md)] |
| 282 | + |
| 283 | +## Next steps |
| 284 | + |
| 285 | +You've created your text completion with Azure Functions and Azure OpenAI. Now you can learn more about developing Functions using the OpenAI Binding in the reference section. |
| 286 | + |
| 287 | + |
0 commit comments