File: ml_samples_genAI_monitors_configuration.py

package info (click to toggle)
python-azure 20250603%2Bgit-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 851,724 kB
  • sloc: python: 7,362,925; ansic: 804; javascript: 287; makefile: 195; sh: 145; xml: 109
file content (139 lines) | stat: -rw-r--r-- 5,699 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# coding: utf-8

# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------

"""
FILE: ml_samples_genAI_monitors_configuration.py
DESCRIPTION:
    These samples demonstrate how to set up monitors in GenAI
USAGE:
    python ml_samples_genAI_monitors_configuration.py

"""
import os
from azure.ai.ml import MLClient
from azure.ai.ml.entities import (
    MonitorSchedule,
    CronTrigger,
    MonitorDefinition,
    ServerlessSparkCompute,
    MonitoringTarget,
    GenerationTokenStatisticsSignal,
    GenerationTokenStatisticsMonitorMetricThreshold,
    GenerationSafetyQualitySignal,
    GenerationSafetyQualityMonitoringMetricThreshold,
    BaselineDataRange,
    AlertNotification,
    LlmData,
)
from azure.ai.ml.entities._inputs_outputs import Input

from azure.ai.ml.constants import MonitorTargetTasks, MonitorDatasetContext

# Authentication package
from azure.identity import DefaultAzureCredential

credential = DefaultAzureCredential()

subscription_id = os.environ["AZURE_SUBSCRIPTION_ID"]
resource_group = os.environ["RESOURCE_GROUP_NAME"]
workspace_name = "test-ws1"
aoai_connection_name = "INSERT_YOUR_AOAI_CONNECTION_NAME"
aoai_deployment_name = "INSERT_YOUR_AOAI_DEPLOYMENT_NAME"
endpoint_name = "INSERT_YOUR_ENDPOINT_NAME"
deployment_name = "INSERT_YOUR_DEPLOYMENT_NAME"
app_trace_name = "app_traces"
app_trace_Version = "1"

# Default Monitor configuration for GenAI apps - Enable monitoring with minimal configurations
ml_client = MLClient(
    credential=credential,
    subscription_id=subscription_id,
    resource_group_name=resource_group,
    workspace_name=workspace_name,
)


class GenAIMonitoringSamples(object):
    def ml_gen_ai_monitor_default(self):
        # [START default_monitoring]
        spark_compute = ServerlessSparkCompute(instance_type="standard_e4s_v3", runtime_version="3.4")
        monitoring_target = MonitoringTarget(
            ml_task=MonitorTargetTasks.QUESTION_ANSWERING,
            endpoint_deployment_id=f"azureml:{endpoint_name}:{deployment_name}",
        )
        monitoring_target = MonitoringTarget(
            ml_task=MonitorTargetTasks.QUESTION_ANSWERING,
            endpoint_deployment_id=f"azureml:{endpoint_name}:{deployment_name}",
        )
        monitor_settings = MonitorDefinition(compute=spark_compute, monitoring_target=monitoring_target)
        model_monitor = MonitorSchedule(
            name="qa_model_monitor", trigger=CronTrigger(expression="15 10 * * *"), create_monitor=monitor_settings
        )
        ml_client.schedules.begin_create_or_update(model_monitor)
        # [END default_monitoring]

    def ml_gen_ai_monitor_advance(self):
        # [START advance_monitoring]
        spark_compute = ServerlessSparkCompute(instance_type="standard_e4s_v3", runtime_version="3.4")
        monitoring_target = MonitoringTarget(
            ml_task=MonitorTargetTasks.QUESTION_ANSWERING,
            endpoint_deployment_id=f"azureml:{endpoint_name}:{deployment_name}",
        )
        token_statistics_signal = GenerationTokenStatisticsSignal()
        generation_quality_thresholds = GenerationSafetyQualityMonitoringMetricThreshold(
            fluency={"acceptable_fluency_score_per_instance": 4, "aggregated_fluency_pass_rate": 0.5},
            coherence={"acceptable_coherence_score_per_instance": 4, "aggregated_coherence_pass_rate": 0.5},
        )
        input_data = Input(
            type="uri_folder",
            path=f"{endpoint_name}-{deployment_name}-{app_trace_name}:{app_trace_Version}",
        )
        data_window = BaselineDataRange(lookback_window_size="P7D", lookback_window_offset="P0D")
        production_data = LlmData(
            data_column_names={"prompt_column": "question", "completion_column": "answer"},
            input_data=input_data,
            data_window=data_window,
        )

        generation_quality_signal = GenerationSafetyQualitySignal(
            connection_id=f"/subscriptions/{subscription_id}/resourceGroups/{resource_group}/providers/Microsoft.MachineLearningServices/workspaces/{workspace_name}/connections/{aoai_connection_name}",
            metric_thresholds=generation_quality_thresholds,
            production_data=[production_data],
            sampling_rate=1.0,
            properties={
                "aoai_deployment_name": aoai_deployment_name,
                "enable_action_analyzer": "false",
                "azureml.modelmonitor.gsq_thresholds": '[{"metricName":"average_fluency","threshold":{"value":4}},{"metricName":"average_coherence","threshold":{"value":4}}]',
            },
        )

        monitoring_signals = {
            "token-usage-signal": token_statistics_signal,
            "gsq-signal": generation_quality_signal,
        }

        alert_notification = AlertNotification(emails=["test@example.com", "def@example.com"])

        monitor_settings = MonitorDefinition(
            compute=spark_compute,
            monitoring_target=monitoring_target,
            monitoring_signals=monitoring_signals,  # type:ignore
            alert_notification=alert_notification,
        )

        model_monitor = MonitorSchedule(
            name="monitor-name-2", trigger=CronTrigger(expression="15 10 * * *"), create_monitor=monitor_settings
        )

        ml_client.schedules.begin_create_or_update(model_monitor)


if __name__ == "__main__":
    sample = GenAIMonitoringSamples()
    sample.ml_gen_ai_monitor_default()
    sample.ml_gen_ai_monitor_advance()