Skip to content

Commit

Permalink
Update to a new model
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiraae committed Feb 6, 2025
1 parent f8fe369 commit 7fecbfa
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 482 deletions.
143 changes: 57 additions & 86 deletions labs/GPT-4o-inferencing/GPT-4o-inferencing.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@
"\n",
"Playground to try the new GPT-4o model. GPT-4o (\"o\" for \"omni\") is designed to handle a combination of text, audio, and video inputs, and can generate outputs in text, audio, and image formats.\n",
"\n",
"### TOC\n",
"- [0️⃣ Initialize notebook variables](#0)\n",
"- [1️⃣ Create the Azure Resource Group](#1)\n",
"- [2️⃣ Create deployment using 🦾 Bicep](#2)\n",
"- [3️⃣ Get the deployment outputs](#3)\n",
"- [🧪 Test the API using the Azure OpenAI Python SDK](#sdk1)\n",
"- [🧪 Image Processing](#sdk2)\n",
"- [🧪 Base64 Image Processing](#sdk3)\n",
"- [🗑️ Clean up resources](#clean)\n",
"\n",
"### Prerequisites\n",
"- [Python 3.12 or later version](https://www.python.org/) installed\n",
"- [Pandas Library](https://pandas.pydata.org/) installed\n",
Expand All @@ -39,7 +29,6 @@
"### 0️⃣ Initialize notebook variables\n",
"\n",
"- Resources will be suffixed by a unique string based on your subscription id.\n",
"- The ```mock_webapps``` variable sets the list of deployed Web Apps for the mocking functionality. Clean the ```openai_resources``` list to simulate the OpenAI behaviour with the mocking service.\n",
"- Adjust the location parameters according your preferences and on the [product availability by Azure region.](https://azure.microsoft.com/explore/global-infrastructure/products-by-region/?cdn=disable&products=cognitive-services,api-management) \n",
"- Adjust the OpenAI model and version according the [availability by region.](https://learn.microsoft.com/azure/ai-services/openai/concepts/models) "
]
Expand All @@ -52,39 +41,35 @@
},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import datetime\n",
"import requests\n",
"import os, sys, json\n",
"sys.path.insert(1, '../../shared') # add the shared directory to the Python path\n",
"import utils\n",
"\n",
"deployment_name = os.path.basename(os.path.dirname(globals()['__vsc_ipynb_file__']))\n",
"resource_group_name = f\"lab-{deployment_name}\" # change the name to match your naming style\n",
"resource_group_location = \"westeurope\"\n",
"apim_resource_name = \"apim\"\n",
"apim_resource_location = \"westeurope\"\n",
"apim_resource_sku = \"Basicv2\"\n",
"openai_resources = [ {\"name\": \"openai1\", \"location\": \"westus3\"} ] # list of OpenAI resources to deploy. Clear this list to use only the mock resources\n",
"openai_resources_sku = \"S0\"\n",
"resource_group_location = \"uksouth\"\n",
"\n",
"apim_sku = 'Basicv2'\n",
"\n",
"openai_resources = [ {\"name\": \"openai1\", \"location\": \"uksouth\"}]\n",
"\n",
"openai_model_name = \"gpt-4o\"\n",
"openai_model_version = \"2024-05-13\"\n",
"openai_model_version = \"2024-08-06\"\n",
"openai_model_sku = \"GlobalStandard\"\n",
"openai_deployment_name = \"gpt-4o\"\n",
"openai_api_version = \"2024-02-01\"\n",
"openai_specification_url='https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/specification/cognitiveservices/data-plane/AzureOpenAI/inference/stable/' + openai_api_version + '/inference.json'\n",
"openai_backend_pool = \"openai-backend-pool\"\n",
"mock_backend_pool = \"mock-backend-pool\"\n",
"mock_webapps = [ {\"name\": \"openaimock1\", \"endpoint\": \"https://openaimock1.azurewebsites.net\"}, {\"name\": \"openaimock2\", \"endpoint\": \"https://openaimock2.azurewebsites.net\"} ]\n",
"openai_api_version = \"2024-10-21\"\n",
"\n",
"log_analytics_name = \"workspace\"\n",
"app_insights_name = 'insights'\n"
"utils.print_ok('Notebook initialized')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a id='1'></a>\n",
"### 1️⃣ Create the Azure Resource Group\n",
"All resources deployed in this lab will be created in the specified resource group. Skip this step if you want to use an existing resource group."
"### 1️⃣ Verify the Azure CLI and the connected Azure subscription\n",
"\n",
"The following commands ensure that you have the latest version of the Azure CLI and that the Azure CLI is connected to your Azure subscription."
]
},
{
Expand All @@ -93,11 +78,16 @@
"metadata": {},
"outputs": [],
"source": [
"resource_group_stdout = ! az group create --name {resource_group_name} --location {resource_group_location}\n",
"if resource_group_stdout.n.startswith(\"ERROR\"):\n",
" print(resource_group_stdout)\n",
"else:\n",
" print(\"✅ Azure Resource Group \", resource_group_name, \" created ⌚ \", datetime.datetime.now().time())"
"output = utils.run(\"az account show\", \"Retrieved az account\", \"Failed to get the current az account\")\n",
"\n",
"if output.success and output.json_data:\n",
" current_user = output.json_data['user']['name']\n",
" tenant_id = output.json_data['tenantId']\n",
" subscription_id = output.json_data['id']\n",
"\n",
" utils.print_info(f\"Current user: {current_user}\")\n",
" utils.print_info(f\"Tenant ID: {tenant_id}\")\n",
" utils.print_info(f\"Subscription ID: {subscription_id}\")"
]
},
{
Expand All @@ -107,7 +97,7 @@
"<a id='2'></a>\n",
"### 2️⃣ Create deployment using 🦾 Bicep\n",
"\n",
"This lab uses [Bicep](https://learn.microsoft.com/azure/azure-resource-manager/bicep/overview?tabs=bicep) to declarative define all the resources that will be deployed. Change the parameters or the [main.bicep](main.bicep) directly to try different configurations. "
"This lab uses [Bicep](https://learn.microsoft.com/azure/azure-resource-manager/bicep/overview?tabs=bicep) to declarative define all the resources that will be deployed in the specified resource group. Change the parameters or the [main.bicep](main.bicep) directly to try different configurations. "
]
},
{
Expand All @@ -116,43 +106,31 @@
"metadata": {},
"outputs": [],
"source": [
"if len(openai_resources) > 0:\n",
" backend_id = openai_backend_pool if len(openai_resources) > 1 else openai_resources[0].get(\"name\")\n",
"elif len(mock_webapps) > 0:\n",
" backend_id = mock_backend_pool if len(mock_backend_pool) > 1 else mock_webapps[0].get(\"name\")\n",
"\n",
"with open(\"policy.xml\", 'r') as policy_xml_file:\n",
" policy_template_xml = policy_xml_file.read()\n",
" policy_xml = policy_template_xml.replace(\"{backend-id}\", backend_id)\n",
" policy_xml_file.close()\n",
"open(\"policy.xml\", 'w').write(policy_xml)\n",
"# Create the resource group if doesn't exist\n",
"utils.create_resource_group(resource_group_name, resource_group_location)\n",
"\n",
"# Define the Bicep parameters\n",
"bicep_parameters = {\n",
" \"$schema\": \"https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#\",\n",
" \"contentVersion\": \"1.0.0.0\",\n",
" \"parameters\": {\n",
" \"mockWebApps\": { \"value\": mock_webapps },\n",
" \"mockBackendPoolName\": { \"value\": mock_backend_pool },\n",
" \"openAIBackendPoolName\": { \"value\": openai_backend_pool },\n",
" \"openAIConfig\": { \"value\": openai_resources },\n",
" \"openAIDeploymentName\": { \"value\": openai_deployment_name },\n",
" \"openAISku\": { \"value\": openai_resources_sku },\n",
" \"openAIModelName\": { \"value\": openai_model_name },\n",
" \"openAIModelVersion\": { \"value\": openai_model_version },\n",
" \"openAIAPISpecURL\": { \"value\": openai_specification_url },\n",
" \"apimResourceName\": { \"value\": apim_resource_name},\n",
" \"apimResourceLocation\": { \"value\": apim_resource_location},\n",
" \"apimSku\": { \"value\": apim_resource_sku},\n",
" \"logAnalyticsName\": { \"value\": log_analytics_name },\n",
" \"applicationInsightsName\": { \"value\": app_insights_name }\n",
" }\n",
" \"$schema\": \"https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#\",\n",
" \"contentVersion\": \"1.0.0.0\",\n",
" \"parameters\": {\n",
" \"apimSku\": { \"value\": apim_sku },\n",
" \"openAIConfig\": { \"value\": openai_resources },\n",
" \"openAIDeploymentName\": { \"value\": openai_deployment_name },\n",
" \"openAIModelName\": { \"value\": openai_model_name },\n",
" \"openAIModelSKU\": { \"value\": openai_model_sku },\n",
" \"openAIModelVersion\": { \"value\": openai_model_version },\n",
" \"openAIAPIVersion\": { \"value\": openai_api_version }\n",
" }\n",
"}\n",
"\n",
"# Write the parameters to the params.json file\n",
"with open('params.json', 'w') as bicep_parameters_file:\n",
" bicep_parameters_file.write(json.dumps(bicep_parameters))\n",
"\n",
"! az deployment group create --name {deployment_name} --resource-group {resource_group_name} --template-file \"main.bicep\" --parameters \"params.json\"\n",
"\n",
"open(\"policy.xml\", 'w').write(policy_template_xml)\n"
"# Run the deployment\n",
"output = utils.run(f\"az deployment group create --name {deployment_name} --resource-group {resource_group_name} --template-file main.bicep --parameters params.json\",\n",
" f\"Deployment '{deployment_name}' succeeded\", f\"Deployment '{deployment_name}' failed\")"
]
},
{
Expand All @@ -162,7 +140,7 @@
"<a id='3'></a>\n",
"### 3️⃣ Get the deployment outputs\n",
"\n",
"We are now at the stage where we only need to retrieve the gateway URL and the subscription before we are ready for testing."
"Retrieve the required outputs from the Bicep deployment."
]
},
{
Expand All @@ -171,20 +149,13 @@
"metadata": {},
"outputs": [],
"source": [
"deployment_stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} --query properties.outputs.apimSubscriptionKey.value -o tsv\n",
"apim_subscription_key = deployment_stdout.n\n",
"deployment_stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} --query properties.outputs.apimResourceGatewayURL.value -o tsv\n",
"apim_resource_gateway_url = deployment_stdout.n\n",
"print(\"👉🏻 API Gateway URL: \", apim_resource_gateway_url)\n",
"# Obtain all of the outputs from the deployment\n",
"output = utils.run(f\"az deployment group show --name {deployment_name} -g {resource_group_name}\", f\"Retrieved deployment: {deployment_name}\", f\"Failed to retrieve deployment: {deployment_name}\")\n",
"\n",
"deployment_stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} --query properties.outputs.logAnalyticsWorkspaceId.value -o tsv\n",
"workspace_id = deployment_stdout.n\n",
"print(\"👉🏻 Workspace ID: \", workspace_id)\n",
"\n",
"deployment_stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} --query properties.outputs.applicationInsightsAppId.value -o tsv\n",
"app_id = deployment_stdout.n\n",
"print(\"👉🏻 App ID: \", app_id)\n",
"\n"
"if output.success and output.json_data:\n",
" apim_service_id = utils.get_deployment_output(output, 'apimServiceId', 'APIM Service Id')\n",
" apim_resource_gateway_url = utils.get_deployment_output(output, 'apimResourceGatewayURL', 'APIM API Gateway URL')\n",
" apim_subscription_key = utils.get_deployment_output(output, 'apimSubscription1Key', 'APIM Subscription 1 Key (masked)', True)"
]
},
{
Expand All @@ -202,7 +173,7 @@
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import time, requests\n",
"runs = 1\n",
"sleep_time_ms = 1000\n",
"\n",
Expand All @@ -220,7 +191,7 @@
" print(\"x-ms-region: \", response.headers.get(\"x-ms-region\")) # this header is useful to determine the region of the backend that served the request\n",
" if (response.status_code == 200):\n",
" data = json.loads(response.text)\n",
" print(\"response: \", data.get(\"choices\")[0].get(\"message\").get(\"content\"))\n",
" print(\"💬 \", data.get(\"choices\")[0].get(\"message\").get(\"content\"))\n",
" else:\n",
" print(response.text)\n",
" time.sleep(sleep_time_ms/1000)\n"
Expand Down Expand Up @@ -339,7 +310,7 @@
" ],\n",
" temperature=0.0,\n",
")\n",
"HTML('<font size=\"5\">' + response.choices[0].message.content + '</font>')"
"HTML(f'<font size=\"5\">{response.choices[0].message.content}</font>')"
]
},
{
Expand Down Expand Up @@ -370,7 +341,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.2"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down
Loading

0 comments on commit 7fecbfa

Please sign in to comment.