Merge pull request #50 from cmurtz-msft/azure-openai-ga

Azure OpenAI GA
pull/77/head
Ted Sanders 1 year ago committed by GitHub
commit 4adea8e4e2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,227 @@
{
"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",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import openai\n",
"from openai import cli"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Setup\n",
"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."
]
},
{
"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": [
"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`."
]
},
{
"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": [
"openai.api_type = 'azure'\n",
"openai.api_key = '' # Please add your api key here"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### (Optional) Setup: Microsoft Active Directory Authentication\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# from azure.identity import DefaultAzureCredential\n",
"\n",
"# default_credential = DefaultAzureCredential()\n",
"# token = default_credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
"\n",
"# openai.api_type = 'azure_ad'\n",
"# openai.api_key = token.token"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Deployments\n",
"In this section we are going to create a deployment using the `text-davinci-002` model that we can then use to create completions."
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### Deployments: Create manually\n",
"Create a new deployment by going to your Resource in your portal under \"Resource Management\" -> \"Model deployments\". Select `text-davinci-002` as the model."
]
},
{
"attachments": {},
"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-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",
"deployment_id = result[\"id\"]\n",
"print(f'Successfully created deployment with id: {deployment_id}')"
]
},
{
"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)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.10.8"
},
"vscode": {
"interpreter": {
"hash": "3a5103089ab7e7c666b279eeded403fcec76de49a40685dbdfe9f9c78ad97c17"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}

@ -1,12 +1,13 @@
{
"cells": [
{
"attachments": {},
"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."
"This example focuses on embeddings but also touches 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."
]
},
{
@ -20,12 +21,39 @@
]
},
{
"attachments": {},
"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."
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"openai.api_version = '2022-12-01'\n",
"openai.api_base = '' # Please add your endpoint here"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"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`."
]
},
{
"attachments": {},
"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."
]
},
{
@ -34,11 +62,32 @@
"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"
"openai.api_key = '' # Please add your api key here"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"### (Optional) Setup: Microsoft Active Directory Authentication\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# from azure.identity import DefaultAzureCredential\n",
"\n",
"# default_credential = DefaultAzureCredential()\n",
"# token = default_credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
"\n",
"# openai.api_type = 'azure_ad'\n",
"# openai.api_key = token.token"
]
},
{
@ -46,22 +95,22 @@
"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."
"In this section we are going to create a deployment that we can use to create embeddings."
]
},
{
"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\"."
"### Deployments: Create manually\n",
"Let's create a deployment using the `text-similarity-curie-001` model. Create a new deployment by going to your Resource in your portal under \"Resource Management\" -> \"Model deployments\"."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### (Optional) Deployments: Create Programatically\n",
"### (Optional) Deployments: Create programatically\n",
"We can also create a deployment using code:"
]
},
@ -113,18 +162,24 @@
"metadata": {},
"outputs": [],
"source": [
"print('While deployment running, selecting a completed one.')\n",
"print('While deployment running, selecting a completed one that supports embeddings.')\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",
" 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",
"\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}.')"
" print(f'Found a succeeded deployment that supports embeddings with id: {deployment_id}.')"
]
},
{
@ -168,7 +223,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.9 ('openai')",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
@ -182,12 +237,11 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.9"
"version": "3.10.8"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "365536dcbde60510dc9073d6b991cd35db2d9bac356a11f5b64279a5e6708b97"
"hash": "3a5103089ab7e7c666b279eeded403fcec76de49a40685dbdfe9f9c78ad97c17"
}
}
},

@ -6,7 +6,7 @@
"source": [
"# Azure Fine tuning example\n",
"In this example we'll try to go over all operations that can be done using the Azure endpoints and their differences with the openAi endpoints (if any).<br>\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 finetune model adaptation tutorial.\n"
"This example focuses on finetuning but also touches on the majority of operations that are available using the API. This example is meant to be a quick way of showing simple operations and is not meant as a finetune model adaptation tutorial.\n"
]
},
{
@ -24,7 +24,32 @@
"metadata": {},
"source": [
"## Setup\n",
"In the following section the endpoint and key need to be set up of the next sections to work.<br> 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."
"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."
]
},
{
"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": [
"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`."
]
},
{
"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."
]
},
{
@ -33,19 +58,17 @@
"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"
"openai.api_key = '' # Please add your api key here"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Microsoft Active Directory Authentication\n",
"Instead of key based authentication, you can use Active Directory to authenticate using credential tokens. Uncomment the next code section to use credential based authentication:"
"### (Optional) Setup: Microsoft Active Directory Authentication\n",
"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."
]
},
{
@ -54,19 +77,13 @@
"metadata": {},
"outputs": [],
"source": [
"\"\"\"\n",
"from azure.identity import DefaultAzureCredential\n",
"\n",
"default_credential = DefaultAzureCredential()\n",
"token = default_credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
"\n",
"openai.api_type = 'azure_ad'\n",
"openai.api_key = token.token\n",
"openai.api_version = '2022-03-01-preview' # this may change in the future\n",
"# from azure.identity import DefaultAzureCredential\n",
"\n",
"# default_credential = DefaultAzureCredential()\n",
"# token = default_credential.get_token(\"https://cognitiveservices.azure.com/.default\")\n",
"\n",
"openai.api_base = '' # Please add your endpoint here\n",
"\"\"\""
"# openai.api_type = 'azure_ad'\n",
"# openai.api_key = token.token"
]
},
{
@ -89,9 +106,9 @@
"training_file_name = 'training.jsonl'\n",
"validation_file_name = 'validation.jsonl'\n",
"\n",
"sample_data = [{\"prompt\": \"When I go to the store, I want an\", \"completion\": \"apple\"},\n",
" {\"prompt\": \"When I go to work, I want a\", \"completion\": \"coffe\"},\n",
" {\"prompt\": \"When I go home, I want a\", \"completion\": \"soda\"}]\n",
"sample_data = [{\"prompt\": \"When I go to the store, I want an\", \"completion\": \"apple.\"},\n",
" {\"prompt\": \"When I go to work, I want a\", \"completion\": \"coffee.\"},\n",
" {\"prompt\": \"When I go home, I want a\", \"completion\": \"soda.\"}]\n",
"\n",
"print(f'Generating the training file: {training_file_name}')\n",
"with open(training_file_name, 'w') as training_file:\n",
@ -141,7 +158,7 @@
"metadata": {},
"outputs": [],
"source": [
"print(f'Deleting already uploaded files.')\n",
"print(f'Deleting already uploaded files...')\n",
"for id in results:\n",
" openai.File.delete(sid = id)\n"
]
@ -197,7 +214,7 @@
"source": [
"print(f'Downloading training file: {training_id}')\n",
"result = openai.File.download(training_id)\n",
"print(result)"
"print(result.decode('utf-8'))"
]
},
{
@ -225,9 +242,12 @@
"create_args = {\n",
" \"training_file\": training_id,\n",
" \"validation_file\": validation_id,\n",
" \"model\": \"curie\",\n",
" \"model\": \"babbage\",\n",
" \"compute_classification_metrics\": True,\n",
" \"classification_n_classes\": 3\n",
" \"classification_n_classes\": 3,\n",
" \"n_epochs\": 20,\n",
" \"batch_size\": 3,\n",
" \"learning_rate_multiplier\": 0.3\n",
"}\n",
"resp = openai.FineTune.create(**create_args)\n",
"job_id = resp[\"id\"]\n",
@ -258,7 +278,7 @@
" print(f\"Stream interrupted. Job is still {status}.\")\n",
" return\n",
"\n",
"print('Streaming events for the fine-tuning job: {job_id}')\n",
"print(f'Streaming events for the fine-tuning job: {job_id}')\n",
"signal.signal(signal.SIGINT, signal_handler)\n",
"\n",
"events = openai.FineTune.stream_events(job_id)\n",
@ -296,7 +316,7 @@
"\n",
"print('Checking other finetune jobs in the subscription.')\n",
"result = openai.FineTune.list()\n",
"print(f'Found {len(result)} finetune jobs.')"
"print(f'Found {len(result.data)} finetune jobs.')"
]
},
{
@ -413,10 +433,10 @@
"outputs": [],
"source": [
"print('Sending a test completion job')\n",
"start_phrase = 'When I go to the store, I want a'\n",
"response = openai.Completion.create(deployment_id=deployment_id, prompt=start_phrase, max_tokens=4)\n",
"start_phrase = 'When I go home, I want a'\n",
"response = openai.Completion.create(deployment_id=deployment_id, prompt=start_phrase, temperature=0, stop=\".\")\n",
"text = response['choices'][0]['text'].replace('\\n', '').replace(' .', '.').strip()\n",
"print(f'\"{start_phrase} {text}\"')\n"
"print(f'\"{start_phrase} {text}.\"')"
]
},
{
@ -447,7 +467,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.9.9 64-bit ('3.9.9')",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
@ -461,12 +481,11 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.9"
"version": "3.10.8"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "cb9817b186a29e4e9713184d901f26c1ee05ad25243d878baff7f31bb1fef480"
"hash": "3a5103089ab7e7c666b279eeded403fcec76de49a40685dbdfe9f9c78ad97c17"
}
}
},

Loading…
Cancel
Save