Skip to content

Commit 19bd600

Browse files
luigiwHanchi WangNagkumar Arkalgud
authored
Update Redteaming agent to go through FDP (#41146)
* Update Redteaming agent to go through FDP * fix import * update the sample * fix the docstring --------- Co-authored-by: Hanchi Wang <hanchiwang@mac.lan> Co-authored-by: Nagkumar Arkalgud <nagkumar@naarkalg-work-mac.local>
1 parent 03c75bc commit 19bd600

File tree

4 files changed

+18
-68
lines changed

4 files changed

+18
-68
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# ---------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# ---------------------------------------------------------

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/red_team/_agent/_agent_tools.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ class RedTeamToolProvider:
3030
3131
This class provides tools that can be registered with Azure AI Agents
3232
to enable red teaming capabilities.
33-
34-
:param azure_ai_project: The Azure AI project configuration for accessing red team services
35-
:type azure_ai_project: Dict[str, Any]
33+
34+
:param azure_ai_project_endpoint: The Azure AI project endpoint (e.g., 'https://your-resource-name.services.ai.azure.com/api/projects/your-project-name')
35+
:type azure_ai_project_endpoint: str
3636
:param credential: The credential to authenticate with Azure services
3737
:type credential: TokenCredential
3838
:param application_scenario: Optional application scenario context for generating relevant prompts
@@ -41,12 +41,12 @@ class RedTeamToolProvider:
4141

4242
def __init__(
4343
self,
44-
azure_ai_project: Dict[str, Any],
44+
azure_ai_project_endpoint: str,
4545
credential: TokenCredential,
4646
*,
4747
application_scenario: Optional[str] = None,
4848
):
49-
self.azure_ai_project = azure_ai_project
49+
self.azure_ai_project_endpoint = azure_ai_project_endpoint
5050
self.credential = credential
5151
self.application_scenario = application_scenario
5252

@@ -59,7 +59,7 @@ def __init__(
5959

6060
# Create the generated RAI client for fetching attack objectives
6161
self.generated_rai_client = GeneratedRAIClient(
62-
azure_ai_project=self.azure_ai_project,
62+
azure_ai_project=self.azure_ai_project_endpoint,
6363
token_manager=self.token_manager.get_aad_credential()
6464
)
6565

sdk/evaluation/azure-ai-evaluation/azure/ai/evaluation/red_team/_agent/_semantic_kernel_plugin.py

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from semantic_kernel.functions import kernel_function
1515

16-
from azure.ai.evaluation.red_team._agent import RedTeamToolProvider
16+
from azure.ai.evaluation.red_team._agent._agent_tools import RedTeamToolProvider
1717
from azure.identity import DefaultAzureCredential
1818

1919
class RedTeamPlugin:
@@ -26,16 +26,7 @@ class RedTeamPlugin:
2626
```python
2727
# Method 1: Create a plugin with individual environment variables
2828
plugin = RedTeamPlugin(
29-
endpoint=os.environ.get("AZURE_AI_ENDPOINT"),
30-
subscription_id=os.environ.get("AZURE_SUBSCRIPTION_ID"),
31-
resource_group=os.environ.get("AZURE_RESOURCE_GROUP"),
32-
project_name=os.environ.get("AZURE_PROJECT_NAME"),
33-
target_func=lambda x: "Target model response"
34-
)
35-
36-
# Method 2: Create a plugin with the Azure AI Project connection string
37-
plugin = RedTeamPlugin.from_connection_string(
38-
projects_connection_string=os.environ["PROJECT_CONNECTION_STRING"],
29+
azure_ai_project_endpoint=os.environ.get("AZURE_AI_PROJECT_ENDPOINT"),
3930
target_func=lambda x: "Target model response"
4031
)
4132
@@ -49,30 +40,20 @@ class RedTeamPlugin:
4940
```
5041
"""
5142

52-
def __init__(self, subscription_id: str, resource_group: str,
53-
project_name: str, target_func: Optional[Callable[[str], str]] = None, *,
43+
def __init__(self, azure_ai_project_endpoint: str, target_func: Optional[Callable[[str], str]] = None, *,
5444
application_scenario: str = "", **kwargs):
5545
"""
5646
Initialize the RedTeamPlugin with the necessary configuration components.
57-
58-
:param endpoint: The Azure AI endpoint (e.g., 'swedencentral.api.azureml.ms')
59-
:param subscription_id: The Azure subscription ID
60-
:param resource_group: The Azure resource group name
61-
:param project_name: The Azure AI project name
47+
48+
:param azure_ai_project_endpoint: The Azure AI project endpoint (e.g., 'https://your-resource-name.services.ai.azure.com/api/projects/your-project-name')
6249
:param target_func: Optional function to call with prompts
6350
:param application_scenario: The application scenario for the tool provider
6451
"""
65-
# Set up project details
66-
azure_ai_project = {
67-
"subscription_id": subscription_id,
68-
"resource_group_name": resource_group,
69-
"project_name": project_name
70-
}
7152

7253
# Initialize credential and tool provider
7354
self.credential = DefaultAzureCredential()
7455
self.tool_provider = RedTeamToolProvider(
75-
azure_ai_project=azure_ai_project,
56+
azure_ai_project_endpoint=azure_ai_project_endpoint,
7657
credential=self.credential,
7758
application_scenario=application_scenario
7859
)
@@ -83,36 +64,6 @@ def __init__(self, subscription_id: str, resource_group: str,
8364
# Dictionary to store fetched prompts for reference
8465
self.fetched_prompts = {}
8566

86-
@classmethod
87-
def from_connection_string(cls, projects_connection_string: str,
88-
target_func: Optional[Callable[[str], str]] = None,
89-
application_scenario: str = "A customer service chatbot for a retail website"):
90-
"""
91-
Create a RedTeamPlugin instance from a connection string.
92-
93-
:param projects_connection_string: The Azure AI project connection string
94-
:param target_func: Optional function to call with prompts
95-
:param application_scenario: The application scenario for the tool provider
96-
:return: A new RedTeamPlugin instance
97-
"""
98-
# Parse connection string
99-
parts = projects_connection_string.split(";")
100-
if len(parts) < 4:
101-
raise ValueError("Invalid connection string format. Expected format: 'endpoint;subscription_id;resource_group;project_name'")
102-
103-
endpoint = parts[0] # type: ignore
104-
subscription_id = parts[1]
105-
resource_group = parts[2]
106-
project_name = parts[3]
107-
108-
return cls(
109-
subscription_id=subscription_id,
110-
resource_group=resource_group,
111-
project_name=project_name,
112-
target_func=target_func,
113-
application_scenario=application_scenario
114-
)
115-
11667
@kernel_function(description="Fetch a harmful prompt for a specific risk category to test content filters")
11768
async def fetch_harmful_prompt(
11869
self,

sdk/evaluation/azure-ai-evaluation/samples/semantic_kernel_red_team_agent_sample.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def call_ollama(query: str) -> str:
2727
Call the Ollama API with a prompt and return the response.
2828
"""
2929
url = "http://localhost:11434/api/generate"
30-
payload = {"model": "<model_name>", "prompt": query, "stream": False}
30+
payload = {"model": "gemma3:1b", "prompt": query, "stream": False}
3131

3232
response = requests.post(url, json=payload, timeout=60)
3333
try:
@@ -52,9 +52,7 @@ async def main():
5252
api_key = os.environ.get("AZURE_OPENAI_API_KEY")
5353

5454
# Get Azure AI Project details from environment variables
55-
subscription_id = os.environ.get("AZURE_SUBSCRIPTION_ID")
56-
resource_group = os.environ.get("AZURE_RESOURCE_GROUP")
57-
project_name = os.environ.get("AZURE_PROJECT_NAME")
55+
azure_ai_project_endpoint = os.environ.get("AZURE_AI_PROJECT_ENDPOINT")
5856

5957
# Initialize the service
6058
service = AzureChatCompletion(
@@ -65,9 +63,7 @@ async def main():
6563

6664
# Initialize the RedTeamPlugin with the target function
6765
red_team_plugin = RedTeamPlugin(
68-
subscription_id=subscription_id,
69-
resource_group=resource_group,
70-
project_name=project_name,
66+
azure_ai_project_endpoint=azure_ai_project_endpoint,
7167
target_func=call_ollama
7268
)
7369

0 commit comments

Comments
 (0)