Integrate Azure OpenAI for Code Generation
An archived guide to setting up Azure OpenAI completions with gpt-3.5-turbo-instruct, superseded by a newer library.
Maintainer of this project? Claim this page to edit the listing.
1.0.0Add to Favorites
Why it matters
Leverage Azure OpenAI service to generate code completions and integrate it into your development workflow. This asset provides a foundational example for setting up Azure OpenAI with Python.
Outcomes
What it gets done
Configure Azure OpenAI API credentials and endpoints.
Set up the OpenAI Python library for Azure integration.
Create and manage model deployments on Azure.
Generate code completions using the Azure OpenAI service.
Install
Add it to your toolbox
Run in your project directory:
curl -fsSL https://spark.entire.vc/get/oai-completions | bash Overview
Azure completions example
An archived Azure OpenAI cookbook covering completions setup with gpt-3.5-turbo-instruct, including portal API key and Active Directory authentication and manual model deployment. Use it as historical reference for the older Azure OpenAI completions setup. This example is archived and superseded by a newer version of the openai library.
What it does
This archived example (the notebook itself notes a newer version of the openai library is available) walks through setting up the Azure OpenAI service to generate text completions using the gpt-3.5-turbo-instruct model. It covers locating your api_base (the resource's Endpoint value in the Azure Portal) and api_version, choosing between azure or azure_ad as the api_type, creating a model deployment, and sending a sample completion request.
When to use - and when NOT to
Use this as historical reference for the older Azure OpenAI completions setup flow rather than a current recommendation - check the newer openai library's own documentation for the up-to-date approach before building anything new on this pattern. The two authentication paths covered are a plain API key from the Azure Portal, or Microsoft Active Directory authentication, which issues a token that expires and needs refreshing via a hook into requests.auth.
Inputs and outputs
Connection settings can be set directly in code or, as recommended for development, via environment variables: OPENAI_API_BASE, OPENAI_API_KEY, OPENAI_API_TYPE, and OPENAI_API_VERSION. The deployment itself is created manually in the Azure Portal under Resource Management -> Model deployments, selecting gpt-3.5-turbo-instruct as the model before it can be used to generate completions.
Integrations
Both authentication paths - portal API key and Active Directory token - ultimately configure the same underlying api_type/api_key setup used to call the deployed model; Active Directory is the path to prefer if token expiry and refresh handling fit your infrastructure better than a static key.
Who it's for
Developers maintaining older Azure OpenAI integrations built against gpt-3.5-turbo-instruct completions who need to understand the legacy authentication and deployment setup this archived example documents, before deciding whether to migrate to the current library and API version instead.
Source README
Azure completions example
Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742
This example will cover completions using the Azure OpenAI service.
Setup
First, we install the necessary dependencies.
For the following sections to work properly we first have to setup some things. Let's start with the api_base and api_version. To find your api_base go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value.
We next have to setup the api_type and api_key. We can either get the key from the portal or we can get it through Microsoft Active Directory Authentication. Depending on this the api_type is either azure or azure_ad.
Setup: Portal
Let's first look at getting the key from the portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values.
Note: In this example, we configured the library to use the Azure API by setting the variables in code. For development, consider setting the environment variables instead:
OPENAI_API_BASE
OPENAI_API_KEY
OPENAI_API_TYPE
OPENAI_API_VERSION
(Optional) Setup: Microsoft Active Directory Authentication
Let's now see how we can get a key via Microsoft Active Directory Authentication. Uncomment the following code if you want to use Active Directory Authentication instead of keys from the portal.
A token is valid for a period of time, after which it will expire. To ensure a valid token is sent with every request, you can refresh an expiring token by hooking into requests.auth:
Deployments
In this section we are going to create a deployment using the gpt-3.5-turbo-instruct model that we can then use to create completions.
Deployments: Create manually
Create a new deployment by going to your Resource in your portal under "Resource Management" -> "Model deployments". Select gpt-3.5-turbo-instruct as the model.
Completions
Now let's send a sample completion to the deployment.
FAQ
Common questions
Discussion
Questions & comments ยท 0
Sign In Sign in to leave a comment.