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.1a1.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.

File details

Details for the file guardrails_ai_restrict_to_topic-0.1.1a1.tar.gz.

File metadata

File hashes

Hashes for guardrails_ai_restrict_to_topic-0.1.1a1.tar.gz
Algorithm Hash digest
SHA256 67a08bce8e00bd90fca5be81aa061277b3e07e5af3e513a162d0a687ba6e3998
MD5 2171165012b9c7d5caf89a3ea2a366b9
BLAKE2b-256 05f081b49c32daf8aed023da7a77de979be064841900528d5c2305fc42caf336

See more details on using hashes here.

Provenance

The following attestation bundles were made for guardrails_ai_restrict_to_topic-0.1.1a1.tar.gz:

Publisher: validators_publish.yml on guardrails-ai/guardrails-hub-monorepo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file guardrails_ai_restrict_to_topic-0.1.1a1-py3-none-any.whl.

File metadata

File hashes

Hashes for guardrails_ai_restrict_to_topic-0.1.1a1-py3-none-any.whl
Algorithm Hash digest
SHA256 8a882a03ec88caf23428a977fa2ee2c34e272ede548aadbef97974f4202590b5
MD5 0a884e89a2694109af80ade3e0c77bcd
BLAKE2b-256 f777ad63529719f6e6cb3959b156807138510877a55e759ab9883815bd49881a

See more details on using hashes here.

Provenance

The following attestation bundles were made for guardrails_ai_restrict_to_topic-0.1.1a1-py3-none-any.whl:

Publisher: validators_publish.yml on guardrails-ai/guardrails-hub-monorepo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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