Skip to main 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

Generate API key from the ZenGuard Console.

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

prompt = "Lets talk about pandas and koalas."
session = requests.Session()

response = session.post(
"https://api.zenguard.ai/v1/detect/topics/allowed",
json={"messages": [prompt]},
headers={"x-api-key": os.getenv("ZEN_API_KEY")}
)

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"

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"

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

prompt = "Lets talk about pandas and koalas."
session = requests.Session()

response = session.post(
"https://api.zenguard.ai/v1/detect/topics/banned",
json={"messages": [prompt]},
headers={"x-api-key": os.getenv("ZEN_API_KEY")}
)

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"

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"