Checks that a text stays on a set of valid topics and avoids invalid ones.
Project description
guardrails-ai-restrict-to-topic
Checks that a text's topic is within a set of valid topics and, optionally, that
it avoids a set of invalid topics. By default it runs a local zero-shot
classifier (facebook/bart-large-mnli) and falls back to an LLM when the
classifier is not confident.
Registered as tryolabs/restricttotopic.
Installation
pip install guardrails-ai-restrict-to-topic
Usage
from guardrails import Guard
from guardrails_ai.restricttotopic import RestrictToTopic
guard = Guard().use(
RestrictToTopic(
valid_topics=["sports", "politics"],
invalid_topics=["music"],
disable_llm=True, # zero-shot classifier only (no OpenAI key needed)
on_fail="exception",
)
)
guard.validate("The Chiefs won the Super Bowl.") # passes
The default ensemble mode also uses an LLM fallback and requires OPENAI_API_KEY.
License
Apache-2.0. Originally authored by Tryolabs.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file guardrails_ai_restrict_to_topic-0.1.0.tar.gz.
File metadata
- Download URL: guardrails_ai_restrict_to_topic-0.1.0.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d224ce95d6d9bbbbe628d6a56af251c1760bfff8640d9a15bd2421290d155580
|
|
| MD5 |
1eb2048f679ca42a0a60a885452c91de
|
|
| BLAKE2b-256 |
a58a83182831ee622183055fdf9fe0911a42d43e997f90cbe43ab73896f7e59d
|
File details
Details for the file guardrails_ai_restrict_to_topic-0.1.0-py3-none-any.whl.
File metadata
- Download URL: guardrails_ai_restrict_to_topic-0.1.0-py3-none-any.whl
- Upload date:
- Size: 11.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f64ed9b7fcf452a27dd490fa613025dc9452a53ed6b828d7aa7ab5b981d6821b
|
|
| MD5 |
d9bac56509a082f47781d8a12e7ee607
|
|
| BLAKE2b-256 |
9ee8f101691d8a5efe16db941c4368652cc019000d2d06bfddc244fe625a32cf
|