Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add azure embeddings example notebook #104

Merged
merged 3 commits into from
Jun 21, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 194 additions & 0 deletions examples/azure/embeddings.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Azure embeddings example\n",
"In this example we'll try to go over all operations for embeddings that can be done using the Azure endpoints. \\\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"from openai import cli"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"In the following section the endpoint and key need to be set up of the next sections to work. \\\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai.api_key = '' # Please add your api key here\n",
"openai.api_base = '' # Please add your endpoint here\n",
"\n",
"openai.api_type = 'azure'\n",
"openai.api_version = '2022-03-01-preview' # this may change in the future"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Deployments\n",
"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."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Deployments: Create Manually\n",
"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\"."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### (Optional) Deployments: Create Programatically\n",
"We can also create a deployment using code:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = \"text-similarity-curie-001\"\n",
"\n",
"# Now let's create the deployment\n",
"print(f'Creating a new deployment with model: {model}')\n",
"result = openai.Deployment.create(model=model, scale_settings={\"scale_type\":\"manual\", \"capacity\": 1})\n",
"deployment_id = result[\"id\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### (Optional) Deployments: Retrieving\n",
"Now let's check the status of the newly created deployment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'Checking for deployment status.')\n",
"resp = openai.Deployment.retrieve(id=deployment_id)\n",
"status = resp[\"status\"]\n",
"print(f'Deployment {deployment_id} is with status: {status}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Deployments: Listing\n",
"Now because creating a new deployment takes a long time, let's look in the subscription for an already finished deployment that succeeded."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('While deployment running, selecting a completed one.')\n",
"deployment_id = None\n",
"result = openai.Deployment.list()\n",
"for deployment in result.data:\n",
" if deployment[\"status\"] == \"succeeded\":\n",
" deployment_id = deployment[\"id\"]\n",
" break\n",
"\n",
"if not deployment_id:\n",
" print('No deployment with status: succeeded found.')\n",
"else:\n",
" print(f'Found a successful deployment with id: {deployment_id}.')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Embeddings\n",
"Now let's send a sample embedding to the deployment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"embeddings = openai.Embedding.create(engine=deployment_id,\n",
" input=\"The food was delicious and the waiter...\")\n",
" \n",
"print(embeddings)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### (Optional) Deployments: Delete\n",
"Finally let's delete the deployment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f'Deleting deployment: {deployment_id}')\n",
"openai.Deployment.delete(sid=deployment_id)"
]
}
],
"metadata": {
"interpreter": {
"hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
},
"kernelspec": {
"display_name": "Python 3.8.10 64-bit",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
2 changes: 1 addition & 1 deletion openai/api_resources/completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from openai.error import InvalidRequestError, TryAgain


class Completion(EngineAPIResource, ListableAPIResource, DeletableAPIResource):
class Completion(EngineAPIResource):
engine_required = False
OBJECT_NAME = "completions"

Expand Down
2 changes: 1 addition & 1 deletion openai/api_resources/embedding.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from openai.error import InvalidRequestError, TryAgain


class Embedding(EngineAPIResource, ListableAPIResource, DeletableAPIResource):
class Embedding(EngineAPIResource):
engine_required = False
OBJECT_NAME = "embeddings"

Expand Down