Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

pyrit.analytics

Analytics module for PyRIT conversation and result analysis.

Functions

analyze_results

analyze_results(attack_results: list[AttackResult]) → dict[str, AttackStats | dict[str, AttackStats]]

Analyze a list of AttackResult objects and return overall and grouped statistics.

Returns:

Raises:

ApproximateTextMatching

Bases: TextMatching

Approximate text matching using n-gram overlap.

This strategy computes the proportion of character n-grams from the target that are present in the text. Useful for detecting partial matches, encoded content, or text with variations.

Constructor Parameters:

ParameterTypeDescription
thresholdfloatThe minimum n-gram overlap score (0.0 to 1.0) required for a match. Defaults to 0.5 (50% overlap). Defaults to 0.5.
nintThe length of character n-grams to use. Defaults to 3. Defaults to 3.
case_sensitiveboolWhether to perform case-sensitive matching. Defaults to False. Defaults to False.

Methods:

get_overlap_score

get_overlap_score(target: str, text: str) → float

Get the n-gram overlap score without threshold comparison.

Useful for getting detailed scoring information.

ParameterTypeDescription
targetstrThe target string to match.
textstrThe text to search in.

Returns:

is_match

is_match(target: str, text: str) → bool

Check if target approximately matches text using n-gram overlap.

ParameterTypeDescription
targetstrThe string to search for.
textstrThe text to search in.

Returns:

AttackStats

Statistics for attack analysis results.

ConversationAnalytics

Handles analytics operations on conversation data, such as finding similar chat messages based on conversation history or embedding similarity.

Constructor Parameters:

ParameterTypeDescription
memory_interfaceMemoryInterfaceAn instance of MemoryInterface for accessing conversation data.

Methods:

get_prompt_entries_with_same_converted_content

get_prompt_entries_with_same_converted_content(chat_message_content: str) → list[ConversationMessageWithSimilarity]

Retrieve chat messages that have the same converted content.

ParameterTypeDescription
chat_message_contentstrThe content of the chat message to find similar messages for.

Returns:

get_similar_chat_messages_by_embedding

get_similar_chat_messages_by_embedding(chat_message_embedding: list[float], threshold: float = 0.8) → list[EmbeddingMessageWithSimilarity]

Retrieve chat messages that are similar to the given embedding based on cosine similarity.

ParameterTypeDescription
chat_message_embeddingList[float]The embedding of the chat message to find similar messages for.
thresholdfloatThe similarity threshold for considering messages as similar. Defaults to 0.8. Defaults to 0.8.

Returns:

ExactTextMatching

Bases: TextMatching

Exact substring matching strategy.

Checks if the target string is present in the text as a substring.

Constructor Parameters:

ParameterTypeDescription
case_sensitiveboolWhether to perform case-sensitive matching. Defaults to False. Defaults to False.
ignore_whitespaceboolWhether to ignore whitespace. Defaults to True. Defaults to True.

Methods:

is_match

is_match(target: str, text: str) → bool

Check if target string is present in text.

ParameterTypeDescription
targetstrThe substring to search for.
textstrThe text to search in.

Returns:

TextMatching

Bases: Protocol

Protocol for text matching strategies.

Classes implementing this protocol must provide an is_match method that checks if a target string matches text according to some strategy.

Methods:

is_match

is_match(target: str, text: str) → bool

Check if target matches text according to the strategy.

ParameterTypeDescription
targetstrThe string to search for.
textstrThe text to search in.

Returns: