Skip to content

Commit 293196b

Browse files
add using api key
1 parent 6331c9e commit 293196b

8 files changed

+90
-18
lines changed

README.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ Once you click the link above, please follow the steps below to set up the Codes
7171
## Configure Azure AI service resource
7272
### (Option 1) Use `azd` commands to auto create temporal resources to run sample
7373
1. Make sure you have permission to grant roles under subscription
74-
1. Login Azure
74+
2. Login Azure
7575
```shell
7676
azd auth login
7777
```
@@ -80,12 +80,11 @@ Once you click the link above, please follow the steps below to set up the Codes
8080
azd auth login --use-device-code
8181
```
8282
83-
1. Setting up environment, following prompts to choose location
83+
3. Setting up environment, following prompts to choose location
8484
```shell
8585
azd up
8686
```
8787
88-
8988
### (Option 2) Manually create resources and set environment variables
9089
1. Create [Azure AI Services resource](docs/create_azure_ai_service.md)
9190
2. Go to `Access Control (IAM)` in resource, grant yourself role `Cognitive Services User`
@@ -97,6 +96,20 @@ Once you click the link above, please follow the steps below to set up the Codes
9796
azd auth login
9897
```
9998

99+
### (Option 3) Use Endpoint and Key (No `azd` Required)
100+
1. Create [Azure AI Services resource](docs/create_azure_ai_service.md)
101+
2. Copy `notebooks/.env.sample` to `notebooks/.env`
102+
```bash
103+
cp notebooks/.env.sample notebooks/.env
104+
```
105+
3. Update `.env` with your credentials
106+
- Edit notebooks/.env and set the following values:
107+
```
108+
AZURE_AI_ENDPOINT=https://<your-resource-name>.services.ai.azure.com/
109+
AZURE_AI_API_KEY=<your-azure-ai-api-key>
110+
```
111+
- Replace <your-resource-name> and <your-azure-ai-api-key> with your actual values. You can find them in your AI Services resource under `Resource Management`/`Keys and Endpoint`
112+
100113
## Open a Jupyter notebook and follow the step-by-step guidance
101114

102115
Navigate to the `notebooks` directory and select the sample notebook you are interested in. Since the Dev Container (in Codespaces or in your local enviornment) is pre-configured with the necessary environment, you can directly execute each step in the notebook.

notebooks/analyzer_training.ipynb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@
6060
"metadata": {},
6161
"source": [
6262
"## Create Azure content understanding client\n",
63-
">The [AzureContentUnderstandingClient](../python/content_understanding_client.py) is utility Class which contain the functions to interact with the Content Understanding server. Before Content Understanding SDK release, we can regard it as a lightweight SDK. Fill the constant **AZURE_AI_ENDPOINT**, **AZURE_AI_API_VERSION**, **AZURE_AI_API_KEY** with the information from your Azure AI Service."
63+
"> The [AzureContentUnderstandingClient](../python/content_understanding_client.py) is utility Class which contain the functions to interact with the Content Understanding server. Before Content Understanding SDK release, we can regard it as a lightweight SDK. Fill the constant **AZURE_AI_ENDPOINT**, **AZURE_AI_API_VERSION**, **AZURE_AI_API_KEY** with the information from your Azure AI Service.\n",
64+
"\n",
65+
"> ⚠️ Important:\n",
66+
"You must update the code below to match your Azure authentication method.\n",
67+
"Look for the `# IMPORTANT` comments and modify those sections accordingly.\n",
68+
"If you skip this step, the sample may not run correctly."
6469
]
6570
},
6671
{
@@ -91,7 +96,10 @@
9196
"client = AzureContentUnderstandingClient(\n",
9297
" endpoint=os.getenv(\"AZURE_AI_ENDPOINT\"),\n",
9398
" api_version=os.getenv(\"AZURE_AI_API_VERSION\", \"2025-05-01-preview\"),\n",
99+
" # IMPORTANT: Comment out token_provider if using subscription key\n",
94100
" token_provider=token_provider,\n",
101+
" # IMPORTANT: Uncomment this if using subscription key\n",
102+
" # subscription_key=os.getenv(\"AZURE_AI_API_KEY\"),\n",
95103
" x_ms_useragent=\"azure-ai-content-understanding-python/analyzer_training\", # This header is used for sample usage telemetry, please comment out this line if you want to opt out.\n",
96104
")"
97105
]

notebooks/build_person_directory.ipynb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,12 @@
2828
"metadata": {},
2929
"source": [
3030
"## Create Azure content understanding face client\n",
31-
">The [AzureContentUnderstandingFaceClient](../python/content_understanding_face_client.py) is a utility class for interacting with the Content Understanding Face service. Before the official SDK is released, this acts as a lightweight SDK. Set the constants **AZURE_AI_ENDPOINT**, **AZURE_AI_API_VERSION**, and **AZURE_AI_API_KEY** with your Azure AI Service information."
31+
"> The [AzureContentUnderstandingFaceClient](../python/content_understanding_face_client.py) is a utility class for interacting with the Content Understanding Face service. Before the official SDK is released, this acts as a lightweight SDK. Set the constants **AZURE_AI_ENDPOINT**, **AZURE_AI_API_VERSION**, and **AZURE_AI_API_KEY** with your Azure AI Service information.\n",
32+
"\n",
33+
"> ⚠️ Important:\n",
34+
"You must update the code below to match your Azure authentication method.\n",
35+
"Look for the `# IMPORTANT` comments and modify those sections accordingly.\n",
36+
"If you skip this step, the sample may not run correctly."
3237
]
3338
},
3439
{
@@ -59,7 +64,10 @@
5964
"client = AzureContentUnderstandingFaceClient(\n",
6065
" endpoint=os.getenv(\"AZURE_AI_ENDPOINT\"),\n",
6166
" api_version=os.getenv(\"AZURE_AI_API_VERSION\", \"2025-05-01-preview\"),\n",
67+
" # IMPORTANT: Comment out token_provider if using subscription key\n",
6268
" token_provider=token_provider,\n",
69+
" # IMPORTANT: Uncomment this if using subscription key\n",
70+
" # subscription_key=os.getenv(\"AZURE_AI_API_KEY\"),\n",
6371
" x_ms_useragent=\"azure-ai-content-understanding-python/build_person_directory\", # This header is used for sample usage telemetry, please comment out this line if you want to opt out.\n",
6472
")"
6573
]

notebooks/classifier.ipynb

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
"\n",
9494
"- **AZURE_AI_ENDPOINT**: Your Azure AI service endpoint URL or set up in \".env\" file\n",
9595
"- **AZURE_AI_API_VERSION**: The Azure AI API version to use. Default is \"2025-05-01-preview\". \n",
96-
"- **SUBSCRIPTION_KEY**: Your subscription key (optional if using token authentication)\n",
96+
"- **AZURE_AI_API_KEY**: Your Azure AI service key (optional if using token authentication)\n",
9797
"- **ANALYZER_SAMPLE_FILE**: Path to the PDF document you want to process"
9898
]
9999
},
@@ -103,13 +103,12 @@
103103
"metadata": {},
104104
"outputs": [],
105105
"source": [
106+
"# For authentication, you can use either token-based auth or subscription key, and only one of them is required\n",
106107
"AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n",
108+
"# IMPORTANT: Replace with your actual subscription key or set up in \".env\" file if not using token auth\n",
109+
"AZURE_AI_API_KEY = os.getenv(\"AZURE_AI_API_KEY\")\n",
107110
"AZURE_AI_API_VERSION = os.getenv(\"AZURE_AI_API_VERSION\", \"2025-05-01-preview\")\n",
108-
"ANALYZER_SAMPLE_FILE = \"../data/MS_Annual_Report_2024.pdf\" # Update this path to your PDF file\n",
109-
"\n",
110-
"# For authentication, you can use either token-based auth or subscription key, and only one of them is required\n",
111-
"# IMPORTANT: Replace with your actual subscription key if not using token auth\n",
112-
"SUBSCRIPTION_KEY = \"dummy_key\"\n",
111+
"ANALYZER_SAMPLE_FILE = \"../data/MS_Annual_Report_2024.pdf\" # Update this path to your PDF file\n",
113112
"\n",
114113
"# Authentication - Using DefaultAzureCredential for token-based auth\n",
115114
"credential = DefaultAzureCredential()\n",
@@ -165,7 +164,7 @@
165164
" \"description\": \"Annual meeting details, stock information, and shareholder services.\"\n",
166165
" }\n",
167166
" },\n",
168-
" \"splitMode\": \"auto\" # IMPORTANT: Automatically detect document boundaries\n",
167+
" \"splitMode\": \"auto\" # IMPORTANT: Automatically detect document boundaries. Can change mode for your needs.\n",
169168
"}\n",
170169
"\n",
171170
"print(\"📄 Classifier Categories:\")\n",
@@ -201,7 +200,7 @@
201200
" # IMPORTANT: Comment out token_provider if using subscription key\n",
202201
" token_provider=token_provider,\n",
203202
" # IMPORTANT: Uncomment this if using subscription key\n",
204-
" # subscription_key=SUBSCRIPTION_KEY\n",
203+
" # subscription_key=AZURE_AI_API_KEY,\n",
205204
" )\n",
206205
" print(\"✅ Content Understanding client initialized successfully!\")\n",
207206
" print(\" Ready to create classifiers and analyzers.\")\n",
@@ -432,7 +431,7 @@
432431
" },\n",
433432
" \"Annual Report\": {\n",
434433
" \"description\": \"Each document must ends with 'end of encounter'. Dont rely on page numbers\",\n",
435-
" \"analyzerId\": analyzer_id # IMPORTANT: Use our custom analyzer for annual reports\n",
434+
" \"analyzerId\": analyzer_id # IMPORTANT: Use created custom analyzer in previous step for annual reports\n",
436435
" },\n",
437436
" \"Declaration_of_custodian\": {\n",
438437
" \"description\": \"Declarations of custodian documents, often used in legal contexts.\"\n",

notebooks/content_extraction.ipynb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838
"source": [
3939
"## Create Azure AI Content Understanding Client\n",
4040
"\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"
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. Fill the constant **AZURE_AI_ENDPOINT**, **AZURE_AI_API_VERSION**, **AZURE_AI_API_KEY** with the information from your Azure AI Service.\n",
42+
"\n",
43+
"> ⚠️ Important:\n",
44+
"You must update the code below to match your Azure authentication method.\n",
45+
"Look for the `# IMPORTANT` comments and modify those sections accordingly.\n",
46+
"If you skip this step, the sample may not run correctly."
4247
]
4348
},
4449
{
@@ -59,7 +64,10 @@
5964
"load_dotenv(find_dotenv())\n",
6065
"logging.basicConfig(level=logging.INFO)\n",
6166
"\n",
67+
"# For authentication, you can use either token-based auth or subscription key, and only one of them is required\n",
6268
"AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n",
69+
"# IMPORTANT: Replace with your actual subscription key or set up in \".env\" file if not using token auth\n",
70+
"AZURE_AI_API_KEY = os.getenv(\"AZURE_AI_API_KEY\")\n",
6371
"AZURE_AI_API_VERSION = os.getenv(\"AZURE_AI_API_VERSION\", \"2025-05-01-preview\")\n",
6472
"\n",
6573
"# Add the parent directory to the path to use shared modules\n",
@@ -73,7 +81,10 @@
7381
"client = AzureContentUnderstandingClient(\n",
7482
" endpoint=AZURE_AI_ENDPOINT,\n",
7583
" api_version=AZURE_AI_API_VERSION,\n",
84+
" # IMPORTANT: Comment out token_provider if using subscription key\n",
7685
" token_provider=token_provider,\n",
86+
" # IMPORTANT: Uncomment this if using subscription key\n",
87+
" # subscription_key=AZURE_AI_API_KEY,\n",
7788
" x_ms_useragent=\"azure-ai-content-understanding-python/content_extraction\", # This header is used for sample usage telemetry, please comment out this line if you want to opt out.\n",
7889
")\n",
7990
"\n",

notebooks/conversational_field_extraction.ipynb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@
8888
"source": [
8989
"## Create Azure AI Content Understanding Client\n",
9090
"\n",
91-
"> 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"
91+
"> 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. Fill the constant **AZURE_AI_ENDPOINT**, **AZURE_AI_API_VERSION**, **AZURE_AI_API_KEY** with the information from your Azure AI Service.\n",
92+
"\n",
93+
"> ⚠️ Important:\n",
94+
"You must update the code below to match your Azure authentication method.\n",
95+
"Look for the `# IMPORTANT` comments and modify those sections accordingly.\n",
96+
"If you skip this step, the sample may not run correctly."
9297
]
9398
},
9499
{
@@ -108,7 +113,10 @@
108113
"load_dotenv(find_dotenv())\n",
109114
"logging.basicConfig(level=logging.INFO)\n",
110115
"\n",
116+
"# For authentication, you can use either token-based auth or subscription key, and only one of them is required\n",
111117
"AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n",
118+
"# IMPORTANT: Replace with your actual subscription key or set up in \".env\" file if not using token auth\n",
119+
"AZURE_AI_API_KEY = os.getenv(\"AZURE_AI_API_KEY\")\n",
112120
"AZURE_AI_API_VERSION = os.getenv(\"AZURE_AI_API_VERSION\", \"2025-05-01-preview\")\n",
113121
"\n",
114122
"# Add the parent directory to the path to use shared modules\n",
@@ -122,7 +130,10 @@
122130
"client = AzureContentUnderstandingClient(\n",
123131
" endpoint=AZURE_AI_ENDPOINT,\n",
124132
" api_version=AZURE_AI_API_VERSION,\n",
133+
" # IMPORTANT: Comment out token_provider if using subscription key\n",
125134
" token_provider=token_provider,\n",
135+
" # IMPORTANT: Uncomment this if using subscription key\n",
136+
" # subscription_key=AZURE_AI_API_KEY,\n",
126137
" # x_ms_useragent=\"azure-ai-content-understanding-python/field_extraction\", # This header is used for sample usage telemetry, please comment out this line if you want to opt out.\n",
127138
")"
128139
]

notebooks/field_extraction.ipynb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,12 @@
9898
"source": [
9999
"## Create Azure AI Content Understanding Client\n",
100100
"\n",
101-
"> 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"
101+
"> 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. Fill the constant **AZURE_AI_ENDPOINT**, **AZURE_AI_API_VERSION**, **AZURE_AI_API_KEY** with the information from your Azure AI Service.\n",
102+
"\n",
103+
"> ⚠️ Important:\n",
104+
"You must update the code below to match your Azure authentication method.\n",
105+
"Look for the `# IMPORTANT` comments and modify those sections accordingly.\n",
106+
"If you skip this step, the sample may not run correctly."
102107
]
103108
},
104109
{
@@ -118,7 +123,10 @@
118123
"load_dotenv(find_dotenv())\n",
119124
"logging.basicConfig(level=logging.INFO)\n",
120125
"\n",
126+
"# For authentication, you can use either token-based auth or subscription key, and only one of them is required\n",
121127
"AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n",
128+
"# IMPORTANT: Replace with your actual subscription key or set up in \".env\" file if not using token auth\n",
129+
"AZURE_AI_API_KEY = os.getenv(\"AZURE_AI_API_KEY\")\n",
122130
"AZURE_AI_API_VERSION = os.getenv(\"AZURE_AI_API_VERSION\", \"2025-05-01-preview\")\n",
123131
"\n",
124132
"# Add the parent directory to the path to use shared modules\n",
@@ -132,7 +140,10 @@
132140
"client = AzureContentUnderstandingClient(\n",
133141
" endpoint=AZURE_AI_ENDPOINT,\n",
134142
" api_version=AZURE_AI_API_VERSION,\n",
143+
" # IMPORTANT: Comment out token_provider if using subscription key\n",
135144
" token_provider=token_provider,\n",
145+
" # IMPORTANT: Uncomment this if using subscription key\n",
146+
" # subscription_key=AZURE_AI_API_KEY,\n",
136147
" # x_ms_useragent=\"azure-ai-content-understanding-python/field_extraction\", # This header is used for sample usage telemetry, please comment out this line if you want to opt out.\n",
137148
")"
138149
]

notebooks/management.ipynb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,12 @@
3838
"source": [
3939
"## Create Azure AI Content Understanding Client\n",
4040
"\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"
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. Fill the constant **AZURE_AI_ENDPOINT**, **AZURE_AI_API_VERSION**, **AZURE_AI_API_KEY** with the information from your Azure AI Service.\n",
42+
"\n",
43+
"> ⚠️ Important:\n",
44+
"You must update the code below to match your Azure authentication method.\n",
45+
"Look for the `# IMPORTANT` comments and modify those sections accordingly.\n",
46+
"If you skip this step, the sample may not run correctly."
4247
]
4348
},
4449
{
@@ -58,7 +63,10 @@
5863
"load_dotenv(find_dotenv())\n",
5964
"logging.basicConfig(level=logging.INFO)\n",
6065
"\n",
66+
"# For authentication, you can use either token-based auth or subscription key, and only one of them is required\n",
6167
"AZURE_AI_ENDPOINT = os.getenv(\"AZURE_AI_ENDPOINT\")\n",
68+
"# IMPORTANT: Replace with your actual subscription key or set up in \".env\" file if not using token auth\n",
69+
"AZURE_AI_API_KEY = os.getenv(\"AZURE_AI_API_KEY\")\n",
6270
"AZURE_AI_API_VERSION = os.getenv(\"AZURE_AI_API_VERSION\", \"2025-05-01-preview\")\n",
6371
"\n",
6472
"# Add the parent directory to the path to use shared modules \n",
@@ -72,7 +80,10 @@
7280
"client = AzureContentUnderstandingClient(\n",
7381
" endpoint=AZURE_AI_ENDPOINT,\n",
7482
" api_version=AZURE_AI_API_VERSION,\n",
83+
" # IMPORTANT: Comment out token_provider if using subscription key\n",
7584
" token_provider=token_provider,\n",
85+
" # IMPORTANT: Uncomment this if using subscription key\n",
86+
" # subscription_key=AZURE_AI_API_KEY,\n",
7687
" x_ms_useragent=\"azure-ai-content-understanding-python/analyzer_management\", # This header is used for sample usage telemetry, please comment out this line if you want to opt out.\n",
7788
")"
7889
]

0 commit comments

Comments
 (0)