2022-12-16 11:34:49 +00:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Azure completions example\n",
"In this example we'll try to go over all operations needed to get completions working using the Azure endpoints. \\\n",
2022-12-19 08:17:29 +00:00
"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."
2022-12-16 11:34:49 +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",
2022-12-20 15:19:15 +00:00
"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."
2022-12-16 11:34:49 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai.api_version = '2022-12-01'\n",
"openai.api_base = '' # Please add your endpoint here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
2022-12-20 15:19:15 +00:00
"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`."
2022-12-16 11:34:49 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup: Portal\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2022-12-20 15:19:15 +00:00
"openai.api_type = 'azure'\n",
2022-12-16 11:34:49 +00:00
"openai.api_key = '' # Please add your api key here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### (Optional) Setup: Microsoft Active Directory Authentication\n",
2022-12-20 15:50:42 +00:00
"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."
2022-12-16 11:34:49 +00:00
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
2022-12-20 15:50:42 +00:00
"# from azure.identity import DefaultAzureCredential\n",
2022-12-16 11:34:49 +00:00
"\n",
2022-12-20 15:50:42 +00:00
"# default_credential = DefaultAzureCredential()\n",
"# token = default_credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
2022-12-16 11:34:49 +00:00
"\n",
2022-12-20 15:50:42 +00:00
"# openai.api_type = 'azure_ad'\n",
"# openai.api_key = token.token"
2022-12-16 11:34:49 +00:00
]
},
{
2022-12-19 08:17:29 +00:00
"attachments": {},
2022-12-16 11:34:49 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
"## Deployments\n",
2022-12-19 08:17:29 +00:00
"In this section we are going to create a deployment using the `text-davinci-002` model that we can then use to create completions."
2022-12-16 11:34:49 +00:00
]
},
{
2022-12-19 08:17:29 +00:00
"attachments": {},
2022-12-16 11:34:49 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
2022-12-19 08:17:29 +00:00
"### Deployments: Create manually\n",
2022-12-16 11:34:49 +00:00
"Create a new deployment by going to your Resource in your portal under \"Resource Management\" -> \"Model deployments\". Select `text-davinci-002` as the model."
]
},
{
2022-12-19 08:17:29 +00:00
"attachments": {},
2022-12-16 11:34:49 +00:00
"cell_type": "markdown",
"metadata": {},
"source": [
2022-12-19 08:17:29 +00:00
"### (Optional) Deployments: Create programatically\n",
2022-12-16 11:34:49 +00:00
"We can also create a deployment using code:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = \"text-davinci-002\"\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\":\"standard\"})\n",
2022-12-19 08:17:29 +00:00
"deployment_id = result[\"id\"]\n",
"print(f'Successfully created deployment with id: {deployment_id}')"
2022-12-16 11:34:49 +00:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### (Optional) Deployments: Wait for deployment to succeed\n",
"Now let's check the status of the newly created deployment and wait till it is succeeded."
]
},
{
"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} has status: {status}')\n",
"while status not in [\"succeeded\", \"failed\"]:\n",
" resp = openai.Deployment.retrieve(id=deployment_id)\n",
" status = resp[\"status\"]\n",
" print(f'Deployment {deployment_id} has status: {status}')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Completions\n",
"Now let's send a sample completion to the deployment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"prompt = \"The food was delicious and the waiter\"\n",
"completion = openai.Completion.create(deployment_id=deployment_id,\n",
" prompt=prompt, stop=\".\", temperature=0)\n",
" \n",
"print(f\"{prompt}{completion['choices'][0]['text']}.\")"
]
},
{
"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)"
]
2022-12-20 15:50:42 +00:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
2022-12-16 11:34:49 +00:00
}
],
"metadata": {
"kernelspec": {
2022-12-16 15:43:18 +00:00
"display_name": "Python 3 (ipykernel)",
2022-12-16 11:34:49 +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-19 08:17:29 +00:00
"version": "3.10.8"
2022-12-16 11:34:49 +00:00
},
"vscode": {
"interpreter": {
"hash": "3a5103089ab7e7c666b279eeded403fcec76de49a40685dbdfe9f9c78ad97c17"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}