@@ -503,6 +503,59 @@ async def pyrit_prompt_chat_target_example(self):
503503 # [END red_team_pyrit_target]
504504 return results
505505
506+ async def custom_objectives_with_context_example (self ):
507+ """
508+ This example demonstrates how to use custom attack objectives with context information.
509+ Context provides additional information about the purpose and setting of prompts,
510+ enabling more accurate evaluation in educational and professional scenarios.
511+ """
512+ # [START red_team_context_objectives]
513+ import os
514+ from azure .ai .evaluation .red_team import RedTeam , AttackStrategy , RiskCategory
515+ from azure .identity import DefaultAzureCredential
516+
517+ # Initialize Red Team
518+ azure_ai_project = {
519+ "subscription_id" : os .environ .get ("AZURE_SUBSCRIPTION_ID" ),
520+ "resource_group_name" : os .environ .get ("AZURE_RESOURCE_GROUP_NAME" ),
521+ "project_name" : os .environ .get ("AZURE_PROJECT_NAME" ),
522+ }
523+ credential = DefaultAzureCredential ()
524+
525+ # Path to custom objectives file with context
526+ custom_objectives_path = "samples/data/custom_objectives_with_context_example.json"
527+
528+ agent = RedTeam (
529+ azure_ai_project = self .azure_ai_project ,
530+ credential = credential ,
531+ risk_categories = [RiskCategory .Violence , RiskCategory .HateUnfairness ],
532+ num_objectives = 2 ,
533+ custom_attack_seed_prompts = custom_objectives_path
534+ )
535+
536+ # Define an educational AI assistant
537+ def educational_ai_assistant (query : str ) -> str :
538+ # Educational AI system that provides informative responses
539+ if "conflict" in query .lower ():
540+ return "Historical conflicts often arise from resource competition and cultural tensions. Understanding root causes helps develop peaceful solutions."
541+ elif "cultural" in query .lower ():
542+ return "Cultural differences enrich workplaces through diverse perspectives. Effective communication bridges these differences."
543+ else :
544+ return "I provide educational information while following ethical guidelines."
545+
546+ # Run the scan with context-aware objectives
547+ results = await agent .scan (
548+ target = educational_ai_assistant ,
549+ scan_name = "Context-Aware-Educational-Test" ,
550+ attack_strategies = [AttackStrategy .Baseline , AttackStrategy .Base64 ],
551+ application_scenario = "Educational AI assistant for academic and training purposes" ,
552+ )
553+
554+ print (f"Context-aware scan completed with { len (results .scan_result ) if results .scan_result else 0 } conversations" )
555+ print ("Context information was used during evaluation for better accuracy" )
556+ # [END red_team_context_objectives]
557+ return results
558+
506559
507560async def run_samples ():
508561 """Run all Red Team samples."""
@@ -521,6 +574,7 @@ async def run_samples():
521574 # samples.output_path_example(),
522575 # samples.custom_application_example(),
523576 # samples.pyrit_prompt_chat_target_example(),
577+ # samples.custom_objectives_with_context_example(),
524578 ]
525579
526580 # Run the selected samples
0 commit comments