Skip to content

Allowed Topics Detector

Open In Colab

This detector is designed to recognize if the prompt is talking about allowed topics. Allowed topics are set in the Policy section of the console.zenguard.ai

Ensure that your GenAI conversation stays within the topics of interest.

Tip

Check prerequisites before proceeding further.

Policies

topics: add allowed topics. The Allowed 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/allowed"

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("Allowed topics are detected.")
else:
    print("No allowed topics detected.")

assert not response.json()["is_detected"], "Error detecting allowed 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": true,
    "score": 1.0,
    "sanitized_message": null
}

  • is_detected(boolean): Indicates whether the prompt talks about allowed topics. In this example, it is True since in the Policy tab the "animals" were specified as the allowed topic.
  • score(float: 0.0 - 1.0): A score representing the certainty that the prompt discusses allowed topics. In this example , it is 1.0.
  • sanitized_message(string or null): For the allowed 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 allowed 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.ALLOWED_TOPICS], prompt=message)
if response.get("is_detected"):
    print("Allowed topics are detected.")
else:
    print("No allowed topics detected.")

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