Skip to content

Commit 4adea8e

Browse files
Merge pull request openai#50 from cmurtz-msft/azure-openai-ga
Azure OpenAI GA
2 parents f607de5 + 934f4d8 commit 4adea8e

File tree

3 files changed

+355
-55
lines changed

3 files changed

+355
-55
lines changed

examples/azure/completions.ipynb

Lines changed: 227 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,227 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Azure completions example\n",
8+
"In this example we'll try to go over all operations needed to get completions working using the Azure endpoints. \\\n",
9+
"This example focuses on 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."
10+
]
11+
},
12+
{
13+
"cell_type": "code",
14+
"execution_count": null,
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import openai\n",
19+
"from openai import cli"
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"metadata": {},
25+
"source": [
26+
"## Setup\n",
27+
"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."
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": null,
33+
"metadata": {},
34+
"outputs": [],
35+
"source": [
36+
"openai.api_version = '2022-12-01'\n",
37+
"openai.api_base = '' # Please add your endpoint here"
38+
]
39+
},
40+
{
41+
"cell_type": "markdown",
42+
"metadata": {},
43+
"source": [
44+
"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`."
45+
]
46+
},
47+
{
48+
"cell_type": "markdown",
49+
"metadata": {},
50+
"source": [
51+
"### Setup: Portal\n",
52+
"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."
53+
]
54+
},
55+
{
56+
"cell_type": "code",
57+
"execution_count": null,
58+
"metadata": {},
59+
"outputs": [],
60+
"source": [
61+
"openai.api_type = 'azure'\n",
62+
"openai.api_key = '' # Please add your api key here"
63+
]
64+
},
65+
{
66+
"cell_type": "markdown",
67+
"metadata": {},
68+
"source": [
69+
"### (Optional) Setup: Microsoft Active Directory Authentication\n",
70+
"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."
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": null,
76+
"metadata": {},
77+
"outputs": [],
78+
"source": [
79+
"# from azure.identity import DefaultAzureCredential\n",
80+
"\n",
81+
"# default_credential = DefaultAzureCredential()\n",
82+
"# token = default_credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
83+
"\n",
84+
"# openai.api_type = 'azure_ad'\n",
85+
"# openai.api_key = token.token"
86+
]
87+
},
88+
{
89+
"attachments": {},
90+
"cell_type": "markdown",
91+
"metadata": {},
92+
"source": [
93+
"## Deployments\n",
94+
"In this section we are going to create a deployment using the `text-davinci-002` model that we can then use to create completions."
95+
]
96+
},
97+
{
98+
"attachments": {},
99+
"cell_type": "markdown",
100+
"metadata": {},
101+
"source": [
102+
"### Deployments: Create manually\n",
103+
"Create a new deployment by going to your Resource in your portal under \"Resource Management\" -> \"Model deployments\". Select `text-davinci-002` as the model."
104+
]
105+
},
106+
{
107+
"attachments": {},
108+
"cell_type": "markdown",
109+
"metadata": {},
110+
"source": [
111+
"### (Optional) Deployments: Create programatically\n",
112+
"We can also create a deployment using code:"
113+
]
114+
},
115+
{
116+
"cell_type": "code",
117+
"execution_count": null,
118+
"metadata": {},
119+
"outputs": [],
120+
"source": [
121+
"model = \"text-davinci-002\"\n",
122+
"\n",
123+
"# Now let's create the deployment\n",
124+
"print(f'Creating a new deployment with model: {model}')\n",
125+
"result = openai.Deployment.create(model=model, scale_settings={\"scale_type\":\"standard\"})\n",
126+
"deployment_id = result[\"id\"]\n",
127+
"print(f'Successfully created deployment with id: {deployment_id}')"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"### (Optional) Deployments: Wait for deployment to succeed\n",
135+
"Now let's check the status of the newly created deployment and wait till it is succeeded."
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": null,
141+
"metadata": {},
142+
"outputs": [],
143+
"source": [
144+
"print(f'Checking for deployment status.')\n",
145+
"resp = openai.Deployment.retrieve(id=deployment_id)\n",
146+
"status = resp[\"status\"]\n",
147+
"print(f'Deployment {deployment_id} has status: {status}')\n",
148+
"while status not in [\"succeeded\", \"failed\"]:\n",
149+
" resp = openai.Deployment.retrieve(id=deployment_id)\n",
150+
" status = resp[\"status\"]\n",
151+
" print(f'Deployment {deployment_id} has status: {status}')"
152+
]
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"### Completions\n",
159+
"Now let's send a sample completion to the deployment."
160+
]
161+
},
162+
{
163+
"cell_type": "code",
164+
"execution_count": null,
165+
"metadata": {},
166+
"outputs": [],
167+
"source": [
168+
"prompt = \"The food was delicious and the waiter\"\n",
169+
"completion = openai.Completion.create(deployment_id=deployment_id,\n",
170+
" prompt=prompt, stop=\".\", temperature=0)\n",
171+
" \n",
172+
"print(f\"{prompt}{completion['choices'][0]['text']}.\")"
173+
]
174+
},
175+
{
176+
"cell_type": "markdown",
177+
"metadata": {},
178+
"source": [
179+
"### (Optional) Deployments: Delete\n",
180+
"Finally let's delete the deployment"
181+
]
182+
},
183+
{
184+
"cell_type": "code",
185+
"execution_count": null,
186+
"metadata": {},
187+
"outputs": [],
188+
"source": [
189+
"print(f'Deleting deployment: {deployment_id}')\n",
190+
"openai.Deployment.delete(sid=deployment_id)"
191+
]
192+
},
193+
{
194+
"cell_type": "code",
195+
"execution_count": null,
196+
"metadata": {},
197+
"outputs": [],
198+
"source": []
199+
}
200+
],
201+
"metadata": {
202+
"kernelspec": {
203+
"display_name": "Python 3 (ipykernel)",
204+
"language": "python",
205+
"name": "python3"
206+
},
207+
"language_info": {
208+
"codemirror_mode": {
209+
"name": "ipython",
210+
"version": 3
211+
},
212+
"file_extension": ".py",
213+
"mimetype": "text/x-python",
214+
"name": "python",
215+
"nbconvert_exporter": "python",
216+
"pygments_lexer": "ipython3",
217+
"version": "3.10.8"
218+
},
219+
"vscode": {
220+
"interpreter": {
221+
"hash": "3a5103089ab7e7c666b279eeded403fcec76de49a40685dbdfe9f9c78ad97c17"
222+
}
223+
}
224+
},
225+
"nbformat": 4,
226+
"nbformat_minor": 2
227+
}

0 commit comments

Comments
 (0)