Skip to content

Banned Topics Detector

Open In Colab

This detector is designed to restrict the prompt from talking about banned topics. Banned topics are set in the Policy section of the console.zenguard.ai

Ensure that your GenAI conversations stay within the boundaries and do not veer off into controversial territory.

Tip

Check prerequisites before proceeding further.

Policies

topics: add banned topics. The Banned Topics Detector checks if the prompt contains any of the specified topics.

API

Usage

import os
import requests

endpoint = "https://api.zenguard.ai/v1/detect/topics/banned"

headers = {
    "x-api-key": os.getenv("ZEN_API_KEY"),
    "Content-Type": "application/json",
}

data = {
    "messages": ["Lets talk about pandas and koalas."]
}

response = requests.post(endpoint, json=data, headers=headers)
if response.json()["is_detected"]:
    print("Banned topics detected. We should not talk about it.")
else:
    print("No banned topics detected. Carry on.")

assert not response.json()["is_detected"], "Error detecting banned topics"
curl -X POST https://api.zenguard.ai/v1/detect/prompt_injection \
    -H "x-api-key: $ZEN_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
        "messages": ["Lets talk about pandas and koalas."]
    }'

Response Example:

{
    "is_detected": false,
    "score": 0.0,
    "sanitized_message": null
}

  • is_detected(boolean): Indicates whether the prompt contains banned topics. In this example, it is False since no banned topics were detected.
  • score(float: 0.0 - 1.0): A score representing the certainty that the prompt contains banned topics. In this example , it is 0.0.
  • sanitized_message(string or null): For the banned topics detector this field is null.

Error Codes:

- `401 Unauthorized`: API key is missing or invalid.
- `400 Bad Request`: Request body is malformed.
- `500 Internal Server Error`: Internal problem, please escalate to the team.

Client

Detect banned topics:

import os
from zenguard import Credentials, Detector, ZenGuard, ZenGuardConfig

api_key = os.environ.get("ZEN_API_KEY")
config = ZenGuardConfig(credentials=Credentials(api_key=api_key))
zenguard = ZenGuard(config=config)

message="Lets talk about pandas and koalas."
response = zenguard.detect(detectors=[Detector.BANNED_TOPICS], prompt=message)
if response.get("is_detected"):
    print("Banned topics detected. We should not talk about it.")
else:
    print("No banned topics detected. Carry on.")

assert not response.get("is_detected"), "Error detecting banned topics"