2022-07-12 16:35:44 +00:00
{
"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",
2022-12-16 10:48:32 +00:00
"This example focuses on embeddings but also touches on the 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."
2022-07-12 16:35:44 +00:00
]
},
{
"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",
2022-12-16 10:48:32 +00:00
"openai.api_version = '2022-12-01' # this may change in the future"
2022-07-12 16:35:44 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Deployments\n",
2022-12-16 10:48:32 +00:00
"In this section we are going to create a deployment that we can use to create embeddings."
2022-07-12 16:35:44 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Deployments: Create Manually\n",
2022-12-16 10:48:32 +00:00
"Let's create a deployment using the `text-similarity-curie-001` engine. Create a new deployment by going to your Resource in your portal under \"Resource Management\" -> \"Deployments\"."
2022-07-12 16:35:44 +00:00
]
},
{
"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",
2022-09-19 12:42:29 +00:00
"result = openai.Deployment.create(model=model, scale_settings={\"scale_type\":\"standard\"})\n",
2022-07-12 16:35:44 +00:00
"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": [
2022-12-16 10:48:32 +00:00
"print('While deployment running, selecting a completed one that supports embeddings.')\n",
2022-07-12 16:35:44 +00:00
"deployment_id = None\n",
"result = openai.Deployment.list()\n",
"for deployment in result.data:\n",
2022-12-16 10:48:32 +00:00
" if deployment[\"status\"] != \"succeeded\":\n",
" continue\n",
" \n",
" model = openai.Model.retrieve(deployment[\"model\"])\n",
" if model[\"capabilities\"][\"embeddings\"] != True:\n",
" continue\n",
" \n",
" deployment_id = deployment[\"id\"]\n",
" break\n",
2022-07-12 16:35:44 +00:00
"\n",
"if not deployment_id:\n",
" print('No deployment with status: succeeded found.')\n",
"else:\n",
2022-12-16 10:48:32 +00:00
" print(f'Found a succeeded deployment that supports embeddings with id: {deployment_id}.')"
2022-07-12 16:35:44 +00:00
]
},
{
"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": [
2022-07-21 00:35:15 +00:00
"embeddings = openai.Embedding.create(deployment_id=deployment_id,\n",
2022-07-12 16:35:44 +00:00
" 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": {
"kernelspec": {
2022-12-16 15:43:18 +00:00
"display_name": "Python 3 (ipykernel)",
2022-07-12 16:35:44 +00:00
"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",
2022-12-16 15:43:18 +00:00
"version": "3.8.10"
2022-07-12 16:35:44 +00:00
},
"vscode": {
"interpreter": {
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}