Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

guardrails_ai_restrict_to_topic-0.1.0.tar.gz (11.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

guardrails_ai_restrict_to_topic-0.1.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file guardrails_ai_restrict_to_topic-0.1.0.tar.gz.

File metadata

File hashes

Hashes for guardrails_ai_restrict_to_topic-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d224ce95d6d9bbbbe628d6a56af251c1760bfff8640d9a15bd2421290d155580
MD5 1eb2048f679ca42a0a60a885452c91de
BLAKE2b-256 a58a83182831ee622183055fdf9fe0911a42d43e997f90cbe43ab73896f7e59d

See more details on using hashes here.

File details

Details for the file guardrails_ai_restrict_to_topic-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for guardrails_ai_restrict_to_topic-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f64ed9b7fcf452a27dd490fa613025dc9452a53ed6b828d7aa7ab5b981d6821b
MD5 d9bac56509a082f47781d8a12e7ee607
BLAKE2b-256 9ee8f101691d8a5efe16db941c4368652cc019000d2d06bfddc244fe625a32cf

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page