Overview
BanCompetitors is an output scanner that detects and optionally redacts competitor names from LLM responses by delegating to the input-side InputBanCompetitors scanner.
Description
The BanCompetitors output scanner is a thin wrapper around the corresponding input scanner InputBanCompetitors. It takes a list of competitor names and scans LLM outputs for mentions of those competitors. When a competitor name is found, the scanner can either flag the output as invalid or redact the competitor name from the text, depending on the redact parameter. The scanner leverages a Named Entity Recognition (NER) model to identify organization names in the text and matches them against the provided competitor list. The threshold parameter controls the confidence level required for entity recognition.
Usage
Use this scanner when your application must not mention competitor products or companies in its responses. This is common in enterprise chatbots, marketing tools, and customer-facing applications where brand guidelines prohibit referencing competitors. The redaction feature allows you to sanitize outputs rather than blocking them entirely.
Code Reference
Source Location
Signature
class BanCompetitors(Scanner):
def __init__(
self,
competitors: list[str],
*,
threshold: float = 0.5,
redact: bool = True,
model: Model | None = None,
use_onnx: bool = False,
) -> None: ...
def scan(self, prompt: str, output: str) -> tuple[str, bool, float]: ...
Import
from llm_guard.output_scanners import BanCompetitors
I/O Contract
Inputs
| Name |
Type |
Required |
Description
|
| prompt |
str |
Yes |
The input prompt
|
| output |
str |
Yes |
The LLM output to scan for competitor mentions
|
Constructor Parameters
| Name |
Type |
Required |
Default |
Description
|
| competitors |
list[str] |
Yes |
N/A |
List of competitor names to detect
|
| threshold |
float |
No |
0.5 |
Confidence threshold for NER entity recognition
|
| redact |
bool |
No |
True |
Whether to redact competitor names from output
|
| model |
None |
No |
None |
Custom NER model to use
|
| use_onnx |
bool |
No |
False |
Whether to use ONNX runtime for inference
|
Outputs
| Name |
Type |
Description
|
| sanitized_output |
str |
The output with competitor names optionally redacted
|
| is_valid |
bool |
Whether the output passed the scan (True if no competitors found)
|
| risk_score |
float |
Risk score (-1.0 to 1.0)
|
Usage Examples
Basic Usage
from llm_guard.output_scanners import BanCompetitors
scanner = BanCompetitors(
competitors=["CompetitorA", "CompetitorB", "RivalCorp"],
threshold=0.5,
redact=True,
)
prompt = "What is the best product in the market?"
output = "While CompetitorA offers a similar product, our solution provides better performance."
sanitized_output, is_valid, risk_score = scanner.scan(prompt, output)
print(sanitized_output) # Competitor name will be redacted
print(f"Valid: {is_valid}, Risk: {risk_score}")
Related Pages
Page Connections
Double-click a node to navigate. Hold to expand connections.