Skip to content

Commit 85478fb

Browse files
ivorbCopilot
andcommitted
Add Lab D: observe, evaluate, and secure your agents
Closes the observability gap in the consolidated catalogue: there was no tracing, evaluation or safety-testing content anywhere in the repo. Instructions/Consolidated/ D landing page (generated task table from frontmatter) D0 getting started - project, Application Insights connection, agent to measure D1 trace your agent (core, L300) - OpenTelemetry + Azure Monitor, custom spans D2 evaluate answer quality (core, L300) - groundedness, relevance, similarity D3 red team your agent (optional, L400) - attack strategies + custom seed prompts Labfiles/D-observe-evaluate-and-secure-agents/ Python/ starter files with fill-in-the-blank placeholders, Solution/Python/ reference code, a ground-truth JSONL dataset, custom attack objectives, and setup/check_env.py + setup/bootstrap_agent.py so the lab stands alone without Lab B. azd template, Bicep and write_env scripts are generated from Labfiles/_shared/ via manifest.yml. Verified: Tier 0 checks (frontmatter, code blocks, links, line endings), Labfiles/_shared/sync.py --check, and the Tier 1 SDK contract check against azure-ai-projects 2.3.0 and azure-ai-evaluation 1.18.3. Not run end to end against a live Foundry project. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent b6cb2a9 commit 85478fb

43 files changed

Lines changed: 4667 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
---
2+
title: 'Observe, evaluate, and secure your agents'
3+
lab:
4+
title: 'Observe, evaluate, and secure your agents'
5+
description: 'Find out what your Tailwind Traders agent is actually doing: trace it with OpenTelemetry, score its answers against ground truth with built-in evaluators, and attack it with the AI Red Teaming Agent. A modular lab you can complete end to end or one task at a time.'
6+
type: 'lab'
7+
id: 'D'
8+
order: 4
9+
difficulty: 3
10+
duration: 60
11+
access: 'open'
12+
level: 300
13+
concepts: 'tracing, OpenTelemetry, evaluation, groundedness, AI red teaming'
14+
islab: true
15+
status: 'draft'
16+
---
17+
18+
<!--
19+
PILOT NOTE (remove before publishing):
20+
"Lab D" is new content: there was no observability, evaluation or safety-testing
21+
material anywhere in this repo. It follows the same template as Labs A-C.
22+
Starter code lives in a single folder — Labfiles/D-observe-evaluate-and-secure-agents/Python/ —
23+
shared by every task (one virtual environment, one .env). The completed reference code is
24+
in Labfiles/D-observe-evaluate-and-secure-agents/Solution/Python/.
25+
26+
This landing page is the lab overview. Setup lives in D0-getting-started.md and each task is
27+
its own page (D1-D3) so it can be completed on its own. The azd template and Bicep are
28+
generated from Labfiles/_shared/ — edit them there, not in the lab folder.
29+
-->
30+
31+
# Observe, evaluate, and secure your agents
32+
33+
**Level** ▰▰▰▱▱ **L300** (**L100** beginner → **L500** expert)
34+
35+
You can build an agent in an afternoon. Knowing whether it's any good — and whether it
36+
behaves when someone tries to make it misbehave — is a different job. This lab is about
37+
that job: seeing inside a running agent, measuring the quality of its answers, and
38+
attacking it before someone else does.
39+
40+
![Anton](../Media/anton-avatar.png)<br /><strong>Meet Anton, your AI guide.</strong><br />You'll spot **Ask Anton** tips throughout this lab. Want more interactive, hands-on help? Chat with Anton in the *[Ask Anton](https://aka.ms/choose-anton)* app.
41+
42+
<details>
43+
<summary><strong><i>About the Ask Anton app</i></strong></summary>
44+
45+
<strong><i><a href="https://aka.ms/choose-anton" target="_blank">Ask Anton</a></i></strong> is a generative AI agent that can answer questions about AI concepts and Microsoft Foundry technologies. It's available in two versions at <code>https://aka.ms/choose-anton</code>:
46+
<ul>
47+
<li><strong>Azure-based</strong>: Best experience <i>(requires an Azure subscription and deployment of a model in a Foundry project)</i>.</li>
48+
<li><strong>Browser-based</strong>: Use a small language model in your browser <i>(reduced functionality - may be slow or work only in "basic" mode in older/lower-spec devices)</i>.</li>
49+
</ul>
50+
<blockquote><i>Ask Anton is <u>not</u> a supported Microsoft product or a component of Microsoft Learn or AI Skills Navigator.</i></blockquote>
51+
</details>
52+
53+
<style>
54+
/* "Ask Anton" just-in-time concept blocks */
55+
details.concept { margin:.6rem 0 1rem; }
56+
details.concept > summary { display:inline-block; cursor:pointer; list-style:none;
57+
font-size:.85em; font-weight:600; color:#6b4ba1; background:#6b4ba112;
58+
border:1px solid #6b4ba133; border-radius:999px; padding:.2em .7em; }
59+
details.concept > summary::-webkit-details-marker { display:none; }
60+
details.concept > summary::before { content:"Ask Anton: "; font-weight:700;
61+
padding-left:1.5em;
62+
background:url("../Media/anton-avatar.png") left center / 1.25em 1.25em no-repeat; }
63+
details.concept > summary:hover { background:#6b4ba1; color:#fff; border-color:#6b4ba1; }
64+
details.concept[open] > summary { border-bottom-left-radius:0; border-bottom-right-radius:0; }
65+
details.concept .concept-body { border:1px solid #6b4ba133; border-top:none;
66+
border-radius:0 8px 8px 8px; padding:.6rem .9rem; background:#6b4ba108; font-size:.95em; }
67+
</style>
68+
69+
<details markdown="1" class="concept">
70+
<summary>Why can't I just read the output?</summary>
71+
<div class="concept-body" markdown="1">
72+
73+
Because the output is the one part of an agent that looks fine when everything else isn't.
74+
An answer can be fluent and confidently wrong, or correct but produced by three retries and
75+
a tool call that timed out. **Tracing** shows you what happened on the way to the answer.
76+
**Evaluation** scores the answer against something you already know to be true.
77+
**Red teaming** tells you what the agent does when the question is hostile.
78+
79+
</div>
80+
</details>
81+
82+
**Your scenario:** you work at **Tailwind Traders**, an outdoor-gear retailer that also runs
83+
guided trips. The staff assistant you built in earlier labs is now answering real questions
84+
on the shop floor — and store managers are asking harder questions about it. *Why was that
85+
answer slow? Is it making things up about the returns policy? What happens if a customer
86+
tries to talk it into something it shouldn't say?* In this lab you answer all three with
87+
evidence rather than opinion.
88+
89+
You'll start with the **Core** tasks, which get you from "it runs" to "I can prove how well
90+
it runs". The **Optional** task then goes after safety.
91+
92+
> **Note**: Some of the technologies used in this exercise are in preview or in active
93+
> development. You may experience some unexpected behavior, warnings, or errors.
94+
95+
## What you'll learn
96+
97+
By completing the **Core** tasks of this exercise, you'll be able to:
98+
99+
- **Trace an agent** with OpenTelemetry, export the traces to Azure Monitor, and read them
100+
in the Foundry portal — including custom spans you add around your own code.
101+
- **Evaluate answer quality** against ground truth using built-in evaluators
102+
(groundedness, relevance, similarity) and a JSONL dataset.
103+
104+
The **Optional** task lets you additionally:
105+
106+
- **Red team your agent** with the AI Red Teaming Agent: run adversarial attack strategies
107+
and your own seed prompts against a deployed agent, and read the attack success rate.
108+
109+
## How this lab is organized
110+
111+
This lab is **modular**. Each task is written to be completed **on its own, starting fresh**
112+
so you can pick a single task and do just that one. Every task also shares one starter folder,
113+
one virtual environment, and one `.env`, so if you'd rather work straight through, you can.
114+
115+
1. **Start with [Getting started](D0-getting-started.md)** — create your Microsoft Foundry
116+
project, connect Application Insights, get the starter code, and set up your `.env`. Every
117+
task begins from here; if you're doing the whole lab in one sitting, you only need to do
118+
this once.
119+
2. **Do any task.** Each task lists the setup it needs so you can start it independently. If
120+
you're moving straight from the previous task, a short *"Continuing from a previous task?"*
121+
note at the top lets you skip the repeated setup and keep going.
122+
123+
## Lab at a glance
124+
125+
Complete the **Core** tasks first (about **1 hour**) — they end with an agent you can see
126+
inside and a scorecard for its answers. Then add the **Optional** task if you want to test
127+
how it stands up to attack.
128+
129+
{% include lab-tasks-table.html lab='D' %}
130+
131+
**Choosing your path** — pick the tasks that fit the time you have:
132+
133+
- **Core only (~1h):** do Tasks 1–2.
134+
- **Everything (~1h 35m):** add **Task 3**, the red team scan.
135+
136+
> **One agent, three questions**: Task 1 traces an agent you create in code. Tasks 2 and 3
137+
> both point at the **grounded knowledge agent** from
138+
> [Lab B](B-integrate-agents-with-enterprise-knowledge-and-m365.md). If you haven't done Lab B,
139+
> one command creates an equivalent agent so this lab stands alone — see
140+
> [Getting started](D0-getting-started.md).
141+
142+
## Measure, don't guess
143+
144+
The three techniques in this lab answer different questions, and it's worth being clear about
145+
which is which:
146+
147+
- **Tracing** answers *"what happened?"* It's a record of one run: which spans took how long,
148+
which tools were called, what the model was sent. Use it when something is slow or broke.
149+
- **Evaluation** answers *"how good is it, on average?"* It's a score over a dataset, so it's
150+
the only one of the three that tells you whether a change made things better or worse.
151+
- **Red teaming** answers *"what can I make it do?"* It's an adversarial probe, and a clean
152+
result is a floor, not a guarantee.
153+
154+
None of them replaces the others, and all three are cheap compared to finding out from a
155+
customer.
156+
157+
## Summary
158+
159+
Across this lab you:
160+
161+
- **Instrumented an agent** with OpenTelemetry, exported traces to Application Insights, and
162+
read them — including your own custom spans — in the Foundry portal.
163+
- **Evaluated a grounded agent** against a ground-truth dataset with built-in groundedness,
164+
relevance and similarity evaluators, and got a score you can compare across changes.
165+
- (Optionally) **Red teamed the agent** with adversarial attack strategies and your own seed
166+
prompts, and read the resulting attack success rate.
167+
168+
Together these turn "the demo worked" into evidence you can show someone.
169+
170+
## Clean up
171+
172+
If you're finished, delete the resources you created to avoid unnecessary Azure costs.
173+
174+
1. In the [Azure portal](https://portal.azure.com), navigate to the resource group that contains your Foundry resource.
175+
1. On the toolbar, select **Delete resource group**, enter the resource group name, and confirm.
176+
177+
> The code you run in Task 1 deletes the agent version it creates. The agent Tasks 2 and 3
178+
> measure is removed when you delete the resource group. If you provisioned with `azd`, run
179+
> `azd down` instead — but note that Application Insights, if you created it from the Foundry
180+
> portal, is a separate resource and is deleted with the resource group rather than by `azd`.
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
---
2+
title: 'Getting started: set up your environment'
3+
lab:
4+
title: 'Getting started: set up your environment'
5+
description: 'Shared setup for the Observe, evaluate, and secure your agents lab: create a Microsoft Foundry project, connect Application Insights, get the starter code, and configure your environment. Complete this once before any task.'
6+
type: 'task'
7+
parent: 'D'
8+
order: 0
9+
section: 'setup'
10+
access: 'open'
11+
level: 300
12+
concepts: 'environment setup, Microsoft Foundry project, Application Insights'
13+
status: 'draft'
14+
---
15+
16+
# Getting started
17+
18+
This page sets up everything the **Observe, evaluate, and secure your agents** lab needs.
19+
**Every task begins here** — complete this page first. Each task is written so you can then
20+
do it on its own; if you're working through the whole lab in one sitting, you only need to
21+
do this setup once.
22+
23+
**Your scenario:** you work at **Tailwind Traders**, an outdoor-gear retailer that also runs
24+
guided trips. The staff assistant is live, and this lab is how you find out what it's really
25+
doing: tracing it, scoring its answers, and attacking it.
26+
27+
> **Note**: Some of the technologies used in this lab are in preview or in active
28+
> development. You may experience some unexpected behavior, warnings, or errors.
29+
30+
## Prerequisites
31+
32+
Before starting, ensure you have:
33+
34+
- An [Azure subscription](https://azure.microsoft.com/free/) with sufficient permissions and quota to provision Azure AI resources
35+
- [Visual Studio Code](https://code.visualstudio.com/) installed on your local machine
36+
- [Python 3.13](https://www.python.org/downloads/) installed
37+
- [Git](https://git-scm.com/downloads) installed on your local machine
38+
- Basic familiarity with Python
39+
40+
> \* Use Python **3.10–3.13**. Task 3 installs PyRIT, which doesn't support Python 3.14 yet.
41+
> The lab has been tested with Python 3.13.
42+
43+
## Create a Microsoft Foundry project
44+
45+
You need a Foundry project and a deployed model for every task. You can create these in the
46+
portal (the default), or provision them with one command using the Azure Developer CLI (`azd`).
47+
48+
### Option A — Create the project in the portal (default)
49+
50+
1. In a web browser, open the [Foundry portal](https://ai.azure.com) at `https://ai.azure.com` and sign in using your Azure credentials. Close any tips or quick start panes, and if necessary use the **Foundry** logo at the top left to navigate to the home page.
51+
52+
> **Important**: For this lab, you're using the **New** Foundry experience.
53+
54+
1. In the top banner, select **Start building**.
55+
56+
1. When prompted, create a **new** project and enter a valid name (for example, `observability-lab-project`).
57+
58+
1. Expand **Advanced options** and specify:
59+
- **Microsoft Foundry resource**: *A valid name for your Foundry resource*
60+
- **Region**: *Select one available near you*\*
61+
- **Subscription**: *Your Azure subscription*
62+
- **Resource group**: *Select or create a resource group*
63+
64+
> \* If you plan to do **Task 3** (red teaming), the AI Red Teaming Agent is only available
65+
> in **East US 2**, **France Central**, **Sweden Central**, **Switzerland West**, and
66+
> **North Central US**. Choosing one of those now saves you creating a second project later.
67+
68+
1. Select **Create** and wait for your project to be created.
69+
70+
1. On the project **Overview** page, note the **project endpoint** and the name of the model
71+
deployment that was created for you — you'll put both in your `.env`.
72+
73+
### Option B — Provision with azd (optional, one command)
74+
75+
If you'd rather not click through the portal, the lab ships an optional `azd` template that
76+
creates the Foundry resource, a project, and a model deployment for you.
77+
78+
1. Install the [Azure Developer CLI](https://learn.microsoft.com/azure/developer/azure-developer-cli/install-azd).
79+
80+
1. From the `Labfiles/D-observe-evaluate-and-secure-agents` folder, run:
81+
82+
```
83+
azd auth login
84+
azd up
85+
```
86+
87+
1. Answer the prompts (environment name, region). When it finishes, `azd` writes
88+
`PROJECT_ENDPOINT` and `MODEL_DEPLOYMENT_NAME` into `Python/.env` for you.
89+
90+
> **Note**: `azd up` does **not** create the Application Insights resource Task 1 needs —
91+
> connect that in the portal using the steps below. When you're done with the lab, run
92+
> `azd down` to delete everything it created.
93+
94+
## Connect Application Insights (needed for Task 1)
95+
96+
Foundry stores traces in an **Application Insights** resource connected to your project. Connect
97+
one now — it takes a minute, and once it's connected Foundry starts recording server-side traces
98+
for your agents without any code at all.
99+
100+
1. In the [Foundry portal](https://ai.azure.com), open your project.
101+
102+
1. In the left navigation, select **Agents**, then select **Traces** at the top.
103+
104+
1. Select **Connect**, then either pick an existing Application Insights resource or select
105+
**Create new** and complete the wizard.
106+
107+
> If you don't see the **Connect** button, select **Manage** in the upper right, then
108+
> **Project details** > **Connected resources** > **Add connection** > **Application Insights**.
109+
110+
1. To *read* the traces you'll need the **Log Analytics Reader** role on that Application
111+
Insights resource. If you created it yourself, you already have it.
112+
113+
> **Why this matters**: your Foundry project can only hand your code a connection string if
114+
> something is connected. Task 1 asks the project for that string, so this step has to happen
115+
> first.
116+
117+
## Get the starter code
118+
119+
1. In VS Code, open the Command Palette (**Ctrl+Shift+P**), run **Git: Clone**, and enter:
120+
121+
```
122+
https://github.com/MicrosoftLearning/mslearn-ai-agents.git
123+
```
124+
125+
1. Open the cloned repo, then **File > Open Folder** and select `mslearn-ai-agents/Labfiles/D-observe-evaluate-and-secure-agents/Python`. This single folder holds the starter code for **every** task in this lab — you use one virtual environment and one `.env` throughout.
126+
127+
1. Right-click **requirements.txt** and choose **Open in Integrated Terminal**. Then create a virtual environment and install packages:
128+
129+
```
130+
python -m venv labenv
131+
.\labenv\Scripts\Activate.ps1
132+
pip install -r requirements.txt
133+
```
134+
135+
> This install is larger than the other labs — it includes the evaluation SDK and, for
136+
> Task 3, PyRIT. Give it a few minutes.
137+
138+
1. Open the **.env** file and set `PROJECT_ENDPOINT` to your project endpoint and `MODEL_DEPLOYMENT_NAME` to your model deployment name. Save the file. (If you used `azd up`, these are already filled in.)
139+
140+
> **Tip**: In the Foundry Toolkit VS Code extension, right-click your project deployment and select **Copy Project Endpoint** to get the endpoint URL.
141+
142+
## Get an agent to measure (needed for Tasks 2 and 3)
143+
144+
Tasks 2 and 3 measure a **grounded** agent — one that answers from the Tailwind Traders
145+
knowledge base rather than from the model's own memory. You have two ways to get one:
146+
147+
- **You did [Lab B](B-integrate-agents-with-enterprise-knowledge-and-m365.md)**: set `AGENT_NAME`
148+
in `.env` to that agent's name (`tailwind-knowledge-agent` if you kept the default) and you're
149+
done.
150+
- **You didn't**: create an equivalent agent here. Sign in and run, from the `Python` folder with
151+
the virtual environment active:
152+
153+
```
154+
az login
155+
```
156+
157+
```
158+
python ../setup/bootstrap_agent.py
159+
```
160+
161+
This uploads the documents in `Python/knowledge/`, grounds an agent named
162+
`tailwind-knowledge-agent` on them with File Search, and writes `AGENT_NAME` into your `.env`.
163+
164+
> Task 1 doesn't need this agent — it creates and deletes its own.
165+
166+
## Check you're ready for a task
167+
168+
Each task needs specific values in your `.env`. Before starting a task, run the preflight
169+
check from the `Python` folder you opened in VS Code — it reads your `.env` and tells you
170+
what (if anything) is missing:
171+
172+
```
173+
python ../setup/check_env.py --task 1
174+
```
175+
176+
Swap `1` for the task number you're about to start.
177+
178+
> **Tip**: The preflight check uses only the Python standard library, so it's safe to run
179+
> before `pip install` and without the virtual environment active. It can't see whether
180+
> Application Insights is connected — that's a project setting, not a `.env` value — so
181+
> do the connection step above if you're starting at Task 1.
182+
183+
That's it — head to any task:
184+
185+
| Task | Page |
186+
| --- | --- |
187+
| Task 1 – Trace your agent | [D1](D1-trace-your-agent.md) |
188+
| Task 2 – Evaluate answer quality | [D2](D2-evaluate-answer-quality.md) |
189+
| Task 3 – Red team your agent | [D3](D3-red-team-your-agent.md) |

0 commit comments

Comments
 (0)