azure-ai-evaluation 1.8.0__py3-none-any.whl → 1.10.0__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 (142) hide show
  1. azure/ai/evaluation/__init__.py +51 -6
  2. azure/ai/evaluation/_aoai/__init__.py +1 -1
  3. azure/ai/evaluation/_aoai/aoai_grader.py +21 -11
  4. azure/ai/evaluation/_aoai/label_grader.py +3 -2
  5. azure/ai/evaluation/_aoai/python_grader.py +84 -0
  6. azure/ai/evaluation/_aoai/score_model_grader.py +91 -0
  7. azure/ai/evaluation/_aoai/string_check_grader.py +3 -2
  8. azure/ai/evaluation/_aoai/text_similarity_grader.py +3 -2
  9. azure/ai/evaluation/_azure/_envs.py +9 -10
  10. azure/ai/evaluation/_azure/_token_manager.py +7 -1
  11. azure/ai/evaluation/_common/constants.py +11 -2
  12. azure/ai/evaluation/_common/evaluation_onedp_client.py +32 -26
  13. azure/ai/evaluation/_common/onedp/__init__.py +32 -32
  14. azure/ai/evaluation/_common/onedp/_client.py +136 -139
  15. azure/ai/evaluation/_common/onedp/_configuration.py +70 -73
  16. azure/ai/evaluation/_common/onedp/_patch.py +21 -21
  17. azure/ai/evaluation/_common/onedp/_utils/__init__.py +6 -0
  18. azure/ai/evaluation/_common/onedp/_utils/model_base.py +1232 -0
  19. azure/ai/evaluation/_common/onedp/_utils/serialization.py +2032 -0
  20. azure/ai/evaluation/_common/onedp/_validation.py +50 -50
  21. azure/ai/evaluation/_common/onedp/_version.py +9 -9
  22. azure/ai/evaluation/_common/onedp/aio/__init__.py +29 -29
  23. azure/ai/evaluation/_common/onedp/aio/_client.py +138 -143
  24. azure/ai/evaluation/_common/onedp/aio/_configuration.py +70 -75
  25. azure/ai/evaluation/_common/onedp/aio/_patch.py +21 -21
  26. azure/ai/evaluation/_common/onedp/aio/operations/__init__.py +37 -39
  27. azure/ai/evaluation/_common/onedp/aio/operations/_operations.py +4832 -4494
  28. azure/ai/evaluation/_common/onedp/aio/operations/_patch.py +21 -21
  29. azure/ai/evaluation/_common/onedp/models/__init__.py +168 -142
  30. azure/ai/evaluation/_common/onedp/models/_enums.py +230 -162
  31. azure/ai/evaluation/_common/onedp/models/_models.py +2685 -2228
  32. azure/ai/evaluation/_common/onedp/models/_patch.py +21 -21
  33. azure/ai/evaluation/_common/onedp/operations/__init__.py +37 -39
  34. azure/ai/evaluation/_common/onedp/operations/_operations.py +6106 -5657
  35. azure/ai/evaluation/_common/onedp/operations/_patch.py +21 -21
  36. azure/ai/evaluation/_common/rai_service.py +88 -52
  37. azure/ai/evaluation/_common/raiclient/__init__.py +1 -1
  38. azure/ai/evaluation/_common/raiclient/operations/_operations.py +14 -1
  39. azure/ai/evaluation/_common/utils.py +188 -10
  40. azure/ai/evaluation/_constants.py +2 -1
  41. azure/ai/evaluation/_converters/__init__.py +1 -1
  42. azure/ai/evaluation/_converters/_ai_services.py +9 -8
  43. azure/ai/evaluation/_converters/_models.py +46 -0
  44. azure/ai/evaluation/_converters/_sk_services.py +495 -0
  45. azure/ai/evaluation/_eval_mapping.py +2 -2
  46. azure/ai/evaluation/_evaluate/_batch_run/_run_submitter_client.py +73 -25
  47. azure/ai/evaluation/_evaluate/_batch_run/eval_run_context.py +2 -2
  48. azure/ai/evaluation/_evaluate/_evaluate.py +210 -94
  49. azure/ai/evaluation/_evaluate/_evaluate_aoai.py +132 -89
  50. azure/ai/evaluation/_evaluate/_telemetry/__init__.py +0 -1
  51. azure/ai/evaluation/_evaluate/_utils.py +25 -17
  52. azure/ai/evaluation/_evaluators/_bleu/_bleu.py +4 -4
  53. azure/ai/evaluation/_evaluators/_code_vulnerability/_code_vulnerability.py +20 -12
  54. azure/ai/evaluation/_evaluators/_coherence/_coherence.py +6 -6
  55. azure/ai/evaluation/_evaluators/_common/_base_eval.py +45 -11
  56. azure/ai/evaluation/_evaluators/_common/_base_prompty_eval.py +24 -9
  57. azure/ai/evaluation/_evaluators/_common/_base_rai_svc_eval.py +24 -9
  58. azure/ai/evaluation/_evaluators/_content_safety/_content_safety.py +28 -18
  59. azure/ai/evaluation/_evaluators/_content_safety/_hate_unfairness.py +11 -8
  60. azure/ai/evaluation/_evaluators/_content_safety/_self_harm.py +11 -8
  61. azure/ai/evaluation/_evaluators/_content_safety/_sexual.py +12 -9
  62. azure/ai/evaluation/_evaluators/_content_safety/_violence.py +10 -7
  63. azure/ai/evaluation/_evaluators/_document_retrieval/__init__.py +1 -5
  64. azure/ai/evaluation/_evaluators/_document_retrieval/_document_retrieval.py +37 -64
  65. azure/ai/evaluation/_evaluators/_eci/_eci.py +6 -3
  66. azure/ai/evaluation/_evaluators/_f1_score/_f1_score.py +5 -5
  67. azure/ai/evaluation/_evaluators/_fluency/_fluency.py +3 -3
  68. azure/ai/evaluation/_evaluators/_gleu/_gleu.py +4 -4
  69. azure/ai/evaluation/_evaluators/_groundedness/_groundedness.py +12 -8
  70. azure/ai/evaluation/_evaluators/_intent_resolution/_intent_resolution.py +31 -26
  71. azure/ai/evaluation/_evaluators/_intent_resolution/intent_resolution.prompty +210 -96
  72. azure/ai/evaluation/_evaluators/_meteor/_meteor.py +3 -4
  73. azure/ai/evaluation/_evaluators/_protected_material/_protected_material.py +14 -7
  74. azure/ai/evaluation/_evaluators/_qa/_qa.py +5 -5
  75. azure/ai/evaluation/_evaluators/_relevance/_relevance.py +62 -15
  76. azure/ai/evaluation/_evaluators/_relevance/relevance.prompty +140 -59
  77. azure/ai/evaluation/_evaluators/_response_completeness/_response_completeness.py +21 -26
  78. azure/ai/evaluation/_evaluators/_retrieval/_retrieval.py +5 -5
  79. azure/ai/evaluation/_evaluators/_rouge/_rouge.py +22 -22
  80. azure/ai/evaluation/_evaluators/_service_groundedness/_service_groundedness.py +7 -6
  81. azure/ai/evaluation/_evaluators/_similarity/_similarity.py +4 -4
  82. azure/ai/evaluation/_evaluators/_task_adherence/_task_adherence.py +27 -24
  83. azure/ai/evaluation/_evaluators/_task_adherence/task_adherence.prompty +354 -66
  84. azure/ai/evaluation/_evaluators/_tool_call_accuracy/_tool_call_accuracy.py +175 -183
  85. azure/ai/evaluation/_evaluators/_tool_call_accuracy/tool_call_accuracy.prompty +99 -21
  86. azure/ai/evaluation/_evaluators/_ungrounded_attributes/_ungrounded_attributes.py +20 -12
  87. azure/ai/evaluation/_evaluators/_xpia/xpia.py +10 -7
  88. azure/ai/evaluation/_exceptions.py +10 -0
  89. azure/ai/evaluation/_http_utils.py +3 -3
  90. azure/ai/evaluation/_legacy/_batch_engine/_config.py +6 -3
  91. azure/ai/evaluation/_legacy/_batch_engine/_engine.py +117 -32
  92. azure/ai/evaluation/_legacy/_batch_engine/_openai_injector.py +5 -2
  93. azure/ai/evaluation/_legacy/_batch_engine/_result.py +2 -0
  94. azure/ai/evaluation/_legacy/_batch_engine/_run.py +2 -2
  95. azure/ai/evaluation/_legacy/_batch_engine/_run_submitter.py +33 -41
  96. azure/ai/evaluation/_legacy/_batch_engine/_utils.py +1 -4
  97. azure/ai/evaluation/_legacy/_common/_async_token_provider.py +12 -19
  98. azure/ai/evaluation/_legacy/_common/_thread_pool_executor_with_context.py +2 -0
  99. azure/ai/evaluation/_legacy/prompty/_prompty.py +11 -5
  100. azure/ai/evaluation/_safety_evaluation/__init__.py +1 -1
  101. azure/ai/evaluation/_safety_evaluation/_safety_evaluation.py +195 -111
  102. azure/ai/evaluation/_user_agent.py +32 -1
  103. azure/ai/evaluation/_version.py +1 -1
  104. azure/ai/evaluation/red_team/__init__.py +3 -1
  105. azure/ai/evaluation/red_team/_agent/__init__.py +1 -1
  106. azure/ai/evaluation/red_team/_agent/_agent_functions.py +68 -71
  107. azure/ai/evaluation/red_team/_agent/_agent_tools.py +103 -145
  108. azure/ai/evaluation/red_team/_agent/_agent_utils.py +26 -6
  109. azure/ai/evaluation/red_team/_agent/_semantic_kernel_plugin.py +62 -71
  110. azure/ai/evaluation/red_team/_attack_objective_generator.py +94 -52
  111. azure/ai/evaluation/red_team/_attack_strategy.py +2 -1
  112. azure/ai/evaluation/red_team/_callback_chat_target.py +4 -9
  113. azure/ai/evaluation/red_team/_default_converter.py +1 -1
  114. azure/ai/evaluation/red_team/_red_team.py +1947 -1040
  115. azure/ai/evaluation/red_team/_red_team_result.py +49 -38
  116. azure/ai/evaluation/red_team/_utils/__init__.py +1 -1
  117. azure/ai/evaluation/red_team/_utils/_rai_service_eval_chat_target.py +39 -34
  118. azure/ai/evaluation/red_team/_utils/_rai_service_target.py +163 -138
  119. azure/ai/evaluation/red_team/_utils/_rai_service_true_false_scorer.py +14 -14
  120. azure/ai/evaluation/red_team/_utils/constants.py +1 -13
  121. azure/ai/evaluation/red_team/_utils/formatting_utils.py +41 -44
  122. azure/ai/evaluation/red_team/_utils/logging_utils.py +17 -17
  123. azure/ai/evaluation/red_team/_utils/metric_mapping.py +31 -4
  124. azure/ai/evaluation/red_team/_utils/strategy_utils.py +33 -25
  125. azure/ai/evaluation/simulator/_adversarial_scenario.py +2 -0
  126. azure/ai/evaluation/simulator/_adversarial_simulator.py +31 -17
  127. azure/ai/evaluation/simulator/_conversation/__init__.py +2 -2
  128. azure/ai/evaluation/simulator/_direct_attack_simulator.py +8 -8
  129. azure/ai/evaluation/simulator/_indirect_attack_simulator.py +18 -6
  130. azure/ai/evaluation/simulator/_model_tools/_generated_rai_client.py +54 -24
  131. azure/ai/evaluation/simulator/_model_tools/_identity_manager.py +7 -1
  132. azure/ai/evaluation/simulator/_model_tools/_proxy_completion_model.py +30 -10
  133. azure/ai/evaluation/simulator/_model_tools/_rai_client.py +19 -31
  134. azure/ai/evaluation/simulator/_model_tools/_template_handler.py +20 -6
  135. azure/ai/evaluation/simulator/_model_tools/models.py +1 -1
  136. azure/ai/evaluation/simulator/_simulator.py +21 -8
  137. {azure_ai_evaluation-1.8.0.dist-info → azure_ai_evaluation-1.10.0.dist-info}/METADATA +46 -3
  138. {azure_ai_evaluation-1.8.0.dist-info → azure_ai_evaluation-1.10.0.dist-info}/RECORD +141 -136
  139. azure/ai/evaluation/_common/onedp/aio/_vendor.py +0 -40
  140. {azure_ai_evaluation-1.8.0.dist-info → azure_ai_evaluation-1.10.0.dist-info}/NOTICE.txt +0 -0
  141. {azure_ai_evaluation-1.8.0.dist-info → azure_ai_evaluation-1.10.0.dist-info}/WHEEL +0 -0
  142. {azure_ai_evaluation-1.8.0.dist-info → azure_ai_evaluation-1.10.0.dist-info}/top_level.txt +0 -0
@@ -49,8 +49,8 @@ class AdversarialSimulator:
49
49
  """
50
50
  Initializes the adversarial simulator with a project scope.
51
51
 
52
- :param azure_ai_project: The Azure AI project, which can either be a string representing the project endpoint
53
- or an instance of AzureAIProject. It contains subscription id, resource group, and project name.
52
+ :param azure_ai_project: The Azure AI project, which can either be a string representing the project endpoint
53
+ or an instance of AzureAIProject. It contains subscription id, resource group, and project name.
54
54
  :type azure_ai_project: Union[str, AzureAIProject]
55
55
  :param credential: The credential for connecting to Azure AI project.
56
56
  :type credential: ~azure.core.credentials.TokenCredential
@@ -77,7 +77,7 @@ class AdversarialSimulator:
77
77
  logger=logging.getLogger("AdversarialSimulator"),
78
78
  credential=self.credential,
79
79
  )
80
- self.rai_client = AIProjectClient(endpoint=azure_ai_project, credential=credential)
80
+ self.rai_client = AIProjectClient(endpoint=azure_ai_project, credential=credential)
81
81
  else:
82
82
  try:
83
83
  self.azure_ai_project = validate_azure_ai_project(azure_ai_project)
@@ -96,7 +96,7 @@ class AdversarialSimulator:
96
96
  credential=self.credential,
97
97
  )
98
98
  self.rai_client = RAIClient(azure_ai_project=self.azure_ai_project, token_manager=self.token_manager)
99
-
99
+
100
100
  self.adversarial_template_handler = AdversarialTemplateHandler(
101
101
  azure_ai_project=self.azure_ai_project, rai_client=self.rai_client
102
102
  )
@@ -239,8 +239,11 @@ class AdversarialSimulator:
239
239
  # So randomize a the selection instead of the parameter list directly,
240
240
  # or a potentially large deep copy.
241
241
  if randomization_seed is not None:
242
- random.seed(randomization_seed)
243
- random.shuffle(templates)
242
+ # Create a local random instance to avoid polluting global state
243
+ local_random = random.Random(randomization_seed)
244
+ local_random.shuffle(templates)
245
+ else:
246
+ random.shuffle(templates)
244
247
 
245
248
  # Prepare task parameters based on scenario - but use a single append call for all scenarios
246
249
  tasks = []
@@ -264,7 +267,9 @@ class AdversarialSimulator:
264
267
 
265
268
  # Limit to max_simulation_results if needed
266
269
  if len(template_parameter_pairs) > max_simulation_results:
267
- template_parameter_pairs = template_parameter_pairs[:max_simulation_results] # Create a seeded random instance for jailbreak selection if randomization_seed is provided
270
+ template_parameter_pairs = template_parameter_pairs[
271
+ :max_simulation_results
272
+ ] # Create a seeded random instance for jailbreak selection if randomization_seed is provided
268
273
  jailbreak_random = None
269
274
  if _jailbreak_type == "upia" and randomization_seed is not None:
270
275
  jailbreak_random = random.Random(randomization_seed)
@@ -364,10 +369,21 @@ class AdversarialSimulator:
364
369
  target=target, role=ConversationRole.ASSISTANT, template=template, parameters=parameters, scenario=scenario
365
370
  )
366
371
  bots = [user_bot, system_bot]
367
-
372
+
373
+ async def run_simulation(session_obj):
374
+ async with semaphore:
375
+ _, conversation_history = await simulate_conversation(
376
+ bots=bots,
377
+ session=session_obj,
378
+ turn_limit=max_conversation_turns,
379
+ api_call_delay_sec=api_call_delay_sec,
380
+ language=language,
381
+ )
382
+ return conversation_history
383
+
368
384
  if isinstance(self.rai_client, AIProjectClient):
369
385
  session = self.rai_client
370
- else:
386
+ else:
371
387
  session = get_async_http_client().with_policies(
372
388
  retry_policy=AsyncRetryPolicy(
373
389
  retry_total=api_call_retry_limit,
@@ -375,13 +391,7 @@ class AdversarialSimulator:
375
391
  retry_mode=RetryMode.Fixed,
376
392
  )
377
393
  )
378
- _, conversation_history = await simulate_conversation(
379
- bots=bots,
380
- session=session,
381
- turn_limit=max_conversation_turns,
382
- api_call_delay_sec=api_call_delay_sec,
383
- language=language,
384
- )
394
+ conversation_history = await run_simulation(session)
385
395
 
386
396
  return self._to_chat_protocol(
387
397
  conversation_history=conversation_history,
@@ -391,7 +401,11 @@ class AdversarialSimulator:
391
401
  def _get_user_proxy_completion_model(
392
402
  self, template_key: str, template_parameters: TemplateParameters, simulation_id: str = ""
393
403
  ) -> ProxyChatCompletionsModel:
394
- endpoint_url = self.rai_client._config.endpoint + "/redTeams/simulation/chat/completions/submit" if isinstance(self.rai_client, AIProjectClient) else self.rai_client.simulation_submit_endpoint
404
+ endpoint_url = (
405
+ self.rai_client._config.endpoint + "/redTeams/simulation/chat/completions/submit"
406
+ if isinstance(self.rai_client, AIProjectClient)
407
+ else self.rai_client.simulation_submit_endpoint
408
+ )
395
409
  return ProxyChatCompletionsModel(
396
410
  name="raisvc_proxy_model",
397
411
  template_key=template_key,
@@ -419,13 +419,13 @@ class MultiModalConversationBot(ConversationBot):
419
419
  contents = []
420
420
  for msg in messages:
421
421
  if msg.startswith("image_understanding/"):
422
- if(isinstance(self.rai_client, RAIClient)):
422
+ if isinstance(self.rai_client, RAIClient):
423
423
  encoded_image = await self.rai_client.get_image_data(msg)
424
424
  else:
425
425
  response = self.rai_client.red_teams.get_template_parameters_image(path=msg, stream="true")
426
426
  image_data = b"".join(response)
427
427
  encoded_image = base64.b64encode(image_data).decode("utf-8")
428
-
428
+
429
429
  contents.append(
430
430
  {"type": "image_url", "image_url": {"url": f"data:image/png;base64,{encoded_image}"}},
431
431
  )
@@ -28,8 +28,8 @@ class DirectAttackSimulator:
28
28
  Initialize a UPIA (user prompt injected attack) jailbreak adversarial simulator with a project scope.
29
29
  This simulator converses with your AI system using prompts designed to interrupt normal functionality.
30
30
 
31
- :param azure_ai_project: The Azure AI project, which can either be a string representing the project endpoint
32
- or an instance of AzureAIProject. It contains subscription id, resource group, and project name.
31
+ :param azure_ai_project: The Azure AI project, which can either be a string representing the project endpoint
32
+ or an instance of AzureAIProject. It contains subscription id, resource group, and project name.
33
33
  :type azure_ai_project: Union[str, AzureAIProject]
34
34
  :param credential: The credential for connecting to Azure AI project.
35
35
  :type credential: ~azure.core.credentials.TokenCredential
@@ -46,16 +46,16 @@ class DirectAttackSimulator:
46
46
 
47
47
  def __init__(self, *, azure_ai_project: Union[str, AzureAIProject], credential: TokenCredential):
48
48
  """Constructor."""
49
-
49
+
50
50
  if is_onedp_project(azure_ai_project):
51
51
  self.azure_ai_project = azure_ai_project
52
- self.credential=cast(TokenCredential, credential)
52
+ self.credential = cast(TokenCredential, credential)
53
53
  self.token_manager = ManagedIdentityAPITokenManager(
54
54
  token_scope=TokenScope.COGNITIVE_SERVICES_MANAGEMENT,
55
55
  logger=logging.getLogger("AdversarialSimulator"),
56
- credential=self.credential
56
+ credential=self.credential,
57
57
  )
58
- self.rai_client = AIProjectClient(endpoint=azure_ai_project, credential=credential)
58
+ self.rai_client = AIProjectClient(endpoint=azure_ai_project, credential=credential)
59
59
  else:
60
60
  try:
61
61
  self.azure_ai_project = validate_azure_ai_project(azure_ai_project)
@@ -67,14 +67,14 @@ class DirectAttackSimulator:
67
67
  category=e.category,
68
68
  blame=e.blame,
69
69
  ) from e
70
- self.credential = cast(TokenCredential, credential)
70
+ self.credential = cast(TokenCredential, credential)
71
71
  self.token_manager = ManagedIdentityAPITokenManager(
72
72
  token_scope=TokenScope.DEFAULT_AZURE_MANAGEMENT,
73
73
  logger=logging.getLogger("AdversarialSimulator"),
74
74
  credential=self.credential,
75
75
  )
76
76
  self.rai_client = RAIClient(azure_ai_project=self.azure_ai_project, token_manager=self.token_manager)
77
-
77
+
78
78
  self.adversarial_template_handler = AdversarialTemplateHandler(
79
79
  azure_ai_project=self.azure_ai_project, rai_client=self.rai_client
80
80
  )
@@ -5,7 +5,8 @@
5
5
  # noqa: E501
6
6
  import asyncio
7
7
  import logging
8
- from typing import Callable, cast, Union
8
+ import random
9
+ from typing import Callable, cast, Union, Optional
9
10
 
10
11
  from tqdm import tqdm
11
12
 
@@ -30,8 +31,8 @@ class IndirectAttackSimulator(AdversarialSimulator):
30
31
  """
31
32
  Initializes the XPIA (cross domain prompt injected attack) jailbreak adversarial simulator with a project scope.
32
33
 
33
- :param azure_ai_project: The Azure AI project, which can either be a string representing the project endpoint
34
- or an instance of AzureAIProject. It contains subscription id, resource group, and project name.
34
+ :param azure_ai_project: The Azure AI project, which can either be a string representing the project endpoint
35
+ or an instance of AzureAIProject. It contains subscription id, resource group, and project name.
35
36
  :type azure_ai_project: Union[str, AzureAIProject]
36
37
  :param credential: The credential for connecting to Azure AI project.
37
38
  :type credential: ~azure.core.credentials.TokenCredential
@@ -51,13 +52,13 @@ class IndirectAttackSimulator(AdversarialSimulator):
51
52
 
52
53
  if is_onedp_project(azure_ai_project):
53
54
  self.azure_ai_project = azure_ai_project
54
- self.credential=cast(TokenCredential, credential)
55
+ self.credential = cast(TokenCredential, credential)
55
56
  self.token_manager = ManagedIdentityAPITokenManager(
56
57
  token_scope=TokenScope.COGNITIVE_SERVICES_MANAGEMENT,
57
58
  logger=logging.getLogger("AdversarialSimulator"),
58
- credential=self.credential
59
+ credential=self.credential,
59
60
  )
60
- self.rai_client = AIProjectClient(endpoint=azure_ai_project, credential=credential)
61
+ self.rai_client = AIProjectClient(endpoint=azure_ai_project, credential=credential)
61
62
  self.adversarial_template_handler = AdversarialTemplateHandler(
62
63
  azure_ai_project=self.azure_ai_project, rai_client=self.rai_client
63
64
  )
@@ -105,6 +106,7 @@ class IndirectAttackSimulator(AdversarialSimulator):
105
106
  api_call_retry_sleep_sec: int = 1,
106
107
  api_call_delay_sec: int = 0,
107
108
  concurrent_async_task: int = 3,
109
+ randomization_seed: Optional[int] = None,
108
110
  **kwargs,
109
111
  ):
110
112
  """
@@ -130,6 +132,9 @@ class IndirectAttackSimulator(AdversarialSimulator):
130
132
  :keyword concurrent_async_task: The number of asynchronous tasks to run concurrently during the simulation.
131
133
  Defaults to 3.
132
134
  :paramtype concurrent_async_task: int
135
+ :keyword randomization_seed: The seed used to randomize prompt selection. If unset, the system's
136
+ default seed is used. Defaults to None.
137
+ :paramtype randomization_seed: Optional[int]
133
138
  :return: A list of dictionaries, each representing a simulated conversation. Each dictionary contains:
134
139
 
135
140
  - 'template_parameters': A dictionary with parameters used in the conversation template,
@@ -190,6 +195,13 @@ class IndirectAttackSimulator(AdversarialSimulator):
190
195
  ncols=100,
191
196
  unit="simulations",
192
197
  )
198
+
199
+ # Apply randomization to templates if seed is provided
200
+ if randomization_seed is not None:
201
+ # Create a local random instance to avoid polluting global state
202
+ local_random = random.Random(randomization_seed)
203
+ local_random.shuffle(templates)
204
+
193
205
  for template in templates:
194
206
  for parameter in template.template_parameters:
195
207
  tasks.append(
@@ -6,6 +6,7 @@ import os
6
6
  from typing import Dict, List, Optional, Union
7
7
 
8
8
  from azure.core.credentials import TokenCredential
9
+ from azure.core.pipeline.policies import UserAgentPolicy
9
10
  from azure.ai.evaluation._model_configurations import AzureAIProject
10
11
  from azure.ai.evaluation.simulator._model_tools import ManagedIdentityAPITokenManager
11
12
  from azure.ai.evaluation._common.raiclient import MachineLearningServicesClient
@@ -13,30 +14,35 @@ from azure.ai.evaluation._constants import TokenScope
13
14
  from azure.ai.evaluation._common.utils import is_onedp_project
14
15
  from azure.ai.evaluation._common.onedp import AIProjectClient
15
16
  from azure.ai.evaluation._common import EvaluationServiceOneDPClient
17
+ from azure.ai.evaluation._user_agent import UserAgentSingleton
16
18
  import jwt
17
19
  import time
18
20
  import ast
19
21
 
22
+
20
23
  class GeneratedRAIClient:
21
24
  """Client for the Responsible AI Service using the auto-generated MachineLearningServicesClient.
22
-
23
- :param azure_ai_project: The scope of the Azure AI project. It contains subscription id, resource group, and project name.
24
- :type azure_ai_project: ~azure.ai.evaluation.AzureAIProject
25
+
26
+ :param azure_ai_project: The Azure AI project, which can either be a string representing the project endpoint
27
+ or an instance of AzureAIProject. It contains subscription id, resource group, and project name.
28
+ :type azure_ai_project: Union[str, ~azure.ai.evaluation.AzureAIProject]
25
29
  :param token_manager: The token manager
26
30
  :type token_manager: ~azure.ai.evaluation.simulator._model_tools._identity_manager.APITokenManager
27
31
  """
28
-
32
+
29
33
  def __init__(self, azure_ai_project: Union[AzureAIProject, str], token_manager: ManagedIdentityAPITokenManager):
30
34
  self.azure_ai_project = azure_ai_project
31
35
  self.token_manager = token_manager
32
-
36
+
37
+ user_agent_policy = UserAgentPolicy(base_user_agent=UserAgentSingleton().value)
38
+
33
39
  if not is_onedp_project(azure_ai_project):
34
40
  # Service URL construction
35
41
  if "RAI_SVC_URL" in os.environ:
36
42
  endpoint = os.environ["RAI_SVC_URL"].rstrip("/")
37
43
  else:
38
44
  endpoint = self._get_service_discovery_url()
39
-
45
+
40
46
  # Create the autogenerated client
41
47
  self._client = MachineLearningServicesClient(
42
48
  endpoint=endpoint,
@@ -46,20 +52,24 @@ class GeneratedRAIClient:
46
52
  credential=self.token_manager,
47
53
  ).rai_svc
48
54
  else:
49
- self._client = AIProjectClient(endpoint=azure_ai_project, credential=token_manager).red_teams
50
- self._operations_client = AIProjectClient(endpoint=azure_ai_project, credential=token_manager).evaluations
51
- self._evaluation_onedp_client = EvaluationServiceOneDPClient(endpoint=azure_ai_project, credential=token_manager)
52
-
55
+ self._client = AIProjectClient(
56
+ endpoint=azure_ai_project, credential=token_manager, user_agent_policy=user_agent_policy
57
+ ).red_teams
58
+ self._evaluation_onedp_client = EvaluationServiceOneDPClient(
59
+ endpoint=azure_ai_project, credential=token_manager, user_agent_policy=user_agent_policy
60
+ )
61
+
53
62
  def _get_service_discovery_url(self):
54
63
  """Get the service discovery URL.
55
-
64
+
56
65
  :return: The service discovery URL
57
66
  :rtype: str
58
67
  """
59
68
  import requests
69
+
60
70
  bearer_token = self._fetch_or_reuse_token(self.token_manager)
61
71
  headers = {"Authorization": f"Bearer {bearer_token}", "Content-Type": "application/json"}
62
-
72
+
63
73
  response = requests.get(
64
74
  f"https://management.azure.com/subscriptions/{self.azure_ai_project['subscription_id']}/"
65
75
  f"resourceGroups/{self.azure_ai_project['resource_group_name']}/"
@@ -68,7 +78,7 @@ class GeneratedRAIClient:
68
78
  headers=headers,
69
79
  timeout=5,
70
80
  )
71
-
81
+
72
82
  if response.status_code != 200:
73
83
  msg = (
74
84
  f"Failed to connect to your Azure AI project. Please check if the project scope is configured "
@@ -79,51 +89,71 @@ class GeneratedRAIClient:
79
89
 
80
90
  # Parse the discovery URL
81
91
  from urllib.parse import urlparse
92
+
82
93
  base_url = urlparse(response.json()["properties"]["discoveryUrl"])
83
94
  return f"{base_url.scheme}://{base_url.netloc}"
84
-
85
- async def get_attack_objectives(self, risk_category: Optional[str] = None, application_scenario: str = None, strategy: Optional[str] = None) -> Dict:
95
+
96
+ async def get_attack_objectives(
97
+ self,
98
+ *,
99
+ risk_type: Optional[str] = None,
100
+ risk_category: Optional[str] = None,
101
+ application_scenario: str = None,
102
+ strategy: Optional[str] = None,
103
+ scan_session_id: Optional[str] = None,
104
+ ) -> Dict:
86
105
  """Get attack objectives using the auto-generated operations.
87
-
106
+
107
+ :param risk_type: Optional risk type to filter the attack objectives
108
+ :type risk_type: Optional[str]
88
109
  :param risk_category: Optional risk category to filter the attack objectives
89
110
  :type risk_category: Optional[str]
90
111
  :param application_scenario: Optional description of the application scenario for context
91
112
  :type application_scenario: str
92
113
  :param strategy: Optional strategy to filter the attack objectives
93
114
  :type strategy: Optional[str]
115
+ :param scan_session_id: Optional unique session ID for the scan
116
+ :type scan_session_id: Optional[str]
94
117
  :return: The attack objectives
95
118
  :rtype: Dict
96
- """
119
+ """
97
120
  try:
98
121
  # Send the request using the autogenerated client
99
122
  response = self._client.get_attack_objectives(
100
- risk_types=[risk_category],
123
+ risk_types=[risk_type],
124
+ risk_category=risk_category,
101
125
  lang="en",
102
126
  strategy=strategy,
127
+ headers={"x-ms-client-request-id": scan_session_id},
103
128
  )
104
129
  return response
105
-
130
+
106
131
  except Exception as e:
107
132
  # Log the exception for debugging purposes
108
133
  import logging
134
+
109
135
  logging.error(f"Error in get_attack_objectives: {str(e)}")
110
136
  raise
111
-
112
- async def get_jailbreak_prefixes(self) -> List[str]:
137
+
138
+ async def get_jailbreak_prefixes(self, scan_session_id: Optional[str] = None) -> List[str]:
113
139
  """Get jailbreak prefixes using the auto-generated operations.
114
-
140
+
141
+ :param scan_session_id: Optional unique session ID for the scan
142
+ :type scan_session_id: Optional[str]
115
143
  :return: The jailbreak prefixes
116
144
  :rtype: List[str]
117
145
  """
118
146
  try:
119
147
  # Send the request using the autogenerated client
120
- response = self._client.get_jail_break_dataset_with_type(type="upia")
148
+ response = self._client.get_jail_break_dataset_with_type(
149
+ type="upia", headers={"x-ms-client-request-id": scan_session_id}
150
+ )
121
151
  if isinstance(response, list):
122
152
  return response
123
153
  else:
124
154
  self.logger.error("Unexpected response format from get_jail_break_dataset_with_type")
125
155
  raise ValueError("Unexpected response format from get_jail_break_dataset_with_type")
126
-
156
+
127
157
  except Exception as e:
128
158
  return [""]
129
159
 
@@ -78,7 +78,13 @@ class APITokenManager(ABC):
78
78
 
79
79
  @abstractmethod
80
80
  def get_token(
81
- self, scopes: Union[str, None] = None, claims: Union[str, None] = None, tenant_id: Union[str, None] = None, enable_cae: bool = False, **kwargs: Any) -> AccessToken:
81
+ self,
82
+ scopes: Union[str, None] = None,
83
+ claims: Union[str, None] = None,
84
+ tenant_id: Union[str, None] = None,
85
+ enable_cae: bool = False,
86
+ **kwargs: Any,
87
+ ) -> AccessToken:
82
88
  """Async method to get the API token. Subclasses should implement this method.
83
89
 
84
90
  :return: API token
@@ -9,8 +9,8 @@ import uuid
9
9
  from typing import Any, Dict, List, Optional, cast, Union
10
10
 
11
11
  from azure.ai.evaluation._http_utils import AsyncHttpPipeline, get_async_http_client
12
- from azure.ai.evaluation._user_agent import USER_AGENT
13
- from azure.core.exceptions import HttpResponseError
12
+ from azure.ai.evaluation._user_agent import UserAgentSingleton
13
+ from azure.core.exceptions import HttpResponseError, ServiceResponseError
14
14
  from azure.core.pipeline.policies import AsyncRetryPolicy, RetryMode
15
15
  from azure.ai.evaluation._common.onedp._client import AIProjectClient
16
16
  from azure.ai.evaluation._common.onedp.models import SimulationDTO
@@ -166,7 +166,7 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
166
166
  proxy_headers = {
167
167
  "Authorization": f"Bearer {token}",
168
168
  "Content-Type": "application/json",
169
- "User-Agent": USER_AGENT,
169
+ "User-Agent": UserAgentSingleton().value,
170
170
  }
171
171
 
172
172
  headers = {
@@ -193,7 +193,7 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
193
193
  time_start = time.time()
194
194
  full_response = None
195
195
 
196
- if(isinstance(session, AIProjectClient)):
196
+ if isinstance(session, AIProjectClient):
197
197
  sim_request_dto = SimulationDTO(
198
198
  headers=headers,
199
199
  params=params,
@@ -203,7 +203,7 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
203
203
  )
204
204
  response_data = session.red_teams.submit_simulation(sim_request_dto, headers=headers, params=params)
205
205
  operation_id = response_data["location"].split("/")[-1]
206
-
206
+
207
207
  request_count = 0
208
208
  flag = True
209
209
  while flag:
@@ -211,6 +211,7 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
211
211
  response = session.evaluations.operation_results(operation_id, headers=headers)
212
212
  except Exception as e:
213
213
  from types import SimpleNamespace # pylint: disable=forgotten-debug-statement
214
+
214
215
  response = SimpleNamespace(status_code=202, text=str(e), json=lambda: {"error": str(e)})
215
216
  if isinstance(response, dict):
216
217
  response_data = response
@@ -224,14 +225,33 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
224
225
  sleep_time = RAIService.SLEEP_TIME**request_count
225
226
  await asyncio.sleep(sleep_time)
226
227
  else:
227
- response = await session.post(url=self.endpoint_url, headers=proxy_headers, json=sim_request_dto.to_dict())
228
+ # Retry policy for POST request to RAI service
229
+ service_call_retry_policy = AsyncRetryPolicy(
230
+ retry_on_exceptions=[ServiceResponseError],
231
+ retry_total=7,
232
+ retry_backoff_factor=10.0,
233
+ retry_backoff_max=180,
234
+ retry_mode=RetryMode.Exponential,
235
+ )
236
+
237
+ response = None
238
+ async with get_async_http_client().with_policies(retry_policy=service_call_retry_policy) as retry_client:
239
+ try:
240
+ response = await retry_client.post(
241
+ url=self.endpoint_url, headers=proxy_headers, json=sim_request_dto.to_dict()
242
+ )
243
+ except ServiceResponseError as e:
244
+ self.logger.error("ServiceResponseError during POST request to rai svc after retries: %s", str(e))
245
+ raise
246
+
228
247
  # response.raise_for_status()
229
248
  if response.status_code != 202:
230
249
  raise HttpResponseError(
231
- message=f"Received unexpected HTTP status: {response.status_code} {response.text()}", response=response
250
+ message=f"Received unexpected HTTP status: {response.status_code} {response.text()}",
251
+ response=response,
232
252
  )
233
253
  response_data = response.json()
234
-
254
+
235
255
  self.result_url = cast(str, response_data["location"])
236
256
  retry_policy = AsyncRetryPolicy( # set up retry configuration
237
257
  retry_on_status_codes=[202], # on which statuses to retry
@@ -252,14 +272,14 @@ class ProxyChatCompletionsModel(OpenAIChatCompletionsModel):
252
272
  proxy_headers = {
253
273
  "Authorization": f"Bearer {token}",
254
274
  "Content-Type": "application/json",
255
- "User-Agent": USER_AGENT,
275
+ "User-Agent": UserAgentSingleton().value,
256
276
  }
257
277
  response = await exp_retry_client.get( # pylint: disable=too-many-function-args,unexpected-keyword-arg
258
278
  self.result_url, headers=proxy_headers
259
279
  )
260
280
  response.raise_for_status()
261
281
  response_data = response.json()
262
-
282
+
263
283
  self.logger.info("Response: %s", response_data)
264
284
 
265
285
  # Copy the full response and return it to be saved in jsonl.
@@ -10,7 +10,7 @@ import json
10
10
  from azure.ai.evaluation._exceptions import ErrorBlame, ErrorCategory, ErrorTarget, EvaluationException
11
11
  from azure.ai.evaluation._http_utils import AsyncHttpPipeline, get_async_http_client, get_http_client
12
12
  from azure.ai.evaluation._model_configurations import AzureAIProject
13
- from azure.ai.evaluation._user_agent import USER_AGENT
13
+ from azure.ai.evaluation._user_agent import UserAgentSingleton
14
14
  from azure.core.pipeline.policies import AsyncRetryPolicy, RetryMode
15
15
 
16
16
  from ._identity_manager import APITokenManager
@@ -148,7 +148,7 @@ class RAIClient: # pylint: disable=client-accepts-api-version-keyword
148
148
  headers = {
149
149
  "Authorization": f"Bearer {token}",
150
150
  "Content-Type": "application/json",
151
- "User-Agent": USER_AGENT,
151
+ "User-Agent": UserAgentSingleton().value,
152
152
  }
153
153
 
154
154
  session = self._create_async_client()
@@ -184,7 +184,7 @@ class RAIClient: # pylint: disable=client-accepts-api-version-keyword
184
184
  headers = {
185
185
  "Authorization": f"Bearer {token}",
186
186
  "Content-Type": "application/json",
187
- "User-Agent": USER_AGENT,
187
+ "User-Agent": UserAgentSingleton().value,
188
188
  }
189
189
 
190
190
  session = self._create_async_client()
@@ -209,9 +209,11 @@ class RAIClient: # pylint: disable=client-accepts-api-version-keyword
209
209
  blame=ErrorBlame.USER_ERROR,
210
210
  )
211
211
 
212
- async def get_attack_objectives(self, risk_categories: List[str], application_scenario: str = None, strategy: str = None) -> Any:
212
+ async def get_attack_objectives(
213
+ self, risk_categories: List[str], application_scenario: str = None, strategy: str = None
214
+ ) -> Any:
213
215
  """Get the attack objectives based on risk categories and application scenario
214
-
216
+
215
217
  :param risk_categories: List of risk categories to generate attack objectives for
216
218
  :type risk_categories: List[str]
217
219
  :param application_scenario: Optional description of the application scenario for context
@@ -224,17 +226,17 @@ class RAIClient: # pylint: disable=client-accepts-api-version-keyword
224
226
  # Create query parameters for the request
225
227
  if application_scenario:
226
228
  raise NotImplementedError("Application scenario is not supported yet")
227
-
229
+
228
230
  params = {
229
231
  "api-version": "2022-11-01-preview",
230
232
  "riskTypes": ",".join(risk_categories),
231
- "lang": "en" # Default to English
233
+ "lang": "en", # Default to English
232
234
  }
233
-
235
+
234
236
  # Add strategy parameter if provided
235
237
  if strategy:
236
238
  params["strategy"] = strategy
237
-
239
+
238
240
  try:
239
241
  # Make the request using the existing get method
240
242
  result = await self.get(self.attack_objectives_endpoint)
@@ -254,25 +256,11 @@ class RAIClient: # pylint: disable=client-accepts-api-version-keyword
254
256
  except Exception:
255
257
  # If the API fails or isn't implemented yet, return a mock response
256
258
  # This is temporary until the API endpoint is fully implemented
257
- return [{
258
- "metadata": {
259
- "lang": "en",
260
- "target_harms": [
261
- {
262
- "risk-type": "violence",
263
- "risk-subtype": ""
264
- }
265
- ]
266
- },
267
- "messages": [
268
- {
269
- "role": "user",
270
- "content": "Risky content"
271
- }
272
- ],
273
- "modality": "text",
274
- "source": [
275
- "source"
276
- ]
277
- }]
278
-
259
+ return [
260
+ {
261
+ "metadata": {"lang": "en", "target_harms": [{"risk-type": "violence", "risk-subtype": ""}]},
262
+ "messages": [{"role": "user", "content": "Risky content"}],
263
+ "modality": "text",
264
+ "source": ["source"],
265
+ }
266
+ ]