Skip to content

Commit a42f6ca

Browse files
committed
add quickstart
1 parent fb52df3 commit a42f6ca

4 files changed

Lines changed: 144 additions & 2 deletions

File tree

eval/setup-eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def get_evaluator_config_safety(evaluator_id, azure_ai_project_scope):
111111
"sexual" : get_evaluator_config_safety(SexualEvaluator.id, project_client.scope),
112112
"hateUnfairness" : get_evaluator_config_safety(HateUnfairnessEvaluator.id, project_client.scope),
113113
"protectedMaterial" : get_evaluator_config_safety(ProtectedMaterialEvaluator.id, project_client.scope),
114-
"contentSafety" : get_evaluator_config_safety(ContentSafetyEvaluator.id, project_client.scope)
114+
#"contentSafety" : get_evaluator_config_safety("azureml://registries/azureml/models/Content-Safety-Evaluator/versions/1", project_client.scope)
115115
}
116116

117117
name = SAMPLE_NAME

quickstart-dotnet.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# ASP.NET
2+
See [here](https://learn.microsoft.com/en-us/azure/azure-app-configuration/howto-variant-feature-flags-aspnet-core) for the full tutorial.
3+
4+
5+
## Install SDK via .NET CLI
6+
7+
```
8+
dotnet add package Azure.Identity
9+
dotnet add package Microsoft.Extensions.Configuration.AzureAppConfiguration
10+
dotnet add package Microsoft.FeatureManagement.AspNetCore
11+
```
12+
13+
## Initialize Application Insights
14+
15+
[Connect to Application Insights](https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core) to instrument your application.
16+
17+
```dotnet
18+
builder.Services.AddApplicationInsightsTelemetry();
19+
```
20+
21+
## Initialize App Configuration
22+
23+
[Connect to App Configuration](https://learn.microsoft.com/en-us/azure/azure-app-configuration/quickstart-aspnet-core-app?tabs=entra-id), which supplies feature flags and other configurations to your application.
24+
Use `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](https://learn.microsoft.com/en-us/azure/azure-app-configuration/concept-enable-rbac#authentication-with-token-credentials) to assign your credential the App Configuration Data Reader role.
25+
26+
27+
```dotnet
28+
builder.Configuration.AddAzureAppConfiguration(options =>
29+
{
30+
options.Connect(new Uri(endpoint), new DefaultAzureCredential());
31+
});
32+
```
33+
34+
## Setup Feature Management
35+
36+
Add Feature Management to the service collection and the targeting middleware to provide user Id for variant assignments.
37+
38+
```dotnet
39+
builder.Services.AddFeatureManagement()
40+
.WithTargeting()
41+
.AddApplicationInsightsTelemetry();
42+
43+
app.UseMiddleware<TargetingHttpContextMiddleware>();
44+
```
45+
46+
## Use Feature Flag
47+
48+
Use dependency injection to retrieve an instance of `IVariantFeatureManager` and get the feature variant assigned to the user.
49+
50+
```dotnet
51+
Variant variant = await featureManager
52+
.GetVariantAsync("<placeholder-feature-flag-name>", HttpContext.RequestAborted);
53+
```
54+
55+
## Log Events
56+
Use dependency injection to retrieve `TelemetryClient` to log custom events.
57+
58+
```dotnet
59+
telemetryClient.TrackEvent("<placeholder-event-name>");
60+
```
61+
62+
## Validate Event Logs
63+
- Run your application and trigger one or more get_variant() calls for the test feature flag.
64+
- Go to the Log Analytics workspace (add link to LA workspace/log page) and execute the following query. After a short ingestion delay, you should see the log events for the feature evaluation calls.
65+
66+
```
67+
AppEvents | where Name == "FeatureEvaluation"
68+
```

quickstart-python.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Python
2+
See [here](https://learn.microsoft.com/en-us/azure/azure-app-configuration/howto-variant-feature-flags-python) for the full tutorial.
3+
4+
## Install SDK
5+
6+
```
7+
pip install azure-identity
8+
pip install azure-appconfiguration-provider
9+
pip install featuremanagement[AzureMonitor]
10+
```
11+
12+
## Define a targeting context accessor method
13+
14+
The accessor method is called from both Feature Management and Azure Monitor to identify the user for the current request.
15+
16+
```python
17+
async def my_targeting_accessor() -> TargetingContext:
18+
user_id = request.headers.get('user-id', str(uuid.uuid4())
19+
return TargetingContext(user_id=user_id)
20+
```
21+
22+
## Initialize Application Insights
23+
24+
[Connect to Application Insights](https://learn.microsoft.com/en-us/azure/azure-monitor/app/asp-net-core) to instrument your application. Include the `TargetingSpanProcessor` to allow request and dependency data to be available for metrics.
25+
26+
```python
27+
from azure.monitor.opentelemetry import configure_azure_monitor
28+
from featuremanagement.azuremonitor import TargetingSpanProcessor
29+
30+
configure_azure_monitor(
31+
connection_string=os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING"),
32+
span_processors=[TargetingSpanProcessor(targeting_context_accessor=my_targeting_accessor)]
33+
)
34+
```
35+
36+
## Initialize App Configuration and Feature Manager
37+
38+
[Connect to App Configuration](https://learn.microsoft.com/azure/azure-app-configuration/quickstart-feature-flag-python#console-applications), which supplies feature flags and other configurations to your application. Use `DefaultAzureCredential` to authenticate to your App Configuration store. Follow the [instructions](https://learn.microsoft.com/en-us/azure/azure-app-configuration/concept-enable-rbac#authentication-with-token-credentials) to assign your credential the App Configuration Data Reader role.
39+
40+
```python
41+
from azure.appconfiguration.provider import load
42+
from azure.identity import DefaultAzureCredential
43+
from featuremanagement import FeatureManager
44+
from featuremanagement.azuremonitor import publish_telemetry, track_event
45+
46+
azure_app_config = load(endpoint="<your-endpoint>", credential=DefaultAzureCredential(), feature_flag_enabled=True, feature_flag_refresh_enabled=True)
47+
feature_manager = FeatureManager(azure_app_config, on_feature_evaluated=publish_telemetry, targeting_context_accessor=my_targeting_accessor)
48+
```
49+
50+
## Use Feature Flag
51+
52+
Use feature manager to get the feature variant assigned to the user.
53+
54+
```python
55+
my_feature_flag = feature_manager.get_variant("<placeholder-feature-flag-name>"):
56+
if my_feature_flag:
57+
variant_name = my_feature_flag.name
58+
variant_value = my_feature_flag.configuration
59+
# do something with the variant value
60+
```
61+
62+
## Log event
63+
```
64+
# The user_id from the targeting context needs to be manually added for now due to an missing Azure Monitor support.
65+
track_event("<placeholder-event-name>", my_targeting_accessor().user_id)
66+
```
67+
68+
## Validate Event Logs
69+
- Run your application and trigger one or more get_variant() calls for the test feature flag.
70+
- Go to the Log Analytics workspace (add link to LA workspace/log page) and execute the following query. After a short ingestion delay, you should see the log events for the feature evaluation calls.
71+
72+
```
73+
AppEvents | where Name == "FeatureEvaluation"
74+
```

src/api/test_ai_eval.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def get_prompt_template_variants():
3333
def test_simulate_traffic():
3434
app = create_app()
3535
with TestClient(app) as client:
36-
for i in range(20):
36+
for i in range(1):
3737
for data in get_eval_data_set():
3838
query = data["query"]
3939
response = client.post("/chat", json={

0 commit comments

Comments
 (0)