Skip to content

Commit b373d57

Browse files
changjian-wangChangjian WangCopilotwangchangjian1130
authored
Update setup script and skills for Azure AI Content Understanding (#46512)
* Add setup script and GitHub Copilot skills for Azure AI Content Understanding SDK - Created setup_user_env.sh for environment setup, including virtual environment creation, SDK installation, and .env configuration. - Added sdk-common-knowledge skill for domain knowledge reference on Content Understanding concepts and SDK usage. - Introduced sdk-py-sample-run skill to guide users in running specific samples interactively. - Implemented run_sample.sh script to facilitate running sync and async samples with user prompts and error handling. - Updated README.md to include information about new skills and usage instructions for GitHub Copilot. * Update REST API Reference link in SKILL.md * Rename skill from sdk-py-setup to cu-sdk-setup and update script path in SKILL.md * Update SKILL.md and run_sample.sh to reference cu-sdk-setup instead of sdk-py-setup * Update SKILL.md files to enhance clarity and detail for SDK setup and usage Co-authored-by: Copilot <copilot@github.com> * Update SKILL.md to remove outdated sections and add related skills for improved guidance Co-authored-by: Copilot <copilot@github.com> * Add SKILL.md files for cu-sdk-common-knowledge and cu-sdk-py-sample-run with detailed guidance and sample execution instructions Co-authored-by: Copilot <copilot@github.com> * Update skill references in README.md for consistency and clarity * Add cu-sdk-sample-run skill and update related documentation - Introduced `cu-sdk-sample-run` skill to guide users in running specific samples interactively. - Updated `SKILL.md` for `cu-sdk-common-knowledge` and `cu-sdk-setup` to include references to the new skill. - Corrected skill name in README and adjusted example prompts accordingly. Co-authored-by: Copilot <copilot@github.com> * Enhance setup scripts for Azure AI Content Understanding SDK with Python version checks and model deployment probing Co-authored-by: Copilot <copilot@github.com> * Enhance Python environment setup guidance for Windows users in SKILL.md and setup_user_env.ps1 Co-authored-by: Copilot <copilot@github.com> * Enhance Python installation prompts and dependency checks in setup scripts for Azure AI Content Understanding SDK Co-authored-by: Copilot <copilot@github.com> * Soften runtime wording for sample_analyze_url.py per PR feedback * Add inline cspell:ignore directive in setup_user_env.sh for pyver/esac --------- Co-authored-by: Changjian Wang <v-changjwang@microsoft.com> Co-authored-by: Copilot <copilot@github.com> Co-authored-by: aluneth <wangchangjian1130@163.com>
1 parent 8ee83e6 commit b373d57

3 files changed

Lines changed: 931 additions & 43 deletions

File tree

sdk/contentunderstanding/azure-ai-contentunderstanding/.github/skills/cu-sdk-setup/SKILL.md

Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,55 @@ Before starting, ensure you have:
1717
- An **Azure subscription** ([create one for free](https://azure.microsoft.com/free/))
1818
- A **Microsoft Foundry resource** in a [supported region](https://learn.microsoft.com/azure/ai-services/content-understanding/language-region-support)
1919

20+
> **[COPILOT] Probe Python runtime first (before asking):**
21+
> Do not take the user's word for it — run these checks, then report. This prevents silent failures later in `python -m venv`.
22+
>
23+
> ```bash
24+
> # POSIX / WSL / macOS
25+
> python3 --version 2>/dev/null || python --version 2>/dev/null
26+
> python3 -m pip --version 2>/dev/null || python -m pip --version 2>/dev/null
27+
> python3 -c "import venv" 2>/dev/null && echo "venv: ok" || echo "venv: MISSING"
28+
> ```
29+
>
30+
> ```powershell
31+
> # Windows PowerShell
32+
> py -3 --version 2>$null; if ($LASTEXITCODE -ne 0) { python --version }
33+
> py -3 -m pip --version 2>$null
34+
> py -3 -c "import venv" 2>$null; if ($LASTEXITCODE -eq 0) { 'venv: ok' } else { 'venv: MISSING' }
35+
> ```
36+
>
37+
> **Decision table:**
38+
>
39+
> | Finding | Action |
40+
> |---|---|
41+
> | `Python 3.9+` and `venv: ok` and `pip` present | ✓ Good to go. Proceed to Step 1. |
42+
> | Python missing or `< 3.9` | Report the finding, then go to the **[ASK USER] Python install choice** block below. |
43+
> | `venv: MISSING` (common on Debian/Ubuntu base images) | Report the finding, then go to the **[ASK USER] Python install choice** block below. |
44+
> | Windows resolves to a `WindowsApps\python*.exe` stub | Report the finding, then go to the **[ASK USER] Python install choice** block below (auto-install cannot fix this — user must disable App Execution Aliases). |
45+
>
46+
> **[ASK USER] Python install choice (only when probe fails):**
47+
> Ask the user: "Python is missing / too old / the `venv` module is unavailable. How would you like to proceed?"
48+
> - **Option A: Install it for me** — Agent runs the platform-appropriate install command (see below), verifies, and continues. Requires elevated privileges on Linux (`sudo`). Not available for the Windows App Execution Alias case — that requires manual UI action.
49+
> - **Option B: I'll install it myself** — Agent prints the install command for the user's platform and stops. User runs it, re-opens the terminal, and tells the agent to resume.
50+
>
51+
> **Default install commands (Option A):**
52+
> - **macOS**`brew install python@3.12` (requires Homebrew; if not installed, fall back to Option B)
53+
> - **Debian / Ubuntu / WSL**`sudo apt update && sudo apt install -y python3.12 python3.12-venv` (will prompt for sudo password)
54+
> - **Debian / Ubuntu — `venv: MISSING` only**`sudo apt install -y python3-venv` (matches the existing Python version)
55+
> - **Windows**`winget install Python.Python.3.12` (run in an elevated PowerShell if needed)
56+
>
57+
> **Before running Option A, confirm with the user one more time** by restating the exact command that will execute, then proceed. After install, re-run the probe to verify `Python 3.9+` and `venv: ok` before continuing.
58+
>
59+
> **Windows gotcha — App Execution Aliases:** Even after uninstalling the Store Python stub, the `python.exe` / `python3.exe` entries under `C:\Users\<you>\AppData\Local\Microsoft\WindowsApps\` may persist as **App Execution Aliases** and shadow a real install. If the probe still reports a stub after the user confirms they installed real Python:
60+
> 1. Point them to **Settings → Apps → Advanced app settings → App execution aliases**, and toggle **off** `python.exe` and `python3.exe`.
61+
> 2. As a fallback, suggest invoking Python via the `py` launcher (`py -3 -m venv .venv`) or the explicit install path (e.g. `%LOCALAPPDATA%\Programs\Python\Python312\python.exe`). The companion `setup_user_env.ps1` script already probes well-known install paths automatically.
62+
>
63+
> Report the detected version + venv status back to the user in one sentence before the `[ASK USER]` block below.
64+
2065
> **[ASK USER] Prerequisites Check:**
21-
> Before proceeding, ask the user to confirm their prerequisites:
22-
> 1. "Do you have **Python 3.9+** installed?" — If no, guide them to install Python first.
23-
> 2. "Do you already have a **Microsoft Foundry resource** set up in Azure?" — If no, jump to **Step 5** (Azure Resource Setup) first, then return here.
24-
> 3. "Have you already deployed the required **AI models** (GPT-4.1, GPT-4.1-mini, text-embedding-3-large) in Microsoft Foundry?" — If no, include Step 5.3 and Step 6 in the workflow.
66+
> After the probe above, confirm the remaining items:
67+
> 1. "Do you already have a **Microsoft Foundry resource** set up in Azure?" — If no, jump to **Step 5** (Azure Resource Setup) first, then return here.
68+
> 2. "Have you already deployed the required **AI models** (GPT-4.1, GPT-4.1-mini, text-embedding-3-large) in Microsoft Foundry?" — If no, include Step 5.3 and Step 6 in the workflow.
2569
2670
## Package Directory
2771
@@ -59,6 +103,9 @@ else
59103
fi
60104
```
61105
106+
> **[COPILOT] Existing `.venv` behavior:**
107+
> If `.venv` already exists, prefer reusing it. If the existing virtual environment was created with a different Python minor version than the interpreter selected in the prerequisite probe, or if the environment is incomplete/corrupted, recreate `.venv` before continuing. This avoids subtle failures when the machine later upgrades from one supported Python version to another (for example, 3.9 → 3.12).
108+
62109
**Activate virtual environment:**
63110
64111
| Platform | Command |
@@ -97,6 +144,9 @@ pip install -e .
97144
pip install -r dev_requirements.txt
98145
```
99146
147+
> **[COPILOT] Repeated-run behavior:**
148+
> On repeated runs, if the required SDK/sample dependencies are already importable from the active virtual environment, the setup scripts may skip the `pip install` step instead of reinstalling everything. Only rerun the install commands when dependencies are missing or the virtual environment was recreated.
149+
100150
This also installs all dependencies needed to run samples:
101151
- `aiohttp` - Required for async operations
102152
- `python-dotenv` - For loading `.env` files
@@ -167,13 +217,55 @@ Open `.env` in your editor and set the following **required** variables:
167217
168218
**For running `sample_update_defaults.py` (one-time model configuration):**
169219
170-
> **[ASK USER] Model deployment names:**
171-
> Ask the user: "Do you want to configure **model deployment names** now? These are needed for `sample_update_defaults.py` (one-time setup)."
172-
> - If yes, ask for each deployment name one by one with sensible defaults:
220+
> **[COPILOT] Probe existing model defaults on the Foundry resource:**
221+
> Before asking the user for deployment names, probe what the resource already has configured. The venv is active and the SDK is installed, so call `get_defaults()` via a short inline Python snippet. Export `CONTENTUNDERSTANDING_ENDPOINT` (and `CONTENTUNDERSTANDING_KEY` if Option A) in the shell first so the snippet can read them.
222+
>
223+
> ```bash
224+
> python - <<'PY'
225+
> import os, sys
226+
> from azure.ai.contentunderstanding import ContentUnderstandingClient
227+
> from azure.core.credentials import AzureKeyCredential
228+
> from azure.identity import DefaultAzureCredential
229+
> from azure.core.exceptions import HttpResponseError, ClientAuthenticationError
230+
>
231+
> ep = os.environ["CONTENTUNDERSTANDING_ENDPOINT"]
232+
> key = os.environ.get("CONTENTUNDERSTANDING_KEY") or None
233+
> cred = AzureKeyCredential(key) if key else DefaultAzureCredential()
234+
> try:
235+
> d = ContentUnderstandingClient(ep, cred).get_defaults().model_deployments or {}
236+
> except ClientAuthenticationError:
237+
> sys.exit(3)
238+
> except HttpResponseError as e:
239+
> sys.exit(3 if e.status_code in (401, 403) else 1)
240+
> except Exception:
241+
> sys.exit(1)
242+
>
243+
> keys = ["gpt-4.1", "gpt-4.1-mini", "text-embedding-3-large"]
244+
> vals = [d.get(k, "") for k in keys]
245+
> print(";".join(f"{k}={v}" for k, v in zip(keys, vals)))
246+
> sys.exit(0 if all(vals) else (2 if not any(vals) else 10))
247+
> PY
248+
> ```
249+
>
250+
> Branch on the exit code:
251+
>
252+
> | Exit | Meaning | Action |
253+
> |------|---------|--------|
254+
> | `0` | **ALL_SET** — all 3 deployments already mapped on the resource | Show the detected values and ask *"Detected existing defaults: gpt-4.1=`<A>`, gpt-4.1-mini=`<B>`, text-embedding-3-large=`<C>`. Use these? (Y/n)"*. On Y, prefill the 3 env vars and **skip Step 6** (defaults already configured). On n, fall through to the per-model prompts below. |
255+
> | `10` | **PARTIAL** — some mapped, some missing | Prefill the ones that are set. For missing models, ask per-item with the default shown below. After Step 4 completes, run Step 6 to fill the gaps. |
256+
> | `2` | **NONE** — resource has no defaults yet | Fall through to the per-model prompts below. Step 6 will configure them. |
257+
> | `3` | **AUTH_ERROR** (401/403) | Print a one-line warning: *"Probe unavailable (auth failed). If you're using DefaultAzureCredential, run `az login` and ensure the Cognitive Services User role is assigned. Continuing with manual entry."* Fall through to per-model prompts. |
258+
> | other | Unexpected error | Print *"Probe failed: `<error>`. Continuing with manual entry."* Fall through. |
259+
>
260+
> Only proceed to the per-model prompts below when the probe outcome requires it.
261+
262+
> **[ASK USER] Model deployment names (only when probe did not yield all values):**
263+
> For each model not already prefilled from the probe, ask with a sensible default:
173264
> - "What is your **GPT-4.1** deployment name?" (default: `gpt-4.1`)
174265
> - "What is your **GPT-4.1-mini** deployment name?" (default: `gpt-4.1-mini`)
175266
> - "What is your **text-embedding-3-large** deployment name?" (default: `text-embedding-3-large`)
176-
> - If no, let them know they can configure these later before running `sample_update_defaults.py`.
267+
>
268+
> If the user prefers to configure these later, let them know they can run `sample_update_defaults.py` (Step 6) anytime before using prebuilt analyzers.
177269
178270
| Variable | Description | How to Get It |
179271
|----------|-------------|---------------|
@@ -203,7 +295,7 @@ Open `.env` in your editor and set the following **required** variables:
203295
CONTENTUNDERSTANDING_ENDPOINT=https://my-foundry-resource.services.ai.azure.com/
204296
205297
# Optional: API key (if not set, DefaultAzureCredential is used)
206-
CONTENTUNDERSTANDING_KEY=abc123...
298+
CONTENTUNDERSTANDING_KEY=
207299
208300
# Required for sample_update_defaults.py (model configuration)
209301
GPT_4_1_DEPLOYMENT=gpt-4.1
@@ -261,6 +353,9 @@ This role is required even if you own the resource:
261353
262354
### Step 6: Configure Model Defaults (One-Time Setup)
263355
356+
> **[COPILOT] Skip condition:**
357+
> If the Step 4.2 probe returned **ALL_SET** and the user accepted the detected values, defaults are already configured on the Foundry resource — skip this step and tell the user *"Your Foundry resource already has model defaults configured; skipping Step 6."* Otherwise continue below.
358+
264359
> **[ASK USER] Run model defaults?:**
265360
> Ask: "Would you like to run `sample_update_defaults.py` now to configure model defaults? This is a **one-time setup** per Microsoft Foundry resource. (Yes / Skip for now)"
266361
> - If yes, ensure deployment name env vars are set, then run the script.
@@ -279,13 +374,15 @@ This is a **one-time setup per Microsoft Foundry resource**.
279374
280375
> **[ASK USER] Which samples?:**
281376
> Ask: "Which sample would you like to run first?" with options:
282-
> - `sample_analyze_url.py` — Analyze content from a URL (recommended start)
283-
> - `sample_analyze_binary.py`Analyze a local file
377+
> - `sample_analyze_binary.py` — Analyze a local PDF (quickest; completes in under a minute)
378+
> - `sample_analyze_url.py` — Full demo: document + video + audio + image from URLs (runs several analyses; takes a few minutes, please be patient)
284379
> - `sample_analyze_invoice.py` — Extract invoice fields
285380
> - Other — Let me see the full list
286381
> - Skip — I'll run samples on my own later
287382
>
288383
> If the user picks "Other", list available samples from the `samples/` directory.
384+
>
385+
> **[COPILOT] Timing note (do not parrot verbatim to user):** `sample_analyze_url.py` runs 14 sequential LROs (document + video + audio + image, with multiple content-range variants). Video/audio chapter generation is slow on the service side, so total runtime can be on the order of 15+ minutes today. Do not interpret quiet periods (no stdout for several minutes during a video/audio LRO) as a hang. Only consider killing if there is **no new stdout for 5+ minutes** AND no active HTTP traffic. When talking to the user, prefer phrasing like "takes a few minutes" or "please be patient" rather than citing exact large minute counts.
289386
290387
#### Sync Samples
291388

0 commit comments

Comments
 (0)