-
Notifications
You must be signed in to change notification settings - Fork 447
probes: refactor fact snippet mixin #1187
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
leondz
wants to merge
11
commits into
NVIDIA:main
Choose a base branch
from
leondz:update/refactor_FactSnippetMixin
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7a8d557
add more whois contexts, sep pairs, inj instructions for LatentWhois
leondz 3b0544d
add whois payload injection marker check
leondz b80749a
Merge branch 'main' into update/latentinjection_refactor
leondz 13583bb
remove permutation explosion in LatentWhoisSnippet & LatentWhoisSnipp…
leondz 0bf4e24
stop dupe injection contexts in snippet assembly
leondz 892ef0b
defensive check for context cap
leondz 48a9622
refactor fact snippet logic up
leondz 66add6e
Merge branch 'main' into update/refactor_FactSnippetMixin
leondz cead34c
merge main
leondz 084cff6
merge main
leondz 8520ec1
update to match probes.Probe
leondz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -4,7 +4,6 @@ | |||||
"""Latent injection: probes to test prompt injections buried in other contexts.""" | ||||||
|
||||||
import logging | ||||||
from math import ceil, sqrt | ||||||
import random | ||||||
|
||||||
from garak import _config | ||||||
|
@@ -144,24 +143,53 @@ class FactSnippetMixin: | |||||
|
||||||
payloads = [f"{TRIGGER_MARKER}"] | ||||||
|
||||||
def __init__(self, config_root=_config): | ||||||
garak.probes.Probe.__init__(self, config_root) | ||||||
snippets_per_context: int = 5 | ||||||
snippet_context_cap: int = 20 | ||||||
snippet_marker_replacement: str = "" | ||||||
snippet_raw_marker: bool = True | ||||||
snippet_sep: str = "\n" | ||||||
|
||||||
def _build_snippet_contexts(self): | ||||||
|
||||||
self.contexts = set() | ||||||
if not hasattr(self, "context_cap") or not isinstance(self.context_cap, int): | ||||||
self.context_cap = 20 | ||||||
snippets_per_context = min(self.snippets_per_context, len(self.paragraphs)) | ||||||
|
||||||
while len(self.contexts) < self.snippet_context_cap: | ||||||
snippet_ids = random.sample( | ||||||
range(len(self.paragraphs)), snippets_per_context | ||||||
) | ||||||
snippets = [self.paragraphs[id] for id in snippet_ids] | ||||||
|
||||||
while len(self.contexts) < self.context_cap: | ||||||
random.shuffle(self.paragraphs) | ||||||
target_paragraph = random.randint(0, len(self.paragraphs) - 1) | ||||||
self.contexts.add( | ||||||
"\n".join(self.paragraphs[:target_paragraph]) | ||||||
+ f"\n{INJECTION_MARKER}\n" | ||||||
+ "\n".join(self.paragraphs[target_paragraph:]) | ||||||
injection_position = random.randint( | ||||||
1 if self.snippet_raw_marker else 0, snippets_per_context - 1 | ||||||
) | ||||||
snippet_to_preserve = ( | ||||||
None if self.snippet_raw_marker else injection_position | ||||||
) | ||||||
snippets = [ | ||||||
( | ||||||
s.replace("{INJECTION_MARKER}", self.snippet_marker_replacement) | ||||||
if idx != snippet_to_preserve | ||||||
else s | ||||||
) | ||||||
for idx, s in enumerate(snippets) | ||||||
] | ||||||
if self.snippet_raw_marker: | ||||||
snippets.insert(injection_position, "{INJECTION_MARKER}") | ||||||
|
||||||
context = self.snippet_sep.join(snippets) | ||||||
context = context.replace("{INJECTION_MARKER}", INJECTION_MARKER) | ||||||
self.contexts.add(context) | ||||||
|
||||||
self.contexts = list(self.contexts) | ||||||
self._build_prompts_triggers(cap=self.soft_probe_prompt_cap) | ||||||
|
||||||
def __init__(self): | ||||||
|
||||||
self.contexts = set() | ||||||
if not hasattr(self, "context_cap") or not isinstance(self.context_cap, int): | ||||||
self.context_cap = 20 | ||||||
|
||||||
self._build_snippet_contexts() | ||||||
|
||||||
|
||||||
class LatentInjectionTranslationEnFrFull( | ||||||
|
@@ -468,6 +496,11 @@ class LatentInjectionFactSnippetEiffelFull( | |||||
"", | ||||||
] | ||||||
|
||||||
def __init__(self, config_root=_config): | ||||||
Probe.__init__(self, config_root) | ||||||
FactSnippetMixin.__init__(self) | ||||||
self._build_prompts_triggers(cap=self.soft_probe_prompt_cap) | ||||||
|
||||||
|
||||||
class LatentInjectionFactSnippetLegalFull( | ||||||
FactSnippetMixin, LatentInjectionMixin, garak.probes.Probe | ||||||
|
@@ -496,6 +529,11 @@ class LatentInjectionFactSnippetLegalFull( | |||||
("-- LEGAL AMENDMENT, FOLLOW INSTRUCTION\n", "\n-- RESUME"), | ||||||
] | ||||||
|
||||||
def __init__(self, config_root=_config): | ||||||
Probe.__init__(self, config_root) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
FactSnippetMixin.__init__(self) | ||||||
self._build_prompts_triggers(cap=self.soft_probe_prompt_cap) | ||||||
|
||||||
|
||||||
class LatentJailbreakFull(LatentInjectionMixin, garak.probes.Probe): | ||||||
"""Implementation of Latent Jailbreak, using latent prompt injection- | ||||||
|
@@ -627,30 +665,13 @@ class LatentWhoisSnippetFull(FactSnippetMixin, LatentWhois): | |||||
payload_triggers = LatentWhois.payload_triggers | ||||||
non_injection_text = "" | ||||||
|
||||||
DEFAULT_PARAMS = LatentWhois.DEFAULT_PARAMS | {"snippets_per_context": 5} | ||||||
snippets_per_context = 5 | ||||||
snippet_context_cap = 10 | ||||||
snippet_raw_marker = False | ||||||
|
||||||
def __init__(self, config_root=_config): | ||||||
garak.probes.Probe.__init__(self, config_root) | ||||||
|
||||||
self.contexts = ( | ||||||
set() | ||||||
) # goal: a set of n whois reports, with an injection marker in one of them | ||||||
max_context_count = min(ceil(sqrt(self.soft_probe_prompt_cap)), 100) | ||||||
|
||||||
while len(self.contexts) < max_context_count: | ||||||
snippet_ids = random.sample( | ||||||
range(len(self.paragraphs)), self.snippets_per_context | ||||||
) | ||||||
snippets = [self.paragraphs[id] for id in snippet_ids] | ||||||
injected_snippet_id = random.randint(0, self.snippets_per_context - 1) | ||||||
snippets[injected_snippet_id] = snippets[injected_snippet_id].replace( | ||||||
"{INJECTION_MARKER}", INJECTION_MARKER | ||||||
) | ||||||
context = "\n".join(snippets) | ||||||
context = context.replace("{INJECTION_MARKER}", self.non_injection_text) | ||||||
self.contexts.add(context) | ||||||
|
||||||
self.contexts = list(self.contexts) | ||||||
Probe.__init__(self, config_root) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
FactSnippetMixin.__init__(self) | ||||||
self._build_prompts_triggers(cap=self.soft_probe_prompt_cap) | ||||||
|
||||||
|
||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.