Skip to content

Commit 8bbd8df

Browse files
Nagkumar ArkalgudNagkumar Arkalgud
authored andcommitted
Adding scan_Session_id
1 parent 8b7f40f commit 8bbd8df

File tree

3 files changed

+26
-5
lines changed

3 files changed

+26
-5
lines changed

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/_common/rai_service.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ async def submit_request(
259259

260260

261261
async def submit_request_onedp(
262-
client: AIProjectClient, data: dict, metric: str, token: str, annotation_task: str, evaluator_name: str
262+
client: AIProjectClient, data: dict, metric: str, token: str, annotation_task: str, evaluator_name: str, scan_session_id: Optional[str] = None
263263
) -> str:
264264
"""Submit request to Responsible AI service for evaluation and return operation ID
265265
@@ -275,12 +275,16 @@ async def submit_request_onedp(
275275
:type annotation_task: str
276276
:param evaluator_name: The evaluator name.
277277
:type evaluator_name: str
278+
:param scan_session_id: The scan session ID to use for the evaluation.
279+
:type scan_session_id: Optional[str]
278280
:return: The operation ID.
279281
:rtype: str
280282
"""
281283
normalized_user_text = get_formatted_template(data, annotation_task)
282284
payload = generate_payload(normalized_user_text, metric, annotation_task=annotation_task)
283285
headers = get_common_headers(token, evaluator_name)
286+
if scan_session_id:
287+
headers["client_request_id"] = scan_session_id
284288
response = client.evaluations.submit_annotation(payload, headers=headers)
285289
result = json.loads(response)
286290
operation_id = result["location"].split("/")[-1]
@@ -631,6 +635,7 @@ async def evaluate_with_rai_service(
631635
annotation_task: str = Tasks.CONTENT_HARM,
632636
metric_display_name=None,
633637
evaluator_name=None,
638+
scan_session_id: Optional[str]=None
634639
) -> Dict[str, Union[str, float]]:
635640
"""Evaluate the content safety of the response using Responsible AI service
636641
@@ -649,6 +654,8 @@ async def evaluate_with_rai_service(
649654
:type metric_display_name: str
650655
:param evaluator_name: The evaluator name to use.
651656
:type evaluator_name: str
657+
:param scan_session_id: The scan session ID to use for the evaluation.
658+
:type scan_session_id: Optional[str]
652659
:return: The parsed annotation result.
653660
:rtype: Dict[str, Union[str, float]]
654661
"""
@@ -661,7 +668,7 @@ async def evaluate_with_rai_service(
661668
)
662669
token = await fetch_or_reuse_token(credential=credential, workspace=COG_SRV_WORKSPACE)
663670
await ensure_service_availability_onedp(client, token, annotation_task)
664-
operation_id = await submit_request_onedp(client, data, metric_name, token, annotation_task, evaluator_name)
671+
operation_id = await submit_request_onedp(client, data, metric_name, token, annotation_task, evaluator_name, scan_session_id)
665672
annotation_response = cast(List[Dict], await fetch_result_onedp(client, operation_id, token))
666673
result = parse_response(annotation_response, metric_name, metric_display_name)
667674
return result

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/red_team/_red_team.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ def __init__(
292292
self.failed_tasks = 0
293293
self.start_time = None
294294
self.scan_id = None
295+
self.scan_session_id = None
295296
self.scan_output_dir = None
296297

297298
self.generated_rai_client = GeneratedRAIClient(azure_ai_project=self.azure_ai_project, token_manager=self.token_manager.credential) # type: ignore
@@ -759,13 +760,15 @@ async def get_jailbreak_prefixes_with_retry():
759760
risk_category=other_risk,
760761
application_scenario=application_scenario or "",
761762
strategy="tense",
763+
scan_session_id=self.scan_session_id
762764
)
763765
else:
764766
objectives_response = await self.generated_rai_client.get_attack_objectives(
765767
risk_type=content_harm_risk,
766768
risk_category=other_risk,
767769
application_scenario=application_scenario or "",
768770
strategy=None,
771+
scan_session_id=self.scan_session_id
769772
)
770773
if isinstance(objectives_response, list):
771774
self.logger.debug(f"API returned {len(objectives_response)} objectives")
@@ -775,7 +778,7 @@ async def get_jailbreak_prefixes_with_retry():
775778
# Handle jailbreak strategy - need to apply jailbreak prefixes to messages
776779
if strategy == "jailbreak":
777780
self.logger.debug("Applying jailbreak prefixes to objectives")
778-
jailbreak_prefixes = await self.generated_rai_client.get_jailbreak_prefixes()
781+
jailbreak_prefixes = await self.generated_rai_client.get_jailbreak_prefixes(scan_session_id=self.scan_session_id)
779782
for objective in objectives_response:
780783
if "messages" in objective and len(objective["messages"]) > 0:
781784
message = objective["messages"][0]
@@ -2350,6 +2353,7 @@ async def evaluate_with_rai_service_with_retry():
23502353
project_scope=self.azure_ai_project,
23512354
credential=self.credential,
23522355
annotation_task=annotation_task,
2356+
scan_session_id=self.scan_session_id,
23532357
)
23542358
except (
23552359
httpx.ConnectTimeout,
@@ -2737,6 +2741,8 @@ async def scan(
27372741
)
27382742
self.scan_id = self.scan_id.replace(" ", "_")
27392743

2744+
self.scan_session_id = str(uuid.uuid4()) # Unique session ID for this scan
2745+
27402746
# Create output directory for this scan
27412747
# If DEBUG environment variable is set, use a regular folder name; otherwise, use a hidden folder
27422748
is_debug = os.environ.get("DEBUG", "").lower() in ("true", "1", "yes", "y")

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/simulator/_model_tools/_generated_rai_client.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ async def get_attack_objectives(
102102
risk_category: Optional[str] = None,
103103
application_scenario: str = None,
104104
strategy: Optional[str] = None,
105+
scan_session_id: Optional[str] = None,
105106
) -> Dict:
106107
"""Get attack objectives using the auto-generated operations.
107108
@@ -113,6 +114,8 @@ async def get_attack_objectives(
113114
:type application_scenario: str
114115
:param strategy: Optional strategy to filter the attack objectives
115116
:type strategy: Optional[str]
117+
:param scan_session_id: Optional unique session ID for the scan
118+
:type scan_session_id: Optional[str]
116119
:return: The attack objectives
117120
:rtype: Dict
118121
"""
@@ -123,6 +126,7 @@ async def get_attack_objectives(
123126
risk_categories=[risk_category],
124127
lang="en",
125128
strategy=strategy,
129+
headers={"client_request_id": scan_session_id},
126130
)
127131
return response
128132

@@ -133,15 +137,19 @@ async def get_attack_objectives(
133137
logging.error(f"Error in get_attack_objectives: {str(e)}")
134138
raise
135139

136-
async def get_jailbreak_prefixes(self) -> List[str]:
140+
async def get_jailbreak_prefixes(self, scan_session_id: Optional[str] = None) -> List[str]:
137141
"""Get jailbreak prefixes using the auto-generated operations.
138142
143+
:param scan_session_id: Optional unique session ID for the scan
144+
:type scan_session_id: Optional[str]
139145
:return: The jailbreak prefixes
140146
:rtype: List[str]
141147
"""
142148
try:
143149
# Send the request using the autogenerated client
144-
response = self._client.get_jail_break_dataset_with_type(type="upia")
150+
response = self._client.get_jail_break_dataset_with_type(
151+
type="upia", headers={"client_request_id": scan_session_id}
152+
)
145153
if isinstance(response, list):
146154
return response
147155
else:

0 commit comments

Comments
 (0)