Install required libraries
Shell
pip install requests python-dotenv tabulate pandas enkryptai-sdk
Setup your development
Sign up for a account on our dashboard and create a API Key. Export the API Keys as environment variables:Shell
export OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
export ENKRYPTAI_API_KEY="YOUR_ENKRYPTAI_API_KEY"
Example Usage
Here is an example of how to use the Red Team API to run a red team test:import requests
import json
import os
import uuid
url = "https://api.enkryptai.com/redteam/v3/add-custom-task"
payload = json.dumps({
"test_name": f"redteam_test_{str(uuid.uuid4())[:8]}", # Required: Unique test identifier
"dataset_configuration": {
"system_description": "System description for your AI application",
"policy_description": "Do not generate any content that is illegal, harmful, or violates the rights of others.",
"max_prompts": 100,
"scenarios": 2,
"categories": 2,
"depth": 2,
},
"redteam_test_configurations": {
"bias_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
},
"cbrn_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
},
"harmful_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
},
"insecure_code_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
},
"toxicity_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
}
},
"endpoint_configuration": {
"testing_for": "foundationModels",
"model_name": "gpt-4o",
"model_config": {
"model_provider": "openai",
"endpoint": {
"scheme": "https",
"host": "api.openai.com",
"port": 443,
"base_path": "/v1/chat/completions"
},
"auth_data": {
"header_name": "Authorization",
"header_prefix": "Bearer",
"space_after_prefix": True
},
"apikeys": [os.getenv('OPENAI_API_KEY')],
"input_modalities": ["text"],
"output_modalities": ["text"]
}
}
})
headers = {
'Content-Type': 'application/json',
'apikey': os.getenv('ENKRYPTAI_API_KEY')
}
response = requests.request("POST", url, headers=headers, data=payload)
formatted_response = json.dumps(json.loads(response.text), indent=4)
print(formatted_response)
curl -X 'POST' \
'https://api.enkryptai.com/redteam/v3/add-custom-task' \
-H 'accept: application/json' \
-H 'Content-Type: application/json' \
-H "apikey: ENKRYPTAI_API_KEY" \
-d '{
"test_name": "redteam_test_001", // Required: Unique test identifier
"dataset_configuration": {
"system_description": "System description for your AI application",
"policy_description": "Do not generate any content that is illegal, harmful, or violates the rights of others.",
"max_prompts": 100,
"scenarios": 2,
"categories": 2,
"depth": 2
},
"redteam_test_configurations": {
"bias_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
},
"cbrn_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
},
"harmful_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
},
"insecure_code_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
},
"toxicity_test": {
"sample_percentage": 1,
"attack_methods": {
"basic": {"basic": {"params": {}}}
}
}
},
"endpoint_configuration": {
"testing_for": "foundationModels",
"model_name": "gpt-4o",
"model_config": {
"model_provider": "openai",
"endpoint": {
"scheme": "https",
"host": "api.openai.com",
"port": 443,
"base_path": "/v1/chat/completions"
},
"auth_data": {
"header_name": "Authorization",
"header_prefix": "Bearer",
"space_after_prefix": true
},
"apikeys": ["OPENAI_API_KEY"],
"input_modalities": ["text"],
"output_modalities": ["text"]
}
}
}'
import os
import uuid
from enkryptai_sdk import *
from dotenv import load_dotenv
load_dotenv()
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
ENKRYPT_API_KEY = os.getenv("ENKRYPTAI_API_KEY")
ENKRYPT_BASE_URL = os.getenv("ENKRYPTAI_BASE_URL") or "https://api.enkryptai.com"
redteam_client = RedTeamClient(api_key=ENKRYPT_API_KEY, base_url=ENKRYPT_BASE_URL)
redteam_test_name = f"Redteam Test {str(uuid.uuid4())[:6]}"
model_name = "gpt-4o-mini"
model_provider = "openai"
model_endpoint_url = "https://api.openai.com/v1/chat/completions"
sample_redteam_target_config = {
"test_name": redteam_test_name, # Required: Unique test identifier
"dataset_configuration": {
"system_description": "System description for your AI application",
"policy_description": "Do not generate any content that is illegal, harmful, or violates the rights of others.",
"max_prompts": 100,
"scenarios": 2,
"categories": 2,
"depth": 2,
},
"redteam_test_configurations": {
"bias_test": {
"sample_percentage": 2,
"attack_methods": {
"basic": {"basic": {"params": {}}}
},
},
"cbrn_test": {
"sample_percentage": 2,
"attack_methods": {
"basic": {"basic": {"params": {}}}
},
},
"insecure_code_test": {
"sample_percentage": 2,
"attack_methods": {
"basic": {"basic": {"params": {}}}
},
},
"toxicity_test": {
"sample_percentage": 2,
"attack_methods": {
"basic": {"basic": {"params": {}}}
},
},
"harmful_test": {
"sample_percentage": 2,
"attack_methods": {
"basic": {"basic": {"params": {}}}
},
},
},
"endpoint_configuration": {
"testing_for": "foundationModels",
"model_name": model_name,
"model_config": {
"model_provider": model_provider,
"endpoint_url": model_endpoint_url,
"apikey": OPENAI_API_KEY,
"input_modalities": ["text"],
"output_modalities": ["text"],
},
},
}
# Use a dictionary to configure a redteam task
add_redteam_target_response = redteam_client.add_custom_task_v3(config=copy.deepcopy(sample_redteam_target_config))
print(add_redteam_target_response)
assert add_redteam_target_response.message == "Task submitted successfully"
# Print as a dictionary
print(add_redteam_target_response.to_dict())
JSON
{
"status": "Success",
"task_id": "generate-and-redteam-6c3babbe-3bc4-4553-a0b7-454df720579f",
"message": "Task submitted successfully",
"data": {
...
}
}

