azure-ai-evaluation 1.0.0b4__py3-none-any.whl → 1.0.0b5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of azure-ai-evaluation might be problematic. Click here for more details.
- azure/ai/evaluation/__init__.py +22 -0
- azure/ai/evaluation/_common/constants.py +5 -0
- azure/ai/evaluation/_common/math.py +11 -0
- azure/ai/evaluation/_common/rai_service.py +172 -35
- azure/ai/evaluation/_common/utils.py +162 -23
- azure/ai/evaluation/_constants.py +6 -6
- azure/ai/evaluation/_evaluate/{_batch_run_client → _batch_run}/__init__.py +3 -2
- azure/ai/evaluation/_evaluate/{_batch_run_client/batch_run_context.py → _batch_run/eval_run_context.py} +4 -4
- azure/ai/evaluation/_evaluate/{_batch_run_client → _batch_run}/proxy_client.py +6 -3
- azure/ai/evaluation/_evaluate/_batch_run/target_run_context.py +35 -0
- azure/ai/evaluation/_evaluate/_eval_run.py +21 -4
- azure/ai/evaluation/_evaluate/_evaluate.py +267 -139
- azure/ai/evaluation/_evaluate/_telemetry/__init__.py +5 -5
- azure/ai/evaluation/_evaluate/_utils.py +40 -7
- azure/ai/evaluation/_evaluators/_bleu/_bleu.py +1 -1
- azure/ai/evaluation/_evaluators/_coherence/_coherence.py +14 -9
- azure/ai/evaluation/_evaluators/_coherence/coherence.prompty +76 -34
- azure/ai/evaluation/_evaluators/_common/_base_eval.py +20 -19
- azure/ai/evaluation/_evaluators/_common/_base_prompty_eval.py +18 -8
- azure/ai/evaluation/_evaluators/_common/_base_rai_svc_eval.py +48 -9
- azure/ai/evaluation/_evaluators/_content_safety/_content_safety.py +56 -19
- azure/ai/evaluation/_evaluators/_content_safety/_content_safety_chat.py +5 -5
- azure/ai/evaluation/_evaluators/_content_safety/_hate_unfairness.py +30 -1
- azure/ai/evaluation/_evaluators/_content_safety/_self_harm.py +30 -1
- azure/ai/evaluation/_evaluators/_content_safety/_sexual.py +30 -1
- azure/ai/evaluation/_evaluators/_content_safety/_violence.py +30 -1
- azure/ai/evaluation/_evaluators/_eci/_eci.py +3 -1
- azure/ai/evaluation/_evaluators/_fluency/_fluency.py +20 -20
- azure/ai/evaluation/_evaluators/_fluency/fluency.prompty +66 -36
- azure/ai/evaluation/_evaluators/_gleu/_gleu.py +1 -1
- azure/ai/evaluation/_evaluators/_groundedness/_groundedness.py +49 -15
- azure/ai/evaluation/_evaluators/_groundedness/groundedness_with_query.prompty +113 -0
- azure/ai/evaluation/_evaluators/_groundedness/groundedness_without_query.prompty +99 -0
- azure/ai/evaluation/_evaluators/_meteor/_meteor.py +3 -7
- azure/ai/evaluation/_evaluators/_multimodal/__init__.py +20 -0
- azure/ai/evaluation/_evaluators/_multimodal/_content_safety_multimodal.py +130 -0
- azure/ai/evaluation/_evaluators/_multimodal/_content_safety_multimodal_base.py +57 -0
- azure/ai/evaluation/_evaluators/_multimodal/_hate_unfairness.py +96 -0
- azure/ai/evaluation/_evaluators/_multimodal/_protected_material.py +120 -0
- azure/ai/evaluation/_evaluators/_multimodal/_self_harm.py +96 -0
- azure/ai/evaluation/_evaluators/_multimodal/_sexual.py +96 -0
- azure/ai/evaluation/_evaluators/_multimodal/_violence.py +96 -0
- azure/ai/evaluation/_evaluators/_protected_material/_protected_material.py +44 -11
- azure/ai/evaluation/_evaluators/_qa/_qa.py +7 -3
- azure/ai/evaluation/_evaluators/_relevance/_relevance.py +21 -19
- azure/ai/evaluation/_evaluators/_relevance/relevance.prompty +78 -42
- azure/ai/evaluation/_evaluators/_retrieval/_retrieval.py +125 -82
- azure/ai/evaluation/_evaluators/_retrieval/retrieval.prompty +74 -24
- azure/ai/evaluation/_evaluators/_rouge/_rouge.py +2 -2
- azure/ai/evaluation/_evaluators/_service_groundedness/__init__.py +9 -0
- azure/ai/evaluation/_evaluators/_service_groundedness/_service_groundedness.py +150 -0
- azure/ai/evaluation/_evaluators/_similarity/_similarity.py +17 -14
- azure/ai/evaluation/_evaluators/_xpia/xpia.py +32 -5
- azure/ai/evaluation/_exceptions.py +17 -0
- azure/ai/evaluation/_model_configurations.py +18 -1
- azure/ai/evaluation/_version.py +1 -1
- azure/ai/evaluation/simulator/__init__.py +2 -1
- azure/ai/evaluation/simulator/_adversarial_scenario.py +5 -0
- azure/ai/evaluation/simulator/_adversarial_simulator.py +4 -1
- azure/ai/evaluation/simulator/_data_sources/__init__.py +3 -0
- azure/ai/evaluation/simulator/_data_sources/grounding.json +1150 -0
- azure/ai/evaluation/simulator/_direct_attack_simulator.py +1 -1
- azure/ai/evaluation/simulator/_helpers/__init__.py +1 -2
- azure/ai/evaluation/simulator/_helpers/_simulator_data_classes.py +22 -1
- azure/ai/evaluation/simulator/_indirect_attack_simulator.py +79 -34
- azure/ai/evaluation/simulator/_model_tools/_identity_manager.py +1 -1
- azure/ai/evaluation/simulator/_prompty/task_query_response.prompty +4 -4
- azure/ai/evaluation/simulator/_prompty/task_simulate.prompty +6 -1
- azure/ai/evaluation/simulator/_simulator.py +115 -61
- azure/ai/evaluation/simulator/_utils.py +6 -6
- {azure_ai_evaluation-1.0.0b4.dist-info → azure_ai_evaluation-1.0.0b5.dist-info}/METADATA +166 -9
- {azure_ai_evaluation-1.0.0b4.dist-info → azure_ai_evaluation-1.0.0b5.dist-info}/NOTICE.txt +20 -0
- azure_ai_evaluation-1.0.0b5.dist-info/RECORD +120 -0
- {azure_ai_evaluation-1.0.0b4.dist-info → azure_ai_evaluation-1.0.0b5.dist-info}/WHEEL +1 -1
- azure/ai/evaluation/_evaluators/_groundedness/groundedness.prompty +0 -49
- azure_ai_evaluation-1.0.0b4.dist-info/RECORD +0 -106
- /azure/ai/evaluation/{simulator/_helpers → _common}/_experimental.py +0 -0
- /azure/ai/evaluation/_evaluate/{_batch_run_client → _batch_run}/code_client.py +0 -0
- {azure_ai_evaluation-1.0.0b4.dist-info → azure_ai_evaluation-1.0.0b5.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
# ---------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# ---------------------------------------------------------
|
|
4
|
+
from promptflow._utils.async_utils import async_run_allowing_running_loop
|
|
5
|
+
from azure.ai.evaluation._common._experimental import experimental
|
|
6
|
+
from azure.ai.evaluation._common.constants import EvaluationMetrics
|
|
7
|
+
from azure.ai.evaluation._common.utils import validate_conversation
|
|
8
|
+
from azure.ai.evaluation._common.rai_service import evaluate_with_rai_service_multimodal
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@experimental
|
|
12
|
+
class ProtectedMaterialMultimodalEvaluator:
|
|
13
|
+
"""
|
|
14
|
+
Initialize a protected materials evaluator to detect whether protected material
|
|
15
|
+
is present in multimodal messages. The evaluator outputs a Boolean label (`True` or `False`)
|
|
16
|
+
indicating the presence of protected material, along with AI-generated reasoning.
|
|
17
|
+
|
|
18
|
+
:param credential: The credential for connecting to the Azure AI project. This is required.
|
|
19
|
+
:type credential: ~azure.core.credentials.TokenCredential
|
|
20
|
+
:param azure_ai_project: The scope of the Azure AI project, containing the subscription ID,
|
|
21
|
+
resource group, and project name.
|
|
22
|
+
:type azure_ai_project: ~azure.ai.evaluation.AzureAIProject
|
|
23
|
+
|
|
24
|
+
:return: A dictionary containing the evaluation result label and reasoning.
|
|
25
|
+
:rtype: Dict[str, str]
|
|
26
|
+
|
|
27
|
+
**Usage Example**
|
|
28
|
+
|
|
29
|
+
.. code-block:: python
|
|
30
|
+
|
|
31
|
+
azure_ai_project = {
|
|
32
|
+
"subscription_id": "<subscription_id>",
|
|
33
|
+
"resource_group_name": "<resource_group_name>",
|
|
34
|
+
"project_name": "<project_name>",
|
|
35
|
+
}
|
|
36
|
+
eval_fn = ProtectedMaterialMultimodalEvaluator(azure_ai_project)
|
|
37
|
+
result = eval_fn(
|
|
38
|
+
{
|
|
39
|
+
"messages": [
|
|
40
|
+
{
|
|
41
|
+
"role": "user",
|
|
42
|
+
"content": [
|
|
43
|
+
{
|
|
44
|
+
"type": "text",
|
|
45
|
+
"text": "What's in this image?"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"type": "image_url",
|
|
49
|
+
"image_url": {
|
|
50
|
+
"url": "<image url or base64 encoded image>"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
},
|
|
55
|
+
{
|
|
56
|
+
"role": "assistant",
|
|
57
|
+
"content": "This picture shows an astronaut standing in the desert."
|
|
58
|
+
}
|
|
59
|
+
]
|
|
60
|
+
}
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
**Output Format**
|
|
64
|
+
|
|
65
|
+
.. code-block:: json
|
|
66
|
+
|
|
67
|
+
{
|
|
68
|
+
"protected_material_label": "False",
|
|
69
|
+
"protected_material_reason": "This query does not contain any protected material."
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
"""
|
|
73
|
+
|
|
74
|
+
def __init__(
|
|
75
|
+
self,
|
|
76
|
+
credential,
|
|
77
|
+
azure_ai_project,
|
|
78
|
+
):
|
|
79
|
+
self._async_evaluator = _AsyncProtectedMaterialMultimodalEvaluator(credential, azure_ai_project)
|
|
80
|
+
|
|
81
|
+
def __call__(self, *, conversation, **kwargs):
|
|
82
|
+
"""
|
|
83
|
+
Evaluates protected materials content.
|
|
84
|
+
|
|
85
|
+
:keyword messages: The messages to be evaluated. Each message should have "role" and "content" keys.
|
|
86
|
+
:paramtype messages: ~azure.ai.evaluation.Conversation
|
|
87
|
+
:return: A dictionary containing a boolean label and reasoning.
|
|
88
|
+
:rtype: Dict[str, str]
|
|
89
|
+
"""
|
|
90
|
+
return async_run_allowing_running_loop(self._async_evaluator, conversation=conversation, **kwargs)
|
|
91
|
+
|
|
92
|
+
def _to_async(self):
|
|
93
|
+
return self._async_evaluator
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class _AsyncProtectedMaterialMultimodalEvaluator:
|
|
97
|
+
def __init__(self, credential, azure_ai_project):
|
|
98
|
+
self._credential = credential
|
|
99
|
+
self._azure_ai_project = azure_ai_project
|
|
100
|
+
|
|
101
|
+
async def __call__(self, *, conversation, **kwargs):
|
|
102
|
+
"""
|
|
103
|
+
Evaluates content according to this evaluator's metric.
|
|
104
|
+
:keyword conversation: The conversation contains list of messages to be evaluated.
|
|
105
|
+
Each message should have "role" and "content" keys.
|
|
106
|
+
:paramtype conversation: ~azure.ai.evaluation.Conversation
|
|
107
|
+
:return: The evaluation score computation based on the Content Safety metric (self.metric).
|
|
108
|
+
:rtype: Any
|
|
109
|
+
"""
|
|
110
|
+
# Validate inputs
|
|
111
|
+
validate_conversation(conversation)
|
|
112
|
+
messages = conversation["messages"]
|
|
113
|
+
# Run score computation based on supplied metric.
|
|
114
|
+
result = await evaluate_with_rai_service_multimodal(
|
|
115
|
+
messages=messages,
|
|
116
|
+
metric_name=EvaluationMetrics.PROTECTED_MATERIAL,
|
|
117
|
+
credential=self._credential,
|
|
118
|
+
project_scope=self._azure_ai_project,
|
|
119
|
+
)
|
|
120
|
+
return result
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# ---------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# ---------------------------------------------------------
|
|
4
|
+
from promptflow._utils.async_utils import async_run_allowing_running_loop
|
|
5
|
+
from azure.ai.evaluation._common._experimental import experimental
|
|
6
|
+
from azure.ai.evaluation._common.constants import EvaluationMetrics
|
|
7
|
+
from ._content_safety_multimodal_base import ContentSafetyMultimodalEvaluatorBase
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@experimental
|
|
11
|
+
class SelfHarmMultimodalEvaluator:
|
|
12
|
+
"""
|
|
13
|
+
Initialize a self harm multimodal evaluator for self harm score.
|
|
14
|
+
|
|
15
|
+
:param credential: The credential for connecting to the Azure AI project. This is required.
|
|
16
|
+
:type credential: ~azure.core.credentials.TokenCredential
|
|
17
|
+
:param azure_ai_project: The scope of the Azure AI project,
|
|
18
|
+
which includes subscription ID, resource group name, and project name.
|
|
19
|
+
:type azure_ai_project: ~azure.ai.evaluation.AzureAIProject
|
|
20
|
+
|
|
21
|
+
**Usage Example**
|
|
22
|
+
|
|
23
|
+
.. code-block:: python
|
|
24
|
+
|
|
25
|
+
azure_ai_project = {
|
|
26
|
+
"subscription_id": "<subscription_id>",
|
|
27
|
+
"resource_group_name": "<resource_group_name>",
|
|
28
|
+
"project_name": "<project_name>",
|
|
29
|
+
}
|
|
30
|
+
eval_fn = SelfHarmMultimodalEvaluator(azure_ai_project)
|
|
31
|
+
result = eval_fn(
|
|
32
|
+
{
|
|
33
|
+
"messages": [
|
|
34
|
+
{
|
|
35
|
+
"role": "user",
|
|
36
|
+
"content": [
|
|
37
|
+
{
|
|
38
|
+
"type": "text",
|
|
39
|
+
"text": "What's in this image?"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"type": "image_url",
|
|
43
|
+
"image_url": {
|
|
44
|
+
"url": "<image url or base64 encoded image>"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"role": "assistant",
|
|
51
|
+
"content": "This picture shows an astronaut standing in the desert."
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
**Output Format**
|
|
58
|
+
|
|
59
|
+
.. code-block:: json
|
|
60
|
+
|
|
61
|
+
{
|
|
62
|
+
"self_harm": "High",
|
|
63
|
+
"self_harm_score": 6.5,
|
|
64
|
+
"self_harm_reason": "Some reason"
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
def __init__(self, credential, azure_ai_project):
|
|
70
|
+
self._async_evaluator = _AsyncSelfHarmMultimodalEvaluator(credential, azure_ai_project)
|
|
71
|
+
|
|
72
|
+
def __call__(self, *, conversation, **kwargs):
|
|
73
|
+
"""
|
|
74
|
+
Evaluates self harm content.
|
|
75
|
+
:keyword conversation: The conversation contains list of messages to be evaluated.
|
|
76
|
+
Each message should have "role" and "content" keys.
|
|
77
|
+
:paramtype conversation: ~azure.ai.evaluation.Conversation
|
|
78
|
+
:return: The self harm score.
|
|
79
|
+
:rtype: Dict[str, Union[float, str]]
|
|
80
|
+
"""
|
|
81
|
+
return async_run_allowing_running_loop(self._async_evaluator, conversation=conversation, **kwargs)
|
|
82
|
+
|
|
83
|
+
def _to_async(self):
|
|
84
|
+
return self._async_evaluator
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class _AsyncSelfHarmMultimodalEvaluator(ContentSafetyMultimodalEvaluatorBase):
|
|
88
|
+
def __init__(self, credential, azure_ai_project):
|
|
89
|
+
super().__init__(
|
|
90
|
+
metric=EvaluationMetrics.SELF_HARM,
|
|
91
|
+
credential=credential,
|
|
92
|
+
azure_ai_project=azure_ai_project,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
async def __call__(self, *, conversation, **kwargs):
|
|
96
|
+
return await super().__call__(conversation=conversation, **kwargs)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# ---------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# ---------------------------------------------------------
|
|
4
|
+
from promptflow._utils.async_utils import async_run_allowing_running_loop
|
|
5
|
+
from azure.ai.evaluation._common.constants import EvaluationMetrics
|
|
6
|
+
from azure.ai.evaluation._common._experimental import experimental
|
|
7
|
+
from ._content_safety_multimodal_base import ContentSafetyMultimodalEvaluatorBase
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@experimental
|
|
11
|
+
class SexualMultimodalEvaluator:
|
|
12
|
+
"""
|
|
13
|
+
Initialize a sexual multimodal evaluator for sexual score.
|
|
14
|
+
|
|
15
|
+
:param credential: The credential for connecting to the Azure AI project. This is required.
|
|
16
|
+
:type credential: ~azure.core.credentials.TokenCredential
|
|
17
|
+
:param azure_ai_project: The scope of the Azure AI project,
|
|
18
|
+
which includes subscription ID, resource group name, and project name.
|
|
19
|
+
:type azure_ai_project: ~azure.ai.evaluation.AzureAIProject
|
|
20
|
+
|
|
21
|
+
**Usage Example**
|
|
22
|
+
|
|
23
|
+
.. code-block:: python
|
|
24
|
+
|
|
25
|
+
azure_ai_project = {
|
|
26
|
+
"subscription_id": "<subscription_id>",
|
|
27
|
+
"resource_group_name": "<resource_group_name>",
|
|
28
|
+
"project_name": "<project_name>",
|
|
29
|
+
}
|
|
30
|
+
eval_fn = SexualMultimodalEvaluator(azure_ai_project)
|
|
31
|
+
result = eval_fn(
|
|
32
|
+
{
|
|
33
|
+
"messages": [
|
|
34
|
+
{
|
|
35
|
+
"role": "user",
|
|
36
|
+
"content": [
|
|
37
|
+
{
|
|
38
|
+
"type": "text",
|
|
39
|
+
"text": "What's in this image?"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"type": "image_url",
|
|
43
|
+
"image_url": {
|
|
44
|
+
"url": "<image url or base64 encoded image>"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"role": "assistant",
|
|
51
|
+
"content": "This picture shows an astronaut standing in the desert."
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
**Output Format**
|
|
58
|
+
|
|
59
|
+
.. code-block:: json
|
|
60
|
+
|
|
61
|
+
{
|
|
62
|
+
"sexual": "High",
|
|
63
|
+
"sexual_score": 6.5,
|
|
64
|
+
"sexual_reason": "Some reason"
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
def __init__(self, credential, azure_ai_project):
|
|
70
|
+
self._async_evaluator = _AsyncSexualMultimodalEvaluator(credential, azure_ai_project)
|
|
71
|
+
|
|
72
|
+
def __call__(self, *, conversation, **kwargs):
|
|
73
|
+
"""
|
|
74
|
+
Evaluates sexual content.
|
|
75
|
+
:keyword conversation: The conversation contains list of messages to be evaluated.
|
|
76
|
+
Each message should have "role" and "content" keys.
|
|
77
|
+
:paramtype conversation: ~azure.ai.evaluation.Conversation
|
|
78
|
+
:return: The sexual score.
|
|
79
|
+
:rtype: Dict[str, Union[float, str]]
|
|
80
|
+
"""
|
|
81
|
+
return async_run_allowing_running_loop(self._async_evaluator, conversation=conversation, **kwargs)
|
|
82
|
+
|
|
83
|
+
def _to_async(self):
|
|
84
|
+
return self._async_evaluator
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class _AsyncSexualMultimodalEvaluator(ContentSafetyMultimodalEvaluatorBase):
|
|
88
|
+
def __init__(self, credential, azure_ai_project):
|
|
89
|
+
super().__init__(
|
|
90
|
+
metric=EvaluationMetrics.SEXUAL,
|
|
91
|
+
credential=credential,
|
|
92
|
+
azure_ai_project=azure_ai_project,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
async def __call__(self, *, conversation, **kwargs):
|
|
96
|
+
return await super().__call__(conversation=conversation, **kwargs)
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# ---------------------------------------------------------
|
|
2
|
+
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
# ---------------------------------------------------------
|
|
4
|
+
from promptflow._utils.async_utils import async_run_allowing_running_loop
|
|
5
|
+
from azure.ai.evaluation._common._experimental import experimental
|
|
6
|
+
from azure.ai.evaluation._common.constants import EvaluationMetrics
|
|
7
|
+
from ._content_safety_multimodal_base import ContentSafetyMultimodalEvaluatorBase
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@experimental
|
|
11
|
+
class ViolenceMultimodalEvaluator:
|
|
12
|
+
"""
|
|
13
|
+
Initialize a violence multimodal evaluator for violence score.
|
|
14
|
+
|
|
15
|
+
:param credential: The credential for connecting to the Azure AI project. This is required.
|
|
16
|
+
:type credential: ~azure.core.credentials.TokenCredential
|
|
17
|
+
:param azure_ai_project: The scope of the Azure AI project,
|
|
18
|
+
which includes subscription ID, resource group name, and project name.
|
|
19
|
+
:type azure_ai_project: ~azure.ai.evaluation.AzureAIProject
|
|
20
|
+
|
|
21
|
+
**Usage Example**
|
|
22
|
+
|
|
23
|
+
.. code-block:: python
|
|
24
|
+
|
|
25
|
+
azure_ai_project = {
|
|
26
|
+
"subscription_id": "<subscription_id>",
|
|
27
|
+
"resource_group_name": "<resource_group_name>",
|
|
28
|
+
"project_name": "<project_name>",
|
|
29
|
+
}
|
|
30
|
+
eval_fn = ViolenceMultimodalEvaluator(azure_ai_project)
|
|
31
|
+
result = eval_fn(
|
|
32
|
+
{
|
|
33
|
+
"messages": [
|
|
34
|
+
{
|
|
35
|
+
"role": "user",
|
|
36
|
+
"content": [
|
|
37
|
+
{
|
|
38
|
+
"type": "text",
|
|
39
|
+
"text": "What's in this image?"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"type": "image_url",
|
|
43
|
+
"image_url": {
|
|
44
|
+
"url": "<image url or base64 encoded image>"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
]
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"role": "assistant",
|
|
51
|
+
"content": "This picture shows an astronaut standing in the desert."
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
**Output Format**
|
|
58
|
+
|
|
59
|
+
.. code-block:: json
|
|
60
|
+
|
|
61
|
+
{
|
|
62
|
+
"violence": "High",
|
|
63
|
+
"violence_score": 6.5,
|
|
64
|
+
"violence_reason": "Some reason"
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
"""
|
|
68
|
+
|
|
69
|
+
def __init__(self, credential, azure_ai_project):
|
|
70
|
+
self._async_evaluator = _AsyncViolenceMultimodalEvaluator(credential, azure_ai_project)
|
|
71
|
+
|
|
72
|
+
def __call__(self, *, conversation, **kwargs):
|
|
73
|
+
"""
|
|
74
|
+
Evaluates violence content.
|
|
75
|
+
:keyword conversation: The conversation contains list of messages to be evaluated.
|
|
76
|
+
Each message should have "role" and "content" keys.
|
|
77
|
+
:paramtype conversation: ~azure.ai.evaluation.Conversation
|
|
78
|
+
:return: The violence score.
|
|
79
|
+
:rtype: Dict[str, Union[float, str]]
|
|
80
|
+
"""
|
|
81
|
+
return async_run_allowing_running_loop(self._async_evaluator, conversation=conversation, **kwargs)
|
|
82
|
+
|
|
83
|
+
def _to_async(self):
|
|
84
|
+
return self._async_evaluator
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class _AsyncViolenceMultimodalEvaluator(ContentSafetyMultimodalEvaluatorBase):
|
|
88
|
+
def __init__(self, credential, azure_ai_project):
|
|
89
|
+
super().__init__(
|
|
90
|
+
metric=EvaluationMetrics.VIOLENCE,
|
|
91
|
+
credential=credential,
|
|
92
|
+
azure_ai_project=azure_ai_project,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
async def __call__(self, *, conversation, **kwargs):
|
|
96
|
+
return await super().__call__(conversation=conversation, **kwargs)
|
|
@@ -1,26 +1,33 @@
|
|
|
1
1
|
# ---------------------------------------------------------
|
|
2
2
|
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
3
|
# ---------------------------------------------------------
|
|
4
|
+
|
|
5
|
+
from typing import Optional
|
|
6
|
+
|
|
4
7
|
from typing_extensions import override
|
|
5
8
|
|
|
9
|
+
from azure.ai.evaluation._common._experimental import experimental
|
|
6
10
|
from azure.ai.evaluation._common.constants import EvaluationMetrics
|
|
7
11
|
from azure.ai.evaluation._evaluators._common import RaiServiceEvaluatorBase
|
|
8
12
|
|
|
9
13
|
|
|
14
|
+
@experimental
|
|
10
15
|
class ProtectedMaterialEvaluator(RaiServiceEvaluatorBase):
|
|
11
16
|
"""
|
|
12
17
|
Initialize a protected material evaluator to detect whether protected material
|
|
13
|
-
is present in
|
|
18
|
+
is present in the AI system's response. The evaluator outputs a Boolean label (`True` or `False`)
|
|
19
|
+
indicating the presence of protected material, along with AI-generated reasoning.
|
|
14
20
|
|
|
15
|
-
:param credential: The credential for connecting to Azure AI project.
|
|
21
|
+
:param credential: The credential required for connecting to the Azure AI project.
|
|
16
22
|
:type credential: ~azure.core.credentials.TokenCredential
|
|
17
|
-
:param azure_ai_project: The scope of the Azure AI project
|
|
18
|
-
|
|
23
|
+
:param azure_ai_project: The scope of the Azure AI project, containing the subscription ID,
|
|
24
|
+
resource group, and project name.
|
|
19
25
|
:type azure_ai_project: ~azure.ai.evaluation.AzureAIProject
|
|
20
|
-
:return: Whether or not protected material was found in the response, with AI-generated reasoning.
|
|
21
|
-
:rtype: Dict[str, str]
|
|
22
26
|
|
|
23
|
-
|
|
27
|
+
:return: A dictionary with a label indicating the presence of protected material and the reasoning.
|
|
28
|
+
:rtype: Dict[str, Union[bool, str]]
|
|
29
|
+
|
|
30
|
+
**Usage Example**
|
|
24
31
|
|
|
25
32
|
.. code-block:: python
|
|
26
33
|
|
|
@@ -32,21 +39,22 @@ class ProtectedMaterialEvaluator(RaiServiceEvaluatorBase):
|
|
|
32
39
|
eval_fn = ProtectedMaterialEvaluator(azure_ai_project)
|
|
33
40
|
result = eval_fn(query="What is the capital of France?", response="Paris.")
|
|
34
41
|
|
|
35
|
-
**Output
|
|
42
|
+
**Output Format**
|
|
36
43
|
|
|
37
|
-
.. code-block::
|
|
44
|
+
.. code-block:: json
|
|
38
45
|
|
|
39
46
|
{
|
|
40
|
-
"protected_material_label":
|
|
47
|
+
"protected_material_label": false,
|
|
41
48
|
"protected_material_reason": "This query does not contain any protected material."
|
|
42
49
|
}
|
|
50
|
+
|
|
43
51
|
"""
|
|
44
52
|
|
|
45
53
|
@override
|
|
46
54
|
def __init__(
|
|
47
55
|
self,
|
|
48
56
|
credential,
|
|
49
|
-
azure_ai_project
|
|
57
|
+
azure_ai_project,
|
|
50
58
|
eval_last_turn: bool = False,
|
|
51
59
|
):
|
|
52
60
|
super().__init__(
|
|
@@ -55,3 +63,28 @@ class ProtectedMaterialEvaluator(RaiServiceEvaluatorBase):
|
|
|
55
63
|
credential=credential,
|
|
56
64
|
eval_last_turn=eval_last_turn,
|
|
57
65
|
)
|
|
66
|
+
|
|
67
|
+
@override
|
|
68
|
+
def __call__(
|
|
69
|
+
self,
|
|
70
|
+
*,
|
|
71
|
+
query: Optional[str] = None,
|
|
72
|
+
response: Optional[str] = None,
|
|
73
|
+
conversation=None,
|
|
74
|
+
**kwargs,
|
|
75
|
+
):
|
|
76
|
+
"""
|
|
77
|
+
Evaluate if protected material is present in your AI system's response.
|
|
78
|
+
|
|
79
|
+
:keyword query: The query to be evaluated.
|
|
80
|
+
:paramtype query: str
|
|
81
|
+
:keyword response: The response to be evaluated.
|
|
82
|
+
:paramtype response: str
|
|
83
|
+
:keyword conversation: The conversation to evaluate. Expected to contain a list of conversation turns under the
|
|
84
|
+
key "messages". Conversation turns are expected
|
|
85
|
+
to be dictionaries with keys "content" and "role".
|
|
86
|
+
:paramtype conversation: Optional[~azure.ai.evaluation.Conversation]
|
|
87
|
+
:return: The fluency score.
|
|
88
|
+
:rtype: Union[Dict[str, Union[str, bool]], Dict[str, Union[str, bool, Dict[str, List[Union[str, bool]]]]]]
|
|
89
|
+
"""
|
|
90
|
+
return super().__call__(query=query, response=response, conversation=conversation, **kwargs)
|
|
@@ -22,8 +22,7 @@ class QAEvaluator:
|
|
|
22
22
|
:param model_config: Configuration for the Azure OpenAI model.
|
|
23
23
|
:type model_config: Union[~azure.ai.evaluation.AzureOpenAIModelConfiguration,
|
|
24
24
|
~azure.ai.evaluation.OpenAIModelConfiguration]
|
|
25
|
-
:return: A
|
|
26
|
-
:rtype: Callable
|
|
25
|
+
:return: A callable class that evaluates and generates metrics for "question-answering" scenario.
|
|
27
26
|
|
|
28
27
|
**Usage**
|
|
29
28
|
|
|
@@ -42,6 +41,11 @@ class QAEvaluator:
|
|
|
42
41
|
.. code-block:: python
|
|
43
42
|
|
|
44
43
|
{
|
|
44
|
+
"groundedness": 3.5,
|
|
45
|
+
"relevance": 4.0,
|
|
46
|
+
"coherence": 1.5,
|
|
47
|
+
"fluency": 4.0,
|
|
48
|
+
"similarity": 3.0,
|
|
45
49
|
"gpt_groundedness": 3.5,
|
|
46
50
|
"gpt_relevance": 4.0,
|
|
47
51
|
"gpt_coherence": 1.5,
|
|
@@ -51,7 +55,7 @@ class QAEvaluator:
|
|
|
51
55
|
}
|
|
52
56
|
"""
|
|
53
57
|
|
|
54
|
-
def __init__(self, model_config
|
|
58
|
+
def __init__(self, model_config, parallel: bool = True):
|
|
55
59
|
self._parallel = parallel
|
|
56
60
|
|
|
57
61
|
self._evaluators: List[Callable[..., Dict[str, float]]] = [
|
|
@@ -25,28 +25,32 @@ class RelevanceEvaluator(PromptyEvaluatorBase):
|
|
|
25
25
|
eval_fn = RelevanceEvaluator(model_config)
|
|
26
26
|
result = eval_fn(
|
|
27
27
|
query="What is the capital of Japan?",
|
|
28
|
-
response="The capital of Japan is Tokyo."
|
|
29
|
-
context="Tokyo is Japan's capital, known for its blend of traditional culture \
|
|
30
|
-
and technological advancements.")
|
|
28
|
+
response="The capital of Japan is Tokyo.")
|
|
31
29
|
|
|
32
30
|
**Output format**
|
|
33
31
|
|
|
34
32
|
.. code-block:: python
|
|
35
33
|
|
|
36
34
|
{
|
|
37
|
-
"
|
|
35
|
+
"relevance": 3.0,
|
|
36
|
+
"gpt_relevance": 3.0,
|
|
37
|
+
"relevance_reason": "The response is relevant to the query because it provides the correct answer.",
|
|
38
38
|
}
|
|
39
|
+
|
|
40
|
+
Note: To align with our support of a diverse set of models, a key without the `gpt_` prefix has been added.
|
|
41
|
+
To maintain backwards compatibility, the old key with the `gpt_` prefix is still be present in the output;
|
|
42
|
+
however, it is recommended to use the new key moving forward as the old key will be deprecated in the future.
|
|
39
43
|
"""
|
|
40
44
|
|
|
41
45
|
# Constants must be defined within eval's directory to be save/loadable
|
|
42
|
-
|
|
43
|
-
|
|
46
|
+
_PROMPTY_FILE = "relevance.prompty"
|
|
47
|
+
_RESULT_KEY = "relevance"
|
|
44
48
|
|
|
45
49
|
@override
|
|
46
|
-
def __init__(self, model_config
|
|
50
|
+
def __init__(self, model_config):
|
|
47
51
|
current_dir = os.path.dirname(__file__)
|
|
48
|
-
prompty_path = os.path.join(current_dir, self.
|
|
49
|
-
super().__init__(model_config=model_config, prompty_file=prompty_path, result_key=self.
|
|
52
|
+
prompty_path = os.path.join(current_dir, self._PROMPTY_FILE)
|
|
53
|
+
super().__init__(model_config=model_config, prompty_file=prompty_path, result_key=self._RESULT_KEY)
|
|
50
54
|
|
|
51
55
|
@override
|
|
52
56
|
def __call__(
|
|
@@ -54,25 +58,23 @@ class RelevanceEvaluator(PromptyEvaluatorBase):
|
|
|
54
58
|
*,
|
|
55
59
|
query: Optional[str] = None,
|
|
56
60
|
response: Optional[str] = None,
|
|
57
|
-
|
|
58
|
-
conversation: Optional[dict] = None,
|
|
61
|
+
conversation=None,
|
|
59
62
|
**kwargs,
|
|
60
63
|
):
|
|
61
|
-
"""Evaluate relevance. Accepts either a
|
|
64
|
+
"""Evaluate relevance. Accepts either a query and response for a single evaluation,
|
|
62
65
|
or a conversation for a multi-turn evaluation. If the conversation has more than one turn,
|
|
63
66
|
the evaluator will aggregate the results of each turn.
|
|
64
67
|
|
|
65
|
-
:keyword query: The query to be evaluated.
|
|
68
|
+
:keyword query: The query to be evaluated. Mutually exclusive with the `conversation` parameter.
|
|
66
69
|
:paramtype query: Optional[str]
|
|
67
|
-
:keyword response: The response to be evaluated.
|
|
70
|
+
:keyword response: The response to be evaluated. Mutually exclusive with the `conversation` parameter.
|
|
68
71
|
:paramtype response: Optional[str]
|
|
69
|
-
:keyword context: The context to be evaluated.
|
|
70
|
-
:paramtype context: Optional[str]
|
|
71
72
|
:keyword conversation: The conversation to evaluate. Expected to contain a list of conversation turns under the
|
|
72
73
|
key "messages", and potentially a global context under the key "context". Conversation turns are expected
|
|
73
74
|
to be dictionaries with keys "content", "role", and possibly "context".
|
|
74
|
-
:paramtype conversation: Optional[
|
|
75
|
+
:paramtype conversation: Optional[~azure.ai.evaluation.Conversation]
|
|
75
76
|
:return: The relevance score.
|
|
76
|
-
:rtype: Dict[str, float]
|
|
77
|
+
:rtype: Union[Dict[str, float], Dict[str, Union[float, Dict[str, List[float]]]]]
|
|
77
78
|
"""
|
|
78
|
-
|
|
79
|
+
|
|
80
|
+
return super().__call__(query=query, response=response, conversation=conversation, **kwargs)
|