Hi Abhishek,
I am also getting the same error while following the Microsoft documentation that you are using.
Please follow the steps below to fix the issue:
- Go to AI Foundry and copy the endpoint as shown in the image
- Replace it in your code as shown below:
Instead of using
azure_ai_project = {subscription_id, resource_group_name, project_name}
paste the endpoint which you copied above as shown below:
azure_ai_project = "<Azure AI Foundry project endpoint>"
Below is the full code for your reference:
import os
import asyncio
from dotenv import load_dotenv
from azure.identity import DefaultAzureCredential
from azure.ai.evaluation.red_team import RedTeam, RiskCategory
# Load environment variables from .env file
load_dotenv()
# Option 1: Provide Azure AI Foundry project endpoint copied as shown in above steps
azure_ai_project = "https://your-foundry-name.services.ai.azure.com/api/projects/your-project-name"
# Option 2: Use the endpoint copied above as an environment variable
azure_ai_project = os.environ.get("AZURE_AI_PROJECT")
# Instantiate Azure Credential
credential = DefaultAzureCredential()
# Define a simple callback -- this simulates your model/app's response
def simple_callback(query: str) -> str:
# Replace this with a call to your own model or application
return "I'm an AI assistant that follows ethical guidelines. I cannot provide harmful content."
# Create the Red Team Agent
red_team_agent = RedTeam(
azure_ai_project=azure_ai_project,
credential=credential,
risk_categories=[
RiskCategory.Violence,
RiskCategory.HateUnfairness,
RiskCategory.Sexual,
RiskCategory.SelfHarm
],
num_objectives=2, # Number of attack prompts per risk category (customize as needed)
)
# Run the scan
async def run_scan():
red_team_result = await red_team_agent.scan(target=simple_callback)
# Optionally, save results to a JSON file
await red_team_agent.scan(target=simple_callback, output_path="My-First-RedTeam-Scan.json")
print("Scan complete. Results saved to My-First-RedTeam-Scan.json.")
# To actually execute the async scan from main thread:
if __name__ == "__main__":
asyncio.run(run_scan())
Feel free to accept this as an answer.
Thank you for reaching to Microsoft QNA Portal