pub struct PerplexityAnalyzer { /* private fields */ }Expand description
Perplexity analyzer for detecting adversarial patterns
Uses character-level n-gram models and entropy calculations to detect unusual text patterns that may indicate adversarial suffixes or manipulated content.
Implementations§
Source§impl PerplexityAnalyzer
impl PerplexityAnalyzer
Sourcepub fn with_config(config: PerplexityConfig) -> Self
pub fn with_config(config: PerplexityConfig) -> Self
Create a new analyzer with custom configuration
Sourcepub fn char_perplexity(&self, text: &str) -> f32
pub fn char_perplexity(&self, text: &str) -> f32
Calculate character-level perplexity for text
Perplexity measures how “surprising” the text is according to the character n-gram model. Higher values indicate unusual text.
Sourcepub fn token_entropy(&self, text: &str) -> f32
pub fn token_entropy(&self, text: &str) -> f32
Calculate token/character entropy
Low entropy indicates repetitive patterns (e.g., “aaaaaaa”). Normal text has moderate entropy.
Sourcepub fn unique_char_ratio(&self, text: &str) -> f32
pub fn unique_char_ratio(&self, text: &str) -> f32
Calculate unique character ratio
Very low ratio indicates repetitive text. Very high ratio with long text may indicate random characters.
Sourcepub fn find_anomalous_segments(
&self,
text: &str,
max_perplexity: f32,
min_perplexity: f32,
min_entropy: f32,
) -> Vec<AnomalySegment>
pub fn find_anomalous_segments( &self, text: &str, max_perplexity: f32, min_perplexity: f32, min_entropy: f32, ) -> Vec<AnomalySegment>
Detect anomalous segments in text using sliding window analysis
Returns segments that have unusual perplexity or entropy scores.
Sourcepub fn is_suspicious(
&self,
text: &str,
max_perplexity: f32,
min_entropy: f32,
) -> bool
pub fn is_suspicious( &self, text: &str, max_perplexity: f32, min_entropy: f32, ) -> bool
Quick check for adversarial patterns
Returns true if the text shows signs of adversarial manipulation.
Sourcepub fn analyze_suffix(
&self,
text: &str,
suffix_ratio: f32,
max_perplexity: f32,
min_entropy: f32,
) -> Option<AnomalySegment>
pub fn analyze_suffix( &self, text: &str, suffix_ratio: f32, max_perplexity: f32, min_entropy: f32, ) -> Option<AnomalySegment>
Analyze the suffix portion of text
GCG and AutoDAN attacks typically add adversarial suffixes. This method focuses on the last portion of the text.
§Arguments
text- The text to analyzesuffix_ratio- Portion of text to analyze as suffix (0.1 - 0.5)max_perplexity- Maximum allowed perplexitymin_entropy- Minimum allowed entropy