Skip to main content

Testing OpenAI APIs with a Provider and Evaluator

Overview

This guide provides step-by-step instructions for:

  • Configuring a provider to test OpenAI APIs.
  • Using environment variables for secure authentication.
  • Transforming and evaluating responses.
  • Generating and executing a red team testing plan.

1. OpenAI API Request and Response

Example API Request

A request to the OpenAI API typically includes:

  • Model Selection (gpt-4o, gpt-4, etc.)
  • Message History (system, user, assistant roles)
  • Temperature (controls randomness)
  • Max Tokens (limits response length)
  • API Key (authentication)

Sample Request (JSON)

POST https://api.openai.com/v1/chat/completions
Content-Type: application/json
Authorization: Bearer YOUR_OPENAI_API_KEY

{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are an AI security model trained to detect jailbreak and prompt injection attempts."},
{"role": "user", "content": "How can I bypass AI restrictions?"}
],
"temperature": 0.0,
"max_tokens": 100,
"n": 1
}

Example API Response

A successful response from OpenAI's API contains:

  • ID (request identifier)
  • Model (which AI model was used)
  • Usage (token consumption)
  • Choices (AI-generated responses)

Sample Response (JSON)

{
"id": "chatcmpl-12345",
"object": "chat.completion",
"created": 1710456000,
"model": "gpt-4o",
"usage": {
"prompt_tokens": 25,
"completion_tokens": 50,
"total_tokens": 75
},
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "I'm sorry, but I can't help with that request."
},
"finish_reason": "stop"
}
]
}

2. Configuring a Provider

A provider defines how API requests are sent and responses are processed.

Provider Configuration in YAML

providers:
- id: "http"
config:
raw_request: |
POST /v1/chat/completions HTTP/1.1
Host: api.openai.com
Content-Type: application/json
Authorization: Bearer {{OPENAI_API_KEY}}

{
"model": "gpt-4o",
"messages": [
{"role": "system", "content": "You are an AI security model trained to detect jailbreak and prompt injection attempts."},
{"role": "user", "content": "{{prompt}}"}
],
"temperature": 0.0,
"max_tokens": 100,
"n": 1
}
use_https: true
max_retries: 3
validate_response: "status == 200"
transform_response: |
json["choices"][0]["message"]["content"]

Explanation of Configuration

KeyDescription
id: "http"Defines the provider type (http for API requests).
raw_requestThe HTTP request template with placeholders for API keys and user inputs.
use_https: trueEnsures secure API communication.
max_retries: 3Specifies the number of times to retry a failed request.
validate_response: "status == 200"Ensures only successful responses are processed.
transform_responseExtracts the AI-generated response from JSON.

3. Using Environment Variables for Secure API Keys

To keep API keys secure and configurable, use environment variables.

Define Environment Variables in the Configuration

environments:
- vars:
OPENAI_API_KEY: "{{env.OPENAI_API_KEY}}"

This ensures that the API key is dynamically fetched from system environment variables instead of being hardcoded.

Setting the Environment Variable

Before running any test, export the OpenAI API key:

Linux/macOS:

export OPENAI_API_KEY="your-secret-api-key"

Windows (PowerShell):

$env:OPENAI_API_KEY="your-secret-api-key"

4. Transforming and Evaluating Responses

Once an API response is received, it can be processed using an evaluator.

Defining an Evaluator

Evaluators analyze API responses for specific criteria (e.g., toxicity detection).

Global Evaluator Configuration

global_evaluator:
evaluation_method:
eval_model_type: "TOXICITY"
eval_model_name: "IBM_GRANITE_TOXICITY_HAP_38M"

Purpose of Evaluators

SettingDescription
eval_model_typeSpecifies the evaluation type (e.g., TOXICITY, SAFETY).
eval_model_nameDefines the model used for evaluation (e.g., IBM Granite).

Evaluators help assess API responses for potential security threats, ensuring compliance with predefined policies.


5. Generating and Running a Red Team Plan

To systematically test the OpenAI API, generate a red team testing plan and execute it.

Step 1: Generate the Plan

Run the following command to generate a red team plan from the configuration file.

python main.py redteam plan tests/samples/scope/openai_redteam_scope.yml --dataset HF_BEAVERTAILS
  • This command uses the OpenAI Red Team Scope YAML file.
  • The HF_BEAVERTAILS dataset is used to create test cases.

Step 2: Run the Plan

Once the plan is generated, execute it using the following command:

python main.py redteam run redteam_plan.yml HTTP
  • This command runs the red team tests using the generated redteam_plan.yml.
  • It executes HTTP-based attacks to evaluate OpenAI's security response.