Chat

chatgptopenaigpt-4examplesarchiveazureopenai-apiopenai-cookbook

Azure chat completions example (preview)

Note: There is a newer version of the openai library available. See https://github.com/openai/openai-python/discussions/742

This example will cover chat 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-35-turbo model that we can then use to create chat completions.

Deployments: Create manually

Let's create a deployment using the gpt-35-turbo model. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Model deployments" create a new gpt-35-turbo deployment.

[ ]

Create chat completion

Now let's send a sample chat completion to the deployment.

[ ]

We can also stream the response.

[ ]