Analyzes text to determine if it aligns with the given specific topic or field of interest. You can provide the topic keyword as a string or a list of strings. The detector returns a binary value (0 for off-topic, 1 for on-topic) as summary value indicating whether the text aligns with the given topic. The output will contain the most likely topic among those provided and returns details for only that topic.

import requests
import json
import os

url = "https://api.enkryptai.com/guardrails/detect"

payload = json.dumps({
    "text": "Aluminum alloys can have very high strength-to-weight ratios, making them useful for applications where weight is a critical factor, such as in the aerospace industry",
    "detectors": {
        "topic_detector": {
            "enabled": True,
            "topic": "science"
        }
    }
})

headers = {
    'Content-Type': 'application/json',
    'api_key': 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)

Example response:

{
  "summary": {
    "on_topic": 1
  },
  "details": {
    "topic_detector": {
      "science": 0.9998229146003723
    }
  }
}