promptguard.analyzers

class promptguard.SentimentAnalyzer[source]

Bases: object

Analyse sentiment and tone of prompts using VADER (with lexicon fallback).

When vaderSentiment is 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").

__init__()[source]

Initialise the analyser with VADER when available, or fall back.

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) — True when 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:

Dict[str, Any]

class promptguard.IntentClassifier[source]

Bases: object

Classify 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'})
__init__()[source]

Compile all regex patterns at initialisation time.

classify(text)[source]

Classify the intent of text.

Parameters:

text (str) – The prompt text to classify.

Returns:

A dict with keys intent, confidence, indicators, and description.

Return type:

Dict[str, Any]

class promptguard.KeywordExtractor[source]

Bases: object

Extract 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.

extract(text, top_n=5)[source]

Extract security-relevant keywords from text.

Parameters:
  • text (str) – The prompt text to analyse.

  • top_n (int) – Maximum number of keywords to return.

Returns:

A list of up to top_n keywords/phrases ranked by relevance score.

Return type:

List[str]

class promptguard.AttackPatternDetector[source]

Bases: object

Detect 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.

__init__()[source]

Compile all attack patterns at initialisation time.

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 categories

  • pattern_count (int) — number of distinct categories matched

  • details (dict) — per-category match details

  • highest_severity (Optional[str]) — "critical", "high", or "medium"

Return type:

Dict[str, Any]