Guardrail that filters content based on allowed/forbidden topics.

Uses keyword matching to classify content into topics and validates against allowed or forbidden topic lists.

const topicGuardrail = new TopicFilterGuardrail({
allowedTopics: ['support', 'sales', 'billing'],
forbiddenTopics: ['politics', 'religion'],
topicKeywords: {
politics: ['election', 'vote', 'government', 'political'],
support: ['help', 'issue', 'problem', 'fix'],
},
severity: GuardrailSeverity.ERROR,
});

const result = await topicGuardrail.evaluate({
content: 'What are your thoughts on the recent election?',
contentType: 'input',
});

console.log(result.passed); // false (politics is forbidden)

Implements

  • Guardrail

Constructors

Methods

Properties

Constructors

Methods

  • Evaluate content against this guardrail

    Parameters

    • context: GuardrailContext

      Evaluation context

    Returns Promise<GuardrailResult>

    Result of the evaluation

Properties

name: "topic-filter" = 'topic-filter'

Unique name of the guardrail

description: "Filters content based on allowed or forbidden topics" = 'Filters content based on allowed or forbidden topics'

Human-readable description

enabled: boolean

Whether this guardrail is enabled