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:

get_cached_results_for_technique

get_cached_results_for_technique(memory_interface: MemoryInterface, technique_eval_hash: str, objective_target_eval_hash: str, additional_filters: Optional[Sequence[IdentifierFilter]] = None) → list[AttackResult]

Return cached AttackResults matching a (technique × objective target) pair.

Memory is queried for AttackResults whose stamped atomic_attack_identifier.eval_hash equals technique_eval_hash, then results are filtered in Python to those whose nested objective target produces the requested objective_target_eval_hash (computed via ObjectiveTargetEvaluationIdentifier). Returned results are sorted newest-first by timestamp so the most recent is at index 0.

No scenario scoping is applied; this is a behavioral cache spanning every run that produced the same (technique × target) combination. Callers that need scenario-level scoping should pass additional IdentifierFilters or filter the returned list themselves.

ParameterTypeDescription
memory_interfaceMemoryInterfaceThe memory interface to query. Analytics is stateless, so callers (e.g. scenarios) must pass their own CentralMemory.get_memory_instance().
technique_eval_hashstrBehavioral eval hash of the atomic-attack technique, as produced by AtomicAttackEvaluationIdentifier.eval_hash (also exposed as AtomicAttack.technique_eval_hash).
objective_target_eval_hashstrBehavioral eval hash of the objective target, as produced by ObjectiveTargetEvaluationIdentifier.eval_hash.
additional_filtersOptional[Sequence[IdentifierFilter]]Extra IdentifierFilter predicates appended to the SQL pre-filter. Defaults to None. Defaults to None.

Returns:

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: