Skip to main content

PerplexityAnalyzer

Struct PerplexityAnalyzer 

Source
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

Source

pub fn new() -> Self

Create a new analyzer with default English n-gram model

Source

pub fn with_config(config: PerplexityConfig) -> Self

Create a new analyzer with custom configuration

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 analyze
  • suffix_ratio - Portion of text to analyze as suffix (0.1 - 0.5)
  • max_perplexity - Maximum allowed perplexity
  • min_entropy - Minimum allowed entropy

Trait Implementations§

Source§

impl Default for PerplexityAnalyzer

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more