|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# Manage Analyzers in Your Resource" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "markdown", |
| 12 | + "metadata": {}, |
| 13 | + "source": [ |
| 14 | + "This notebook demo how to create a simple analyzer and manage its lifecycle." |
| 15 | + ] |
| 16 | + }, |
| 17 | + { |
| 18 | + "cell_type": "markdown", |
| 19 | + "metadata": {}, |
| 20 | + "source": [ |
| 21 | + "## Prerequisites\n", |
| 22 | + "1. Ensure Azure AI service is configured following [steps](../README.md#configure-azure-ai-service-resource)\n", |
| 23 | + "2. Install the required packages to run the sample." |
| 24 | + ] |
| 25 | + }, |
| 26 | + { |
| 27 | + "cell_type": "code", |
| 28 | + "execution_count": null, |
| 29 | + "metadata": {}, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "%pip install -r ../requirements.txt" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "markdown", |
| 37 | + "metadata": {}, |
| 38 | + "source": [ |
| 39 | + "## Create Azure AI Content Understanding Client\n", |
| 40 | + "\n", |
| 41 | + "> The [AzureContentUnderstandingClient](../python/content_understanding_client.py) is a utility class containing functions to interact with the Content Understanding API. Before the official release of the Content Understanding SDK, it can be regarded as a lightweight SDK.\n" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "code", |
| 46 | + "execution_count": null, |
| 47 | + "metadata": {}, |
| 48 | + "outputs": [], |
| 49 | + "source": [ |
| 50 | + "import logging\n", |
| 51 | + "import json\n", |
| 52 | + "import os\n", |
| 53 | + "import sys\n", |
| 54 | + "from pathlib import Path\n", |
| 55 | + "from dotenv import find_dotenv, load_dotenv\n", |
| 56 | + "from azure.identity import DefaultAzureCredential, get_bearer_token_provider\n", |
| 57 | + "\n", |
| 58 | + "load_dotenv(find_dotenv())\n", |
| 59 | + "logging.basicConfig(level=logging.INFO)\n", |
| 60 | + "\n", |
| 61 | + "AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n", |
| 62 | + "AZURE_AI_API_VERSION = os.getenv(\"AZURE_AI_API_VERSION\", \"2024-12-01-preview\")\n", |
| 63 | + "\n", |
| 64 | + "# Add the parent directory to the path to use shared modules\n", |
| 65 | + "parent_dir = Path(Path.cwd()).parent\n", |
| 66 | + "sys.path.append(str(parent_dir))\n", |
| 67 | + "from python.content_understanding_client import AzureContentUnderstandingClient\n", |
| 68 | + "\n", |
| 69 | + "credential = DefaultAzureCredential()\n", |
| 70 | + "token_provider = get_bearer_token_provider(credential, \"https://cognitiveservices.azure.com/.default\")\n", |
| 71 | + "\n", |
| 72 | + "client = AzureContentUnderstandingClient(\n", |
| 73 | + " endpoint=AZURE_AI_ENDPOINT,\n", |
| 74 | + " api_version=AZURE_AI_API_VERSION,\n", |
| 75 | + " token_provider=token_provider,\n", |
| 76 | + " x_ms_useragent=\"azure-ai-content-understanding-python/field_extraction\",\n", |
| 77 | + ")" |
| 78 | + ] |
| 79 | + }, |
| 80 | + { |
| 81 | + "cell_type": "markdown", |
| 82 | + "metadata": {}, |
| 83 | + "source": [ |
| 84 | + "## Create a simple analyzer\n", |
| 85 | + "We first create an analyzer from a template to extract invoice fields." |
| 86 | + ] |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": null, |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "import uuid\n", |
| 95 | + "\n", |
| 96 | + "ANALYZER_TEMPLATE = \"../analyzer_templates/invoice.json\"\n", |
| 97 | + "ANALYZER_ID = \"analyzer-management-sample-\" + str(uuid.uuid4())\n", |
| 98 | + "\n", |
| 99 | + "response = client.begin_create_analyzer(ANALYZER_ID, analyzer_template_path=ANALYZER_TEMPLATE)\n", |
| 100 | + "result = client.poll_result(response)\n", |
| 101 | + "\n", |
| 102 | + "print(json.dumps(result, indent=2))" |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "markdown", |
| 107 | + "metadata": {}, |
| 108 | + "source": [ |
| 109 | + "## List all analyzers created in your resource" |
| 110 | + ] |
| 111 | + }, |
| 112 | + { |
| 113 | + "cell_type": "markdown", |
| 114 | + "metadata": {}, |
| 115 | + "source": [ |
| 116 | + "After the analyzer is successfully created, we can use it to analyze our input files." |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "cell_type": "code", |
| 121 | + "execution_count": null, |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [], |
| 124 | + "source": [ |
| 125 | + "response = client.get_all_analyzers()\n", |
| 126 | + "print(f\"Number of analyzers in your resource: {len(response['value'])}\")\n", |
| 127 | + "print(f\"First 3 analyzer details: {json.dumps(response['value'][:3], indent=2)}\")" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "markdown", |
| 132 | + "metadata": {}, |
| 133 | + "source": [ |
| 134 | + "## Get analyzer details with id\n", |
| 135 | + "\n", |
| 136 | + "Remember the analyzer id when you create it. You can use the id to look up detail analyzer definitions afterwards." |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "code", |
| 141 | + "execution_count": null, |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "result = client.get_analyzer_detail_by_id(ANALYZER_ID)\n", |
| 146 | + "print(json.dumps(result, indent=2))" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "## Delete Analyzer\n", |
| 154 | + "If you don't need an analyzer anymore, delete it with its id." |
| 155 | + ] |
| 156 | + }, |
| 157 | + { |
| 158 | + "cell_type": "code", |
| 159 | + "execution_count": null, |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [ |
| 163 | + "client.delete_analyzer(ANALYZER_ID)" |
| 164 | + ] |
| 165 | + } |
| 166 | + ], |
| 167 | + "metadata": { |
| 168 | + "kernelspec": { |
| 169 | + "display_name": "Python 3", |
| 170 | + "language": "python", |
| 171 | + "name": "python3" |
| 172 | + }, |
| 173 | + "language_info": { |
| 174 | + "codemirror_mode": { |
| 175 | + "name": "ipython", |
| 176 | + "version": 3 |
| 177 | + }, |
| 178 | + "file_extension": ".py", |
| 179 | + "mimetype": "text/x-python", |
| 180 | + "name": "python", |
| 181 | + "nbconvert_exporter": "python", |
| 182 | + "pygments_lexer": "ipython3", |
| 183 | + "version": "3.11.11" |
| 184 | + } |
| 185 | + }, |
| 186 | + "nbformat": 4, |
| 187 | + "nbformat_minor": 2 |
| 188 | +} |
0 commit comments