|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "attachments": {}, |
| 5 | + "cell_type": "markdown", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Azure chat completions example (preview)\n", |
| 9 | + "In this example we'll try to go over all operations needed to get chat completions working using the Azure endpoints. \\\n", |
| 10 | + "This example focuses on chat completions but also touches on some other operations that are also available using the API. This example is meant to be a quick way of showing simple operations and is not meant as a tutorial." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": null, |
| 16 | + "metadata": {}, |
| 17 | + "outputs": [], |
| 18 | + "source": [ |
| 19 | + "import openai" |
| 20 | + ] |
| 21 | + }, |
| 22 | + { |
| 23 | + "attachments": {}, |
| 24 | + "cell_type": "markdown", |
| 25 | + "metadata": {}, |
| 26 | + "source": [ |
| 27 | + "## Setup\n", |
| 28 | + "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." |
| 29 | + ] |
| 30 | + }, |
| 31 | + { |
| 32 | + "cell_type": "code", |
| 33 | + "execution_count": null, |
| 34 | + "metadata": {}, |
| 35 | + "outputs": [], |
| 36 | + "source": [ |
| 37 | + "openai.api_version = '2023-03-15-preview'\n", |
| 38 | + "openai.api_base = '' # Please add your endpoint here" |
| 39 | + ] |
| 40 | + }, |
| 41 | + { |
| 42 | + "cell_type": "markdown", |
| 43 | + "metadata": {}, |
| 44 | + "source": [ |
| 45 | + "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`." |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "attachments": {}, |
| 50 | + "cell_type": "markdown", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "### Setup: Portal\n", |
| 54 | + "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." |
| 55 | + ] |
| 56 | + }, |
| 57 | + { |
| 58 | + "cell_type": "code", |
| 59 | + "execution_count": null, |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "openai.api_type = 'azure'\n", |
| 64 | + "openai.api_key = '' # Please add your api key here" |
| 65 | + ] |
| 66 | + }, |
| 67 | + { |
| 68 | + "attachments": {}, |
| 69 | + "cell_type": "markdown", |
| 70 | + "metadata": {}, |
| 71 | + "source": [ |
| 72 | + "### (Optional) Setup: Microsoft Active Directory Authentication\n", |
| 73 | + "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." |
| 74 | + ] |
| 75 | + }, |
| 76 | + { |
| 77 | + "cell_type": "code", |
| 78 | + "execution_count": null, |
| 79 | + "metadata": {}, |
| 80 | + "outputs": [], |
| 81 | + "source": [ |
| 82 | + "# from azure.identity import DefaultAzureCredential\n", |
| 83 | + "\n", |
| 84 | + "# default_credential = DefaultAzureCredential()\n", |
| 85 | + "# token = default_credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n", |
| 86 | + "\n", |
| 87 | + "# openai.api_type = 'azure_ad'\n", |
| 88 | + "# openai.api_key = token.token" |
| 89 | + ] |
| 90 | + }, |
| 91 | + { |
| 92 | + "attachments": {}, |
| 93 | + "cell_type": "markdown", |
| 94 | + "metadata": {}, |
| 95 | + "source": [ |
| 96 | + "## Deployments\n", |
| 97 | + "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." |
| 98 | + ] |
| 99 | + }, |
| 100 | + { |
| 101 | + "attachments": {}, |
| 102 | + "cell_type": "markdown", |
| 103 | + "metadata": {}, |
| 104 | + "source": [ |
| 105 | + "### Deployments: Create manually\n", |
| 106 | + "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. " |
| 107 | + ] |
| 108 | + }, |
| 109 | + { |
| 110 | + "cell_type": "code", |
| 111 | + "execution_count": null, |
| 112 | + "metadata": {}, |
| 113 | + "outputs": [], |
| 114 | + "source": [ |
| 115 | + "deployment_id = \"\" # Fill in the deployment id from the portal here" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "attachments": {}, |
| 120 | + "cell_type": "markdown", |
| 121 | + "metadata": {}, |
| 122 | + "source": [ |
| 123 | + "### (Optional) Deployments: Create programatically\n", |
| 124 | + "We can also create the deployment using code. Note that you can only create one deployment per model." |
| 125 | + ] |
| 126 | + }, |
| 127 | + { |
| 128 | + "cell_type": "code", |
| 129 | + "execution_count": null, |
| 130 | + "metadata": {}, |
| 131 | + "outputs": [], |
| 132 | + "source": [ |
| 133 | + "model = \"gpt-35-turbo\"\n", |
| 134 | + "\n", |
| 135 | + "# Now let's create the deployment\n", |
| 136 | + "print(f'Creating a new deployment with model: {model}')\n", |
| 137 | + "result = openai.Deployment.create(model=model, scale_settings={\"scale_type\":\"standard\"})\n", |
| 138 | + "deployment_id = result[\"id\"]\n", |
| 139 | + "print(f'Successfully created deployment with id: {deployment_id}')" |
| 140 | + ] |
| 141 | + }, |
| 142 | + { |
| 143 | + "cell_type": "markdown", |
| 144 | + "metadata": {}, |
| 145 | + "source": [ |
| 146 | + "### (Optional) Deployments: Wait for deployment to succeed\n", |
| 147 | + "Now let's check the status of the newly created deployment and wait till it is succeeded." |
| 148 | + ] |
| 149 | + }, |
| 150 | + { |
| 151 | + "cell_type": "code", |
| 152 | + "execution_count": null, |
| 153 | + "metadata": {}, |
| 154 | + "outputs": [], |
| 155 | + "source": [ |
| 156 | + "print(f'Checking for deployment status.')\n", |
| 157 | + "resp = openai.Deployment.retrieve(id=deployment_id)\n", |
| 158 | + "status = resp[\"status\"]\n", |
| 159 | + "print(f'Deployment {deployment_id} has status: {status}')\n", |
| 160 | + "while status not in [\"succeeded\", \"failed\"]:\n", |
| 161 | + " resp = openai.Deployment.retrieve(id=deployment_id)\n", |
| 162 | + " status = resp[\"status\"]\n", |
| 163 | + " print(f'Deployment {deployment_id} has status: {status}')" |
| 164 | + ] |
| 165 | + }, |
| 166 | + { |
| 167 | + "attachments": {}, |
| 168 | + "cell_type": "markdown", |
| 169 | + "metadata": {}, |
| 170 | + "source": [ |
| 171 | + "### Create chat completion\n", |
| 172 | + "Now let's send a sample chat completion to the deployment." |
| 173 | + ] |
| 174 | + }, |
| 175 | + { |
| 176 | + "cell_type": "code", |
| 177 | + "execution_count": null, |
| 178 | + "metadata": {}, |
| 179 | + "outputs": [], |
| 180 | + "source": [ |
| 181 | + "# For all possible arguments see https://platform.openai.com/docs/api-reference/chat-completions/create\n", |
| 182 | + "response = openai.ChatCompletion.create(\n", |
| 183 | + " deployment_id=deployment_id,\n", |
| 184 | + " messages=[\n", |
| 185 | + " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", |
| 186 | + " {\"role\": \"user\", \"content\": \"Knock knock.\"},\n", |
| 187 | + " {\"role\": \"assistant\", \"content\": \"Who's there?\"},\n", |
| 188 | + " {\"role\": \"user\", \"content\": \"Orange.\"},\n", |
| 189 | + " ],\n", |
| 190 | + " temperature=0,\n", |
| 191 | + ")\n", |
| 192 | + "\n", |
| 193 | + "print(f\"{response.choices[0].message.role}: {response.choices[0].message.content}\")" |
| 194 | + ] |
| 195 | + }, |
| 196 | + { |
| 197 | + "attachments": {}, |
| 198 | + "cell_type": "markdown", |
| 199 | + "metadata": {}, |
| 200 | + "source": [ |
| 201 | + "We can also stream the response.\n" |
| 202 | + ] |
| 203 | + }, |
| 204 | + { |
| 205 | + "cell_type": "code", |
| 206 | + "execution_count": null, |
| 207 | + "metadata": {}, |
| 208 | + "outputs": [], |
| 209 | + "source": [ |
| 210 | + "response = openai.ChatCompletion.create(\n", |
| 211 | + " deployment_id=deployment_id,\n", |
| 212 | + " messages=[\n", |
| 213 | + " {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n", |
| 214 | + " {\"role\": \"user\", \"content\": \"Knock knock.\"},\n", |
| 215 | + " {\"role\": \"assistant\", \"content\": \"Who's there?\"},\n", |
| 216 | + " {\"role\": \"user\", \"content\": \"Orange.\"},\n", |
| 217 | + " ],\n", |
| 218 | + " temperature=0,\n", |
| 219 | + " stream=True\n", |
| 220 | + ")\n", |
| 221 | + "\n", |
| 222 | + "for chunk in response:\n", |
| 223 | + " delta = chunk.choices[0].delta\n", |
| 224 | + "\n", |
| 225 | + " if \"role\" in delta.keys():\n", |
| 226 | + " print(delta.role + \": \", end=\"\", flush=True)\n", |
| 227 | + " if \"content\" in delta.keys():\n", |
| 228 | + " print(delta.content, end=\"\", flush=True)" |
| 229 | + ] |
| 230 | + }, |
| 231 | + { |
| 232 | + "attachments": {}, |
| 233 | + "cell_type": "markdown", |
| 234 | + "metadata": {}, |
| 235 | + "source": [ |
| 236 | + "### (Optional) Deployments: Delete\n", |
| 237 | + "Finally let's delete the deployment." |
| 238 | + ] |
| 239 | + }, |
| 240 | + { |
| 241 | + "cell_type": "code", |
| 242 | + "execution_count": null, |
| 243 | + "metadata": {}, |
| 244 | + "outputs": [], |
| 245 | + "source": [ |
| 246 | + "print(f'Deleting deployment: {deployment_id}')\n", |
| 247 | + "openai.Deployment.delete(sid=deployment_id)" |
| 248 | + ] |
| 249 | + } |
| 250 | + ], |
| 251 | + "metadata": { |
| 252 | + "kernelspec": { |
| 253 | + "display_name": "Python 3", |
| 254 | + "language": "python", |
| 255 | + "name": "python3" |
| 256 | + }, |
| 257 | + "language_info": { |
| 258 | + "codemirror_mode": { |
| 259 | + "name": "ipython", |
| 260 | + "version": 3 |
| 261 | + }, |
| 262 | + "file_extension": ".py", |
| 263 | + "mimetype": "text/x-python", |
| 264 | + "name": "python", |
| 265 | + "nbconvert_exporter": "python", |
| 266 | + "pygments_lexer": "ipython3", |
| 267 | + "version": "3.8.10" |
| 268 | + }, |
| 269 | + "orig_nbformat": 4 |
| 270 | + }, |
| 271 | + "nbformat": 4, |
| 272 | + "nbformat_minor": 2 |
| 273 | +} |
0 commit comments