azure-ai-evaluation 1.0.0b3__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.

Files changed (93) hide show
  1. azure/ai/evaluation/__init__.py +23 -1
  2. azure/ai/evaluation/{simulator/_helpers → _common}/_experimental.py +20 -9
  3. azure/ai/evaluation/_common/constants.py +9 -2
  4. azure/ai/evaluation/_common/math.py +29 -0
  5. azure/ai/evaluation/_common/rai_service.py +222 -93
  6. azure/ai/evaluation/_common/utils.py +328 -19
  7. azure/ai/evaluation/_constants.py +16 -8
  8. azure/ai/evaluation/_evaluate/{_batch_run_client → _batch_run}/__init__.py +3 -2
  9. azure/ai/evaluation/_evaluate/{_batch_run_client → _batch_run}/code_client.py +33 -17
  10. azure/ai/evaluation/_evaluate/{_batch_run_client/batch_run_context.py → _batch_run/eval_run_context.py} +14 -7
  11. azure/ai/evaluation/_evaluate/{_batch_run_client → _batch_run}/proxy_client.py +22 -4
  12. azure/ai/evaluation/_evaluate/_batch_run/target_run_context.py +35 -0
  13. azure/ai/evaluation/_evaluate/_eval_run.py +47 -14
  14. azure/ai/evaluation/_evaluate/_evaluate.py +370 -188
  15. azure/ai/evaluation/_evaluate/_telemetry/__init__.py +15 -16
  16. azure/ai/evaluation/_evaluate/_utils.py +77 -25
  17. azure/ai/evaluation/_evaluators/_bleu/_bleu.py +1 -1
  18. azure/ai/evaluation/_evaluators/_coherence/_coherence.py +16 -10
  19. azure/ai/evaluation/_evaluators/_coherence/coherence.prompty +76 -34
  20. azure/ai/evaluation/_evaluators/_common/_base_eval.py +76 -46
  21. azure/ai/evaluation/_evaluators/_common/_base_prompty_eval.py +26 -19
  22. azure/ai/evaluation/_evaluators/_common/_base_rai_svc_eval.py +62 -25
  23. azure/ai/evaluation/_evaluators/_content_safety/_content_safety.py +68 -36
  24. azure/ai/evaluation/_evaluators/_content_safety/_content_safety_chat.py +67 -46
  25. azure/ai/evaluation/_evaluators/_content_safety/_hate_unfairness.py +33 -4
  26. azure/ai/evaluation/_evaluators/_content_safety/_self_harm.py +33 -4
  27. azure/ai/evaluation/_evaluators/_content_safety/_sexual.py +33 -4
  28. azure/ai/evaluation/_evaluators/_content_safety/_violence.py +33 -4
  29. azure/ai/evaluation/_evaluators/_eci/_eci.py +7 -5
  30. azure/ai/evaluation/_evaluators/_f1_score/_f1_score.py +14 -6
  31. azure/ai/evaluation/_evaluators/_fluency/_fluency.py +22 -21
  32. azure/ai/evaluation/_evaluators/_fluency/fluency.prompty +66 -36
  33. azure/ai/evaluation/_evaluators/_gleu/_gleu.py +1 -1
  34. azure/ai/evaluation/_evaluators/_groundedness/_groundedness.py +51 -16
  35. azure/ai/evaluation/_evaluators/_groundedness/groundedness_with_query.prompty +113 -0
  36. azure/ai/evaluation/_evaluators/_groundedness/groundedness_without_query.prompty +99 -0
  37. azure/ai/evaluation/_evaluators/_meteor/_meteor.py +3 -7
  38. azure/ai/evaluation/_evaluators/_multimodal/__init__.py +20 -0
  39. azure/ai/evaluation/_evaluators/_multimodal/_content_safety_multimodal.py +130 -0
  40. azure/ai/evaluation/_evaluators/_multimodal/_content_safety_multimodal_base.py +57 -0
  41. azure/ai/evaluation/_evaluators/_multimodal/_hate_unfairness.py +96 -0
  42. azure/ai/evaluation/_evaluators/_multimodal/_protected_material.py +120 -0
  43. azure/ai/evaluation/_evaluators/_multimodal/_self_harm.py +96 -0
  44. azure/ai/evaluation/_evaluators/_multimodal/_sexual.py +96 -0
  45. azure/ai/evaluation/_evaluators/_multimodal/_violence.py +96 -0
  46. azure/ai/evaluation/_evaluators/_protected_material/_protected_material.py +46 -13
  47. azure/ai/evaluation/_evaluators/_qa/_qa.py +11 -6
  48. azure/ai/evaluation/_evaluators/_relevance/_relevance.py +23 -20
  49. azure/ai/evaluation/_evaluators/_relevance/relevance.prompty +78 -42
  50. azure/ai/evaluation/_evaluators/_retrieval/_retrieval.py +126 -80
  51. azure/ai/evaluation/_evaluators/_retrieval/retrieval.prompty +74 -24
  52. azure/ai/evaluation/_evaluators/_rouge/_rouge.py +2 -2
  53. azure/ai/evaluation/_evaluators/_service_groundedness/__init__.py +9 -0
  54. azure/ai/evaluation/_evaluators/_service_groundedness/_service_groundedness.py +150 -0
  55. azure/ai/evaluation/_evaluators/_similarity/_similarity.py +32 -15
  56. azure/ai/evaluation/_evaluators/_xpia/xpia.py +36 -10
  57. azure/ai/evaluation/_exceptions.py +26 -6
  58. azure/ai/evaluation/_http_utils.py +203 -132
  59. azure/ai/evaluation/_model_configurations.py +23 -6
  60. azure/ai/evaluation/_vendor/__init__.py +3 -0
  61. azure/ai/evaluation/_vendor/rouge_score/__init__.py +14 -0
  62. azure/ai/evaluation/_vendor/rouge_score/rouge_scorer.py +328 -0
  63. azure/ai/evaluation/_vendor/rouge_score/scoring.py +63 -0
  64. azure/ai/evaluation/_vendor/rouge_score/tokenize.py +63 -0
  65. azure/ai/evaluation/_vendor/rouge_score/tokenizers.py +53 -0
  66. azure/ai/evaluation/_version.py +1 -1
  67. azure/ai/evaluation/simulator/__init__.py +2 -1
  68. azure/ai/evaluation/simulator/_adversarial_scenario.py +5 -0
  69. azure/ai/evaluation/simulator/_adversarial_simulator.py +88 -60
  70. azure/ai/evaluation/simulator/_conversation/__init__.py +13 -12
  71. azure/ai/evaluation/simulator/_conversation/_conversation.py +4 -4
  72. azure/ai/evaluation/simulator/_data_sources/__init__.py +3 -0
  73. azure/ai/evaluation/simulator/_data_sources/grounding.json +1150 -0
  74. azure/ai/evaluation/simulator/_direct_attack_simulator.py +24 -66
  75. azure/ai/evaluation/simulator/_helpers/__init__.py +1 -2
  76. azure/ai/evaluation/simulator/_helpers/_simulator_data_classes.py +26 -5
  77. azure/ai/evaluation/simulator/_indirect_attack_simulator.py +98 -95
  78. azure/ai/evaluation/simulator/_model_tools/_identity_manager.py +67 -21
  79. azure/ai/evaluation/simulator/_model_tools/_proxy_completion_model.py +28 -11
  80. azure/ai/evaluation/simulator/_model_tools/_template_handler.py +68 -24
  81. azure/ai/evaluation/simulator/_model_tools/models.py +10 -10
  82. azure/ai/evaluation/simulator/_prompty/task_query_response.prompty +4 -9
  83. azure/ai/evaluation/simulator/_prompty/task_simulate.prompty +6 -5
  84. azure/ai/evaluation/simulator/_simulator.py +222 -169
  85. azure/ai/evaluation/simulator/_tracing.py +4 -4
  86. azure/ai/evaluation/simulator/_utils.py +6 -6
  87. {azure_ai_evaluation-1.0.0b3.dist-info → azure_ai_evaluation-1.0.0b5.dist-info}/METADATA +237 -52
  88. azure_ai_evaluation-1.0.0b5.dist-info/NOTICE.txt +70 -0
  89. azure_ai_evaluation-1.0.0b5.dist-info/RECORD +120 -0
  90. {azure_ai_evaluation-1.0.0b3.dist-info → azure_ai_evaluation-1.0.0b5.dist-info}/WHEEL +1 -1
  91. azure/ai/evaluation/_evaluators/_groundedness/groundedness.prompty +0 -49
  92. azure_ai_evaluation-1.0.0b3.dist-info/RECORD +0 -98
  93. {azure_ai_evaluation-1.0.0b3.dist-info → azure_ai_evaluation-1.0.0b5.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,57 @@
1
+ # ---------------------------------------------------------
2
+ # Copyright (c) Microsoft Corporation. All rights reserved.
3
+ # ---------------------------------------------------------
4
+ from abc import ABC
5
+ from typing import Union
6
+ from azure.ai.evaluation._common.rai_service import evaluate_with_rai_service_multimodal
7
+ from azure.ai.evaluation._common.constants import EvaluationMetrics, _InternalEvaluationMetrics
8
+ from azure.ai.evaluation._common.utils import validate_conversation
9
+ from azure.core.credentials import TokenCredential
10
+ from azure.ai.evaluation._common._experimental import experimental
11
+
12
+
13
+ @experimental
14
+ class ContentSafetyMultimodalEvaluatorBase(ABC):
15
+ """
16
+ Initialize a evaluator for a specified Evaluation Metric. Base class that is not
17
+ meant to be instantiated by users.
18
+
19
+ :param metric: The metric to be evaluated.
20
+ :type metric: ~azure.ai.evaluation._evaluators._content_safety.flow.constants.EvaluationMetrics
21
+ :param credential: The credential for connecting to Azure AI project. Required
22
+ :type credential: ~azure.core.credentials.TokenCredential
23
+ :param azure_ai_project: The scope of the Azure AI project.
24
+ It contains subscription id, resource group, and project name.
25
+ :type azure_ai_project: ~azure.ai.evaluation.AzureAIProject
26
+ """
27
+
28
+ def __init__(
29
+ self,
30
+ metric: Union[EvaluationMetrics, _InternalEvaluationMetrics],
31
+ credential: TokenCredential,
32
+ azure_ai_project,
33
+ ):
34
+ self._metric = metric
35
+ self._azure_ai_project = azure_ai_project
36
+ self._credential = credential
37
+
38
+ async def __call__(self, *, conversation, **kwargs):
39
+ """
40
+ Evaluates content according to this evaluator's metric.
41
+ :keyword conversation: The conversation contains list of messages to be evaluated.
42
+ Each message should have "role" and "content" keys.
43
+ :paramtype conversation: ~azure.ai.evaluation.Conversation
44
+ :return: The evaluation score computation based on the Content Safety metric (self.metric).
45
+ :rtype: Dict[str, Union[float, str]]
46
+ """
47
+ # validate inputs
48
+ validate_conversation(conversation)
49
+ messages = conversation["messages"]
50
+ # Run score computation based on supplied metric.
51
+ result = await evaluate_with_rai_service_multimodal(
52
+ messages=messages,
53
+ metric_name=self._metric,
54
+ project_scope=self._azure_ai_project,
55
+ credential=self._credential,
56
+ )
57
+ 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 HateUnfairnessMultimodalEvaluator:
12
+ """
13
+ Initialize a hate-unfairness multimodal evaluator for hate unfairness 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 = HateUnfairnessMultimodalEvaluator(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
+ "hate_unfairness": "High",
63
+ "hate_unfairness_score": 6.5,
64
+ "hate_unfairness_reason": "Some reason"
65
+ }
66
+
67
+ """
68
+
69
+ def __init__(self, credential, azure_ai_project):
70
+ self._async_evaluator = _AsyncHateUnfairnessMultimodalEvaluator(credential, azure_ai_project)
71
+
72
+ def __call__(self, *, conversation, **kwargs):
73
+ """
74
+ Evaluates hate unfairness 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 hate unfairness 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 _AsyncHateUnfairnessMultimodalEvaluator(ContentSafetyMultimodalEvaluatorBase):
88
+ def __init__(self, credential, azure_ai_project):
89
+ super().__init__(
90
+ metric=EvaluationMetrics.HATE_FAIRNESS,
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,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
+
4
5
  from typing import Optional
6
+
5
7
  from typing_extensions import override
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 your AI system's response. Outputs True or False with AI-generated reasoning.
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 azure_ai_project: The scope of the Azure AI project.
16
- It contains subscription id, resource group, and project name.
21
+ :param credential: The credential required for connecting to the Azure AI project.
22
+ :type credential: ~azure.core.credentials.TokenCredential
23
+ :param azure_ai_project: The scope of the Azure AI project, containing the subscription ID,
24
+ resource group, and project name.
17
25
  :type azure_ai_project: ~azure.ai.evaluation.AzureAIProject
18
- :param credential: The credential for connecting to Azure AI project.
19
- :type credential: Optional[~azure.core.credentials.TokenCredential]
20
- :return: Whether or not protected material was found in the response, with AI-generated reasoning.
21
- :rtype: Dict[str, str]
22
26
 
23
- **Usage**
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 format**
42
+ **Output Format**
36
43
 
37
- .. code-block:: python
44
+ .. code-block:: json
38
45
 
39
46
  {
40
- "protected_material_label": "False",
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
- azure_ai_project: dict,
49
- credential: Optional[dict] = None,
56
+ credential,
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)