Skip to content

Commit

Permalink
token counting lab scaffolding
Browse files Browse the repository at this point in the history
  • Loading branch information
vieiraae committed Apr 21, 2024
1 parent 69c86ca commit d81edea
Show file tree
Hide file tree
Showing 6 changed files with 1,264 additions and 0 deletions.
22 changes: 22 additions & 0 deletions labs/token-counting/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# APIM ❤️ OpenAI

## [Token counting lab)](token-counting.ipynb)
[![flow](../../images/token-counting.gif)](token-counting.ipynb)

Playground to try the ...

### Prerequisites
- [Python 3.8 or later version](https://www.python.org/) installed
- [Pandas Library](https://pandas.pydata.org) installed
- [VS Code](https://code.visualstudio.com/) installed with the [Jupyter notebook extension](https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter) enabled
- [Azure CLI](https://learn.microsoft.com/en-us/cli/azure/install-azure-cli) installed
- [An Azure Subscription](https://azure.microsoft.com/en-us/free/) with Contributor permissions
- [Access granted to Azure OpenAI](https://aka.ms/oai/access) or just enable the mock service
- [Sign in to Azure with Azure CLI](https://learn.microsoft.com/en-us/cli/azure/authenticate-azure-cli-interactively)

### 🚀 Get started
Proceed by opening the [Jupyter notebook](token-counting.ipynb), and follow the steps provided.

### 🗑️ Clean up resources
When you're finished with the lab, you should remove all your deployed resources from Azure to avoid extra charges and keep your Azure subscription uncluttered.
Use the [clean-up-resources notebook](clean-up-resources.ipynb) for that.
82 changes: 82 additions & 0 deletions labs/token-counting/clean-up-resources.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 🗑️ Clean up resources\n",
"\n",
"When you're finished with the lab, you should remove all your deployed resources from Azure to avoid extra charges and keep your Azure subscription uncluttered."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import json\n",
"import datetime\n",
"\n",
"deployment_name = os.path.basename(os.path.dirname(globals()['__vsc_ipynb_file__']))\n",
"resource_group_name = f\"lab-{deployment_name}\"\n",
"\n",
"def log(stdout, name, action):\n",
" if stdout.n.startswith(\"ERROR\"):\n",
" print(\"👎🏻 \", name, \" was NOT \", action, \": \", stdout)\n",
" else:\n",
" print(\"👍🏻 \", name, \" was \", action, \"\", datetime.datetime.now().time())\n",
"\n",
"deployment_stdout = ! az deployment group show --name {deployment_name} -g {resource_group_name} -o json \n",
"deployment = json.loads(deployment_stdout.n)\n",
"for resource in deployment.get(\"properties\").get(\"outputResources\"):\n",
" resource_id = resource.get(\"id\")\n",
" try:\n",
" query = \"\\\"{type:type, name:name, location:location}\\\"\"\n",
" resource_stdout = ! az resource show --id {resource_id} --query {query} -o json\n",
" resource = json.loads(resource_stdout.n)\n",
" resource_name = resource.get(\"name\")\n",
" resource_type = resource.get(\"type\") \n",
" if resource_type == \"Microsoft.CognitiveServices/accounts\":\n",
" resource_location = \"\\\"\" + resource.get(\"location\") + \"\\\"\"\n",
" delete_stdout = ! az cognitiveservices account delete -g {resource_group_name} -n {resource_name}\n",
" log(delete_stdout, resource_name, \"deleted\")\n",
" delete_stdout = ! az cognitiveservices account purge -g {resource_group_name} -n {resource_name} -l {resource_location}\n",
" log(delete_stdout, resource_name, \"purged\")\n",
" elif resource_type == \"Microsoft.ApiManagement/service\":\n",
" resource_location = \"\\\"\" + resource.get(\"location\") + \"\\\"\"\n",
" delete_stdout = ! az apim delete -n {resource_name} -g {resource_group_name} -y\n",
" log(delete_stdout, resource_name, \"deleted\")\n",
" delete_stdout = ! az apim deletedservice purge --service-name {resource_name} --location {resource_location}\n",
" log(delete_stdout, resource_name, \"purged\")\n",
" except:\n",
" print(\"✌🏻 \", resource_id, \" ignored\")\n",
"delete_stdout = ! az group delete --name {resource_group_name} -y\n",
"log(delete_stdout, resource_group_name, \"deleted\")\n",
"\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.12.2"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading

0 comments on commit d81edea

Please sign in to comment.