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) Copy
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)
Evaluate content against this guardrail
Evaluation context
Result of the evaluation
Readonly
Unique name of the guardrail
Human-readable description
Whether this guardrail is enabled
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.
Example