promptguard.analyzers¶
- class promptguard.SentimentAnalyzer[source]¶
Bases:
objectAnalyse sentiment and tone of prompts using VADER (with lexicon fallback).
When
vaderSentimentis installed it is used as the primary scorer. Otherwise a lightweight word-level lexicon is used. In both modes an additional aggressive-tone signal is computed from a curated vocabulary of security-relevant commands and coercive language, with basic negation awareness (e.g."don't ignore"scores lower than"ignore").- analyze(text)[source]¶
Analyse the sentiment and tone of text.
- Parameters:
text (str) – The prompt text to analyse.
- Returns:
A dict with the following keys –
sentiment(Sentiment) — overall sentiment class.polarity(float) — compound polarity score in[-1, 1].subjectivity(float) — degree of subjectivity in[0, 1].is_aggressive(bool) —Truewhen un-negated aggressive words are detected.positive_words(int) — count of positive lexicon matches.negative_words(int) — count of negative lexicon matches.aggressive_words(int) — net un-negated aggressive word count.
- Return type:
- class promptguard.IntentClassifier[source]¶
Bases:
objectClassify the intent of a prompt.
Patterns are pre-compiled at construction time for efficiency. Classification priority: JAILBREAK > INJECTION > QUESTION > INSTRUCTION > CONVERSATION.
- QUESTION_WORDS = frozenset({'are', 'can', 'could', 'do', 'does', 'how', 'is', 'should', 'what', 'when', 'where', 'which', 'who', 'why', 'would'})¶
- INSTRUCTION_WORDS = frozenset({'build', 'calculate', 'create', 'describe', 'design', 'explain', 'find', 'generate', 'give', 'help', 'list', 'make', 'provide', 'show', 'tell', 'translate', 'write'})¶
- class promptguard.KeywordExtractor[source]¶
Bases:
objectExtract security-relevant keywords from prompts.
Uses spaCy for advanced noun-chunk and lemma extraction when available, falling back to a regex-based word scan otherwise.
- class promptguard.AttackPatternDetector[source]¶
Bases:
objectDetect specific categories of attack patterns in prompts.
Patterns are pre-compiled at construction time. Input text is Unicode-normalised before matching to catch full-width and compatibility character obfuscation.
- detect(text)[source]¶
Detect attack patterns in text.
Input is Unicode-normalised before matching to catch full-width and compatibility character variants.
- Parameters:
text (str) – The prompt text to inspect.
- Returns:
A dict with keys –
has_attack_patterns(bool)attack_types(List[str]) — names of detected categoriespattern_count(int) — number of distinct categories matcheddetails(dict) — per-category match detailshighest_severity(Optional[str]) —"critical","high", or"medium"
- Return type: