-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Added Red Team tests and pushed new recordings #42711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
posaninagendra
merged 9 commits into
Azure:main
from
posaninagendra:naposani/redteamtests
Aug 28, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bf6cbc0
Added RedTeam tests
posaninagendra 611b378
updated tests
posaninagendra 27b175e
pushed assets
posaninagendra 1ea9837
Update sdk/ai/azure-ai-projects/tests/test_redteams_async.py
posaninagendra 92be878
Update sdk/ai/azure-ai-projects/tests/test_redteams.py
posaninagendra 4727b9d
Update sdk/ai/azure-ai-projects/tests/test_base.py
posaninagendra a96a8e1
Update connection_name to a fixed value
posaninagendra fbe38ae
Add test_redteams_params to TestBase class
posaninagendra bf7309b
Add connection parameters for Azure OpenAI test
posaninagendra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
|
||
from azure.ai.projects import AIProjectClient | ||
from azure.ai.projects.models import ( | ||
RedTeam, | ||
AzureOpenAIModelConfiguration, | ||
AttackStrategy, | ||
RiskCategory, | ||
) | ||
from test_base import TestBase, servicePreparer | ||
from devtools_testutils import recorded_by_proxy | ||
|
||
|
||
class TestRedTeams(TestBase): | ||
|
||
# To run this test, use the following command in the \sdk\ai\azure-ai-projects folder: | ||
# cls & pytest tests\test_redteams.py::TestRedTeams::test_red_teams -s | ||
@servicePreparer() | ||
@recorded_by_proxy | ||
def test_red_teams(self, **kwargs): | ||
|
||
endpoint = kwargs.pop("azure_ai_projects_tests_project_endpoint") | ||
print("\n=====> Endpoint:", endpoint) | ||
|
||
connection_name = self.test_redteams_params["connection_name"] | ||
model_deployment_name = self.test_redteams_params["model_deployment_name"] | ||
|
||
with AIProjectClient( | ||
endpoint=endpoint, | ||
credential=self.get_credential(AIProjectClient, is_async=False), | ||
) as project_client: | ||
|
||
# [START red_team_sample] | ||
print("Creating a Red Team scan for direct model testing") | ||
|
||
# Create target configuration for testing an Azure OpenAI model | ||
target_config = AzureOpenAIModelConfiguration(model_deployment_name=f"{connection_name}/{model_deployment_name}") | ||
|
||
# Create the Red Team configuration | ||
red_team = RedTeam( | ||
attack_strategies=[AttackStrategy.BASE64], | ||
risk_categories=[RiskCategory.VIOLENCE], | ||
display_name="redteamtest1", # Use a simpler name | ||
target=target_config, | ||
) | ||
|
||
# Create and run the Red Team scan | ||
red_team_response = project_client.red_teams.create(red_team=red_team) | ||
print(f"Red Team scan created with scan name: {red_team_response.name}") | ||
TestBase.validate_red_team_response(red_team_response, expected_attack_strategies=1, expected_risk_categories=1) | ||
|
||
print("Getting Red Team scan details") | ||
# Use the name returned by the create operation for the get call | ||
get_red_team_response = project_client.red_teams.get(name=red_team_response.name) | ||
print(f"Red Team scan status: {get_red_team_response.status}") | ||
TestBase.validate_red_team_response(get_red_team_response, expected_attack_strategies=1, expected_risk_categories=1) | ||
|
||
print("Listing all Red Team scans") | ||
for scan in project_client.red_teams.list(): | ||
print(f"Found scan: {scan.name}, Status: {scan.status}") | ||
TestBase.validate_red_team_response(scan) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
# ------------------------------------ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
# ------------------------------------ | ||
|
||
from azure.ai.projects.aio import AIProjectClient | ||
from azure.ai.projects.models import ( | ||
RedTeam, | ||
AzureOpenAIModelConfiguration, | ||
AttackStrategy, | ||
RiskCategory, | ||
) | ||
from test_base import TestBase, servicePreparer | ||
from devtools_testutils.aio import recorded_by_proxy_async | ||
|
||
|
||
class TestRedTeams(TestBase): | ||
|
||
# To run this test, use the following command in the \sdk\ai\azure-ai-projects folder: | ||
# cls & pytest tests\test_redteams.py::TestRedTeams::test_red_teams_async -s | ||
@servicePreparer() | ||
@recorded_by_proxy_async | ||
async def test_red_teams_async(self, **kwargs): | ||
|
||
endpoint = kwargs.pop("azure_ai_projects_tests_project_endpoint") | ||
print("\n=====> Endpoint:", endpoint) | ||
|
||
connection_name = self.test_redteams_params["connection_name"] | ||
model_deployment_name = self.test_redteams_params["model_deployment_name"] | ||
|
||
async with AIProjectClient( | ||
endpoint=endpoint, | ||
credential=self.get_credential(AIProjectClient, is_async=True), | ||
) as project_client: | ||
|
||
# [START red_team_sample] | ||
print("Creating a Red Team scan for direct model testing") | ||
|
||
# Create target configuration for testing an Azure OpenAI model | ||
target_config = AzureOpenAIModelConfiguration(model_deployment_name=f"{connection_name}/{model_deployment_name}") | ||
|
||
# Create the Red Team configuration | ||
red_team = RedTeam( | ||
attack_strategies=[AttackStrategy.BASE64], | ||
risk_categories=[RiskCategory.VIOLENCE], | ||
display_name="redteamtest1", # Use a simpler name | ||
target=target_config, | ||
) | ||
|
||
# Create and run the Red Team scan | ||
red_team_response = await project_client.red_teams.create(red_team=red_team) | ||
print(f"Red Team scan created with scan name: {red_team_response.name}") | ||
TestBase.validate_red_team_response(red_team_response, expected_attack_strategies=1, expected_risk_categories=1) | ||
|
||
print("Getting Red Team scan details") | ||
# Use the name returned by the create operation for the get call | ||
get_red_team_response = await project_client.red_teams.get(name=red_team_response.name) | ||
print(f"Red Team scan status: {get_red_team_response.status}") | ||
TestBase.validate_red_team_response(get_red_team_response, expected_attack_strategies=1, expected_risk_categories=1) | ||
|
||
print("Listing all Red Team scans") | ||
async for scan in project_client.red_teams.list(): | ||
print(f"Found scan: {scan.name}, Status: {scan.status}") | ||
TestBase.validate_red_team_response(scan) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.