Skip to content

Commit a893b79

Browse files
authored
Tox black code reformatting
Fix "tox -e black" formatting issues
1 parent 45cab16 commit a893b79

File tree

10 files changed

+294
-180
lines changed

10 files changed

+294
-180
lines changed

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,5 @@
6464
"EvaluatorConfig",
6565
"Conversation",
6666
"Message",
67-
"EvaluationResult"
67+
"EvaluationResult",
6868
]

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/_azure/_models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Workspace(Model):
5252
"agents_endpoint_uri": {"readonly": True},
5353
"ml_flow_tracking_uri": {"readonly": True},
5454
#'notebook_info': {'readonly': True},
55-
#"private_endpoint_connections": {"readonly": True},
55+
# "private_endpoint_connections": {"readonly": True},
5656
#'private_link_count': {'readonly': True},
5757
"provisioning_state": {"readonly": True},
5858
"service_provisioned_resource_group": {"readonly": True},
@@ -99,10 +99,10 @@ class Workspace(Model):
9999
#'network_acls': {'key': 'properties.networkAcls', 'type': 'NetworkAcls'},
100100
#'notebook_info': {'key': 'properties.notebookInfo', 'type': 'NotebookResourceInfo'},
101101
"primary_user_assigned_identity": {"key": "properties.primaryUserAssignedIdentity", "type": "str"},
102-
#"private_endpoint_connections": {
102+
# "private_endpoint_connections": {
103103
# "key": "properties.privateEndpointConnections",
104104
# "type": "[PrivateEndpointConnection]",
105-
#},
105+
# },
106106
"private_link_count": {"key": "properties.privateLinkCount", "type": "int"},
107107
"provision_network_now": {"key": "properties.provisionNetworkNow", "type": "bool"},
108108
"provisioning_state": {"key": "properties.provisioningState", "type": "str"},

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/_evaluate/_evaluate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,10 @@ def _apply_target_to_data(
486486
run_summary = batch_client.get_run_summary(run)
487487

488488
if run_summary["completed_lines"] == 0:
489-
msg = (f"Evaluation target failed to produce any results."
490-
f" Please check the logs at {run_summary['log_path']} for more details about cause of failure.")
489+
msg = (
490+
f"Evaluation target failed to produce any results."
491+
f" Please check the logs at {run_summary['log_path']} for more details about cause of failure."
492+
)
491493
raise EvaluationException(
492494
message=msg,
493495
target=ErrorTarget.EVALUATE,

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/_safety_evaluation/_safety_evaluation.py

Lines changed: 220 additions & 123 deletions
Large diffs are not rendered by default.

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/simulator/_adversarial_simulator.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ async def __call__(
224224
# Prepare task parameters based on scenario - but use a single append call for all scenarios
225225
tasks = []
226226
template_parameter_pairs = []
227-
227+
228228
if scenario == AdversarialScenario.ADVERSARIAL_CONVERSATION:
229229
# For ADVERSARIAL_CONVERSATION, flatten the parameters
230230
for i, template in enumerate(templates):
@@ -236,20 +236,20 @@ async def __call__(
236236
# Use original logic for other scenarios - zip parameters
237237
parameter_lists = [t.template_parameters for t in templates]
238238
zipped_parameters = list(zip(*parameter_lists))
239-
239+
240240
for param_group in zipped_parameters:
241241
for template, parameter in zip(templates, param_group):
242242
template_parameter_pairs.append((template, parameter))
243243

244244
# Limit to max_simulation_results if needed
245245
if len(template_parameter_pairs) > max_simulation_results:
246246
template_parameter_pairs = template_parameter_pairs[:max_simulation_results]
247-
247+
248248
# Single task append loop for all scenarios
249249
for template, parameter in template_parameter_pairs:
250250
if _jailbreak_type == "upia":
251251
parameter = self._add_jailbreak_parameter(parameter, random.choice(jailbreak_dataset))
252-
252+
253253
tasks.append(
254254
asyncio.create_task(
255255
self._simulate_async(
@@ -326,7 +326,11 @@ async def _simulate_async(
326326
simulation_id: str = "",
327327
) -> List[Dict]:
328328
user_bot = self._setup_bot(
329-
role=ConversationRole.USER, template=template, parameters=parameters, scenario=scenario, simulation_id=simulation_id
329+
role=ConversationRole.USER,
330+
template=template,
331+
parameters=parameters,
332+
scenario=scenario,
333+
simulation_id=simulation_id,
330334
)
331335
system_bot = self._setup_bot(
332336
target=target, role=ConversationRole.ASSISTANT, template=template, parameters=parameters, scenario=scenario
@@ -381,7 +385,9 @@ def _setup_bot(
381385
) -> ConversationBot:
382386
if role is ConversationRole.USER:
383387
model = self._get_user_proxy_completion_model(
384-
template_key=template.template_name, template_parameters=parameters, simulation_id=simulation_id,
388+
template_key=template.template_name,
389+
template_parameters=parameters,
390+
simulation_id=simulation_id,
385391
)
386392
return ConversationBot(
387393
role=role,

sdk/evaluation/azure-ai-evaluation/setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
"azure-identity>=1.16.0",
7272
"azure-core>=1.30.2",
7373
"nltk>=3.9.1",
74-
"azure-storage-blob>=12.10.0"
74+
"azure-storage-blob>=12.10.0",
7575
],
7676
project_urls={
7777
"Bug Reports": "https://github.com/Azure/azure-sdk-for-python/issues",

sdk/evaluation/azure-ai-evaluation/tests/e2etests/test_evaluate.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ def questions_file():
3434
data_path = os.path.join(pathlib.Path(__file__).parent.resolve(), "data")
3535
return os.path.join(data_path, "questions.jsonl")
3636

37+
3738
def answer_evaluator(response):
3839
return {"length": len(response)}
3940

sdk/evaluation/azure-ai-evaluation/tests/e2etests/test_mass_evaluate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class TestMassEvaluate:
7373
- Conversation inputs: Where a conversation is inputted and the relevant inputs are extracted.
7474
- Multi-modal inputs: This one has some parameters for the different types of multi-modal inputs.
7575
"""
76-
76+
7777
@pytest.mark.skipif(not is_live(), reason="Skip in playback due to inconsistency in evaluation results.")
7878
def test_evaluate_singleton_inputs(self, model_config, azure_cred, project_scope, data_file):
7979
# qa fails in playback but ONLY when using the pf proxy for some reason, and

sdk/evaluation/azure-ai-evaluation/tests/unittests/test_evaluate.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,11 @@ def _target_fn2(query):
118118
response["query"] = f"The query is as follows: {query}"
119119
return response
120120

121+
121122
def _target_that_fails(query):
122123
raise Exception("I am failing")
123124

125+
124126
def _new_answer_target():
125127
return {"response": "new response"}
126128

@@ -390,7 +392,7 @@ def test_apply_column_mapping_target(self, json_data, inputs_mapping, response):
390392
{"query": "${data.query"},
391393
{"query": "data.query", "response": "target.response"},
392394
{"query": "${data.query}", "response": "${target.response.one}"},
393-
]
395+
],
394396
)
395397
def test_evaluate_invalid_column_mapping(self, mock_model_config, evaluate_test_data_jsonl_file, column_mapping):
396398
# Invalid source reference
@@ -402,12 +404,12 @@ def test_evaluate_invalid_column_mapping(self, mock_model_config, evaluate_test_
402404
"g": {
403405
"column_mapping": column_mapping,
404406
}
405-
}
407+
},
406408
)
407409

408410
assert (
409-
"Unexpected references detected in 'column_mapping'. Ensure only ${target.} and ${data.} are used."
410-
in exc_info.value.args[0]
411+
"Unexpected references detected in 'column_mapping'. Ensure only ${target.} and ${data.} are used."
412+
in exc_info.value.args[0]
411413
)
412414

413415
def test_renaming_column(self):

sdk/evaluation/azure-ai-evaluation/tests/unittests/test_safety_evaluation.py

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@
77
from azure.ai.evaluation.simulator._utils import JsonLineChatProtocol, JsonLineList
88
from azure.core.credentials import TokenCredential
99

10+
1011
@pytest.fixture
1112
def mock_credential():
1213
return MagicMock(spec=TokenCredential)
1314

15+
1416
@pytest.fixture
1517
def mock_model_config_dict_valid():
1618
return {
@@ -19,51 +21,58 @@ def mock_model_config_dict_valid():
1921
"type": "azure_openai",
2022
}
2123

24+
2225
@pytest.fixture
2326
def mock_model_config_dict_invalid():
2427
return {
2528
"type": "azure_openai",
2629
}
2730

31+
2832
@pytest.fixture
2933
def mock_target():
3034
def mock_target_fn() -> str:
3135
return "mock response"
36+
3237
return mock_target_fn
3338

39+
3440
@pytest.fixture
3541
def mock_target_with_context():
3642
def mock_target_with_context_fn() -> tuple:
3743
return ("mock response", "mock context")
44+
3845
return mock_target_with_context_fn
3946

47+
4048
@pytest.fixture
4149
def mock_eval_result_dict():
4250
jailbreak = {
43-
'rows': [
51+
"rows": [
4452
{
45-
'outputs.content_safety.hate_unfairness_score': 0.0,
46-
'outputs.content_safety.self_harm_score': 0.0,
47-
'outputs.content_safety.sexual_score': 0.0,
48-
'outputs.content_safety.violence_score': 0.0,
53+
"outputs.content_safety.hate_unfairness_score": 0.0,
54+
"outputs.content_safety.self_harm_score": 0.0,
55+
"outputs.content_safety.sexual_score": 0.0,
56+
"outputs.content_safety.violence_score": 0.0,
4957
}
5058
],
51-
'metrics': {},
52-
'studio_url': 'some url'
59+
"metrics": {},
60+
"studio_url": "some url",
5361
}
5462
regular = {
55-
'rows': [
63+
"rows": [
5664
{
57-
'outputs.content_safety.hate_unfairness_score': 0.0,
58-
'outputs.content_safety.self_harm_score': 0.0,
59-
'outputs.content_safety.sexual_score': 0.0,
60-
'outputs.content_safety.violence_score': 0.0,
65+
"outputs.content_safety.hate_unfairness_score": 0.0,
66+
"outputs.content_safety.self_harm_score": 0.0,
67+
"outputs.content_safety.sexual_score": 0.0,
68+
"outputs.content_safety.violence_score": 0.0,
6169
}
6270
],
63-
'metrics': {},
64-
'studio_url': 'some url'
71+
"metrics": {},
72+
"studio_url": "some url",
6573
}
66-
return {'jailbreak': jailbreak , 'regular': regular}
74+
return {"jailbreak": jailbreak, "regular": regular}
75+
6776

6877
@pytest.fixture
6978
def safety_eval(mock_model_config_dict_valid, mock_credential):
@@ -73,13 +82,15 @@ def safety_eval(mock_model_config_dict_valid, mock_credential):
7382
model_config=mock_model_config_dict_valid,
7483
)
7584

85+
7686
@pytest.fixture
7787
def safety_eval_no_model_config(mock_credential):
7888
return _SafetyEvaluation(
7989
azure_ai_project={"subscription_id": "mock-sub", "resource_group_name": "mock-rg", "project_name": "mock-proj"},
8090
credential=mock_credential,
8191
)
8292

93+
8394
@pytest.mark.usefixtures("mock_model_config")
8495
@pytest.mark.unittest
8596
class TestSafetyEvaluation:
@@ -91,7 +102,7 @@ def test_validate_model_config_missing_keys(self, mock_credential, mock_model_co
91102
model_config=mock_model_config_dict_invalid,
92103
)
93104
assert "missing required keys" in str(exc_info.value)
94-
105+
95106
def test_get_evaluators_invalid(self, safety_eval):
96107
with pytest.raises(EvaluationException) as exc_info:
97108
safety_eval._get_evaluators([None]) # type: ignore
@@ -116,12 +127,11 @@ def test_validate_inputs_groundedness_no_source(self, safety_eval, mock_target):
116127
source_text=None,
117128
)
118129
assert "requires either source_text" in str(exc_info.value)
119-
130+
120131
def test_validate_inputs_multi_turn_scenario(self, safety_eval, mock_target):
121132
with pytest.raises(EvaluationException) as exc_info:
122133
safety_eval._validate_inputs(
123134
target=mock_target,
124-
125135
evaluators=[_SafetyEvaluator.CONTENT_SAFETY],
126136
scenario=AdversarialScenario.ADVERSARIAL_SUMMARIZATION,
127137
num_turns=3,
@@ -135,8 +145,7 @@ def test_validate_inputs_no_model_config(self, safety_eval_no_model_config, mock
135145
evaluators=[_SafetyEvaluator.COHERENCE],
136146
)
137147
assert "Model configuration is required" in str(exc_info.value)
138-
139-
148+
140149
def test_validate_inputs_scenario_not_content_safety(self, safety_eval, mock_target):
141150
with pytest.raises(EvaluationException) as exc_info:
142151
safety_eval._validate_inputs(
@@ -145,17 +154,17 @@ def test_validate_inputs_scenario_not_content_safety(self, safety_eval, mock_tar
145154
scenario=AdversarialScenario.ADVERSARIAL_SUMMARIZATION,
146155
)
147156
assert "not supported without content safety evaluation" in str(exc_info.value)
148-
157+
149158
def test_calculate_defect_rate(self, safety_eval, mock_eval_result_dict):
150159
eval_result = safety_eval._calculate_defect_rate(mock_eval_result_dict)
151160
assert eval_result is not None
152-
assert isinstance(eval_result['metrics']['content_safety.violence_defect_rate'], float)
153-
assert eval_result['metrics']['content_safety.violence_defect_rate'] == 0.0
154-
161+
assert isinstance(eval_result["metrics"]["content_safety.violence_defect_rate"], float)
162+
assert eval_result["metrics"]["content_safety.violence_defect_rate"] == 0.0
163+
155164
@pytest.mark.asyncio
156165
@patch("azure.ai.evaluation.simulator._simulator.Simulator.__call__", new_callable=AsyncMock)
157166
async def test_simulate_no_scenario(self, mock__call__, safety_eval, mock_target):
158-
mock__call__.return_value = [JsonLineChatProtocol({"messages":[]})]
167+
mock__call__.return_value = [JsonLineChatProtocol({"messages": []})]
159168
results = await safety_eval._simulate(target=mock_target)
160169
assert isinstance(results, dict)
161170
assert isinstance(results["regular"], str)
@@ -164,27 +173,26 @@ async def test_simulate_no_scenario(self, mock__call__, safety_eval, mock_target
164173
@patch("azure.ai.evaluation.simulator.DirectAttackSimulator.__init__", return_value=None)
165174
@patch("azure.ai.evaluation.simulator.DirectAttackSimulator.__call__", new_callable=AsyncMock)
166175
async def test_simulate_direct_attack(self, mock_call, mock_init, safety_eval, mock_target):
167-
mock_call.return_value = {"jailbreak":JsonLineList([{"messages": []}]),"regular":JsonLineList([{"messages": []}])}
176+
mock_call.return_value = {
177+
"jailbreak": JsonLineList([{"messages": []}]),
178+
"regular": JsonLineList([{"messages": []}]),
179+
}
168180

169181
results = await safety_eval._simulate(
170-
target=mock_target,
171-
direct_attack=True,
172-
adversarial_scenario=AdversarialScenario.ADVERSARIAL_QA
182+
target=mock_target, direct_attack=True, adversarial_scenario=AdversarialScenario.ADVERSARIAL_QA
173183
)
174184
assert isinstance(results, dict)
175185
assert isinstance(results["regular"], str)
176186
assert isinstance(results["jailbreak"], str)
177187

178-
179188
@pytest.mark.asyncio
180189
@patch("azure.ai.evaluation.simulator.IndirectAttackSimulator.__init__", return_value=None)
181190
@patch("azure.ai.evaluation.simulator.IndirectAttackSimulator.__call__", new_callable=AsyncMock)
182191
async def test_simulate_indirect_jailbreak(self, mock_call, mock_init, safety_eval, mock_target):
183-
mock_call.return_value = JsonLineList([{"messages":[]}])
192+
mock_call.return_value = JsonLineList([{"messages": []}])
184193

185194
results = await safety_eval._simulate(
186-
target=mock_target,
187-
adversarial_scenario=AdversarialScenarioJailbreak.ADVERSARIAL_INDIRECT_JAILBREAK
195+
target=mock_target, adversarial_scenario=AdversarialScenarioJailbreak.ADVERSARIAL_INDIRECT_JAILBREAK
188196
)
189197
assert isinstance(results, dict)
190198
assert isinstance(results["regular"], str)
@@ -193,10 +201,9 @@ async def test_simulate_indirect_jailbreak(self, mock_call, mock_init, safety_ev
193201
@patch("azure.ai.evaluation.simulator.AdversarialSimulator.__init__", return_value=None)
194202
@patch("azure.ai.evaluation.simulator.AdversarialSimulator.__call__", new_callable=AsyncMock)
195203
async def test_simulate_adversarial(self, mock_call, mock_init, safety_eval, mock_target):
196-
mock_call.return_value = JsonLineList([{"messages":[]}])
204+
mock_call.return_value = JsonLineList([{"messages": []}])
197205
results = await safety_eval._simulate(
198-
target=mock_target,
199-
adversarial_scenario=AdversarialScenario.ADVERSARIAL_QA
206+
target=mock_target, adversarial_scenario=AdversarialScenario.ADVERSARIAL_QA
200207
)
201208
assert isinstance(results, dict)
202209
assert isinstance(results["regular"], str)
@@ -209,7 +216,6 @@ async def test_simulate_no_results(self, mock_call, mock_init, safety_eval, mock
209216
mock_call.return_value = None
210217
with pytest.raises(EvaluationException) as exc_info:
211218
results = await safety_eval._simulate(
212-
target=mock_target,
213-
adversarial_scenario=AdversarialScenario.ADVERSARIAL_QA
219+
target=mock_target, adversarial_scenario=AdversarialScenario.ADVERSARIAL_QA
214220
)
215-
assert "outputs generated by the simulator" in str(exc_info.value)
221+
assert "outputs generated by the simulator" in str(exc_info.value)

0 commit comments

Comments
 (0)