Skip to content

Commit d4f2d0f

Browse files
authored
Add azure embeddings example notebook (#104)
* Remove invalid inheritances from Embedding and Completion - Remove ListableAPIResource inheritance - Remove DeletableAPIResource * Add embeddings example notebook * bump version
1 parent 0e37b49 commit d4f2d0f

File tree

3 files changed

+196
-2
lines changed

3 files changed

+196
-2
lines changed

examples/azure/embeddings.ipynb

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Azure embeddings example\n",
8+
"In this example we'll try to go over all operations for embeddings that can be done using the Azure endpoints. \\\n",
9+
"This example focuses on finetuning but touches on the majority of 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+
"In the following section the endpoint and key need to be set up of the next sections to work. \\\n",
28+
"Please go to https://portal.azure.com, find your resource and then under \"Resource Management\" -> \"Keys and Endpoints\" look for the \"Endpoint\" value and one of the Keys. They will act as api_base and api_key in the code below."
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": null,
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"openai.api_key = '' # Please add your api key here\n",
38+
"openai.api_base = '' # Please add your endpoint here\n",
39+
"\n",
40+
"openai.api_type = 'azure'\n",
41+
"openai.api_version = '2022-03-01-preview' # this may change in the future"
42+
]
43+
},
44+
{
45+
"cell_type": "markdown",
46+
"metadata": {},
47+
"source": [
48+
"## Deployments\n",
49+
"In this section we are going to create a deployment using the finetune model that we just adapted and then used the deployment to create a simple completion operation."
50+
]
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"### Deployments: Create Manually\n",
57+
"Let's create a deployment using the text-similarity-curie-001 engine. You can create a new deployment by going to your Resource in your portal under \"Resource Management\" -> \"Deployments\"."
58+
]
59+
},
60+
{
61+
"cell_type": "markdown",
62+
"metadata": {},
63+
"source": [
64+
"### (Optional) Deployments: Create Programatically\n",
65+
"We can also create a deployment using code:"
66+
]
67+
},
68+
{
69+
"cell_type": "code",
70+
"execution_count": null,
71+
"metadata": {},
72+
"outputs": [],
73+
"source": [
74+
"model = \"text-similarity-curie-001\"\n",
75+
"\n",
76+
"# Now let's create the deployment\n",
77+
"print(f'Creating a new deployment with model: {model}')\n",
78+
"result = openai.Deployment.create(model=model, scale_settings={\"scale_type\":\"manual\", \"capacity\": 1})\n",
79+
"deployment_id = result[\"id\"]"
80+
]
81+
},
82+
{
83+
"cell_type": "markdown",
84+
"metadata": {},
85+
"source": [
86+
"### (Optional) Deployments: Retrieving\n",
87+
"Now let's check the status of the newly created deployment"
88+
]
89+
},
90+
{
91+
"cell_type": "code",
92+
"execution_count": null,
93+
"metadata": {},
94+
"outputs": [],
95+
"source": [
96+
"print(f'Checking for deployment status.')\n",
97+
"resp = openai.Deployment.retrieve(id=deployment_id)\n",
98+
"status = resp[\"status\"]\n",
99+
"print(f'Deployment {deployment_id} is with status: {status}')"
100+
]
101+
},
102+
{
103+
"cell_type": "markdown",
104+
"metadata": {},
105+
"source": [
106+
"### Deployments: Listing\n",
107+
"Now because creating a new deployment takes a long time, let's look in the subscription for an already finished deployment that succeeded."
108+
]
109+
},
110+
{
111+
"cell_type": "code",
112+
"execution_count": null,
113+
"metadata": {},
114+
"outputs": [],
115+
"source": [
116+
"print('While deployment running, selecting a completed one.')\n",
117+
"deployment_id = None\n",
118+
"result = openai.Deployment.list()\n",
119+
"for deployment in result.data:\n",
120+
" if deployment[\"status\"] == \"succeeded\":\n",
121+
" deployment_id = deployment[\"id\"]\n",
122+
" break\n",
123+
"\n",
124+
"if not deployment_id:\n",
125+
" print('No deployment with status: succeeded found.')\n",
126+
"else:\n",
127+
" print(f'Found a successful deployment with id: {deployment_id}.')"
128+
]
129+
},
130+
{
131+
"cell_type": "markdown",
132+
"metadata": {},
133+
"source": [
134+
"### Embeddings\n",
135+
"Now let's send a sample embedding to the deployment."
136+
]
137+
},
138+
{
139+
"cell_type": "code",
140+
"execution_count": null,
141+
"metadata": {},
142+
"outputs": [],
143+
"source": [
144+
"embeddings = openai.Embedding.create(engine=deployment_id,\n",
145+
" input=\"The food was delicious and the waiter...\")\n",
146+
" \n",
147+
"print(embeddings)"
148+
]
149+
},
150+
{
151+
"cell_type": "markdown",
152+
"metadata": {},
153+
"source": [
154+
"### (Optional) Deployments: Delete\n",
155+
"Finally let's delete the deployment"
156+
]
157+
},
158+
{
159+
"cell_type": "code",
160+
"execution_count": null,
161+
"metadata": {},
162+
"outputs": [],
163+
"source": [
164+
"print(f'Deleting deployment: {deployment_id}')\n",
165+
"openai.Deployment.delete(sid=deployment_id)"
166+
]
167+
}
168+
],
169+
"metadata": {
170+
"interpreter": {
171+
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
172+
},
173+
"kernelspec": {
174+
"display_name": "Python 3.8.10 64-bit",
175+
"language": "python",
176+
"name": "python3"
177+
},
178+
"language_info": {
179+
"codemirror_mode": {
180+
"name": "ipython",
181+
"version": 3
182+
},
183+
"file_extension": ".py",
184+
"mimetype": "text/x-python",
185+
"name": "python",
186+
"nbconvert_exporter": "python",
187+
"pygments_lexer": "ipython3",
188+
"version": "3.8.10"
189+
},
190+
"orig_nbformat": 4
191+
},
192+
"nbformat": 4,
193+
"nbformat_minor": 2
194+
}

openai/api_resources/completion.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from openai.error import InvalidRequestError, TryAgain
77

88

9-
class Completion(EngineAPIResource, ListableAPIResource, DeletableAPIResource):
9+
class Completion(EngineAPIResource):
1010
engine_required = False
1111
OBJECT_NAME = "completions"
1212

openai/api_resources/embedding.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from openai.error import InvalidRequestError, TryAgain
1010

1111

12-
class Embedding(EngineAPIResource, ListableAPIResource, DeletableAPIResource):
12+
class Embedding(EngineAPIResource):
1313
engine_required = False
1414
OBJECT_NAME = "embeddings"
1515

0 commit comments

Comments
 (0)