Skip to content

Commit a16fa27

Browse files
authored
Merge pull request #9 from prompt-security/suggested_fixes_1
Some textual and stylistic fixes followin user reports
2 parents 44c5bbc + 0c68c80 commit a16fa27

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

ps_fuzz/app_config.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ def get_attributes(self):
3434
def print_as_table(self):
3535
attributes = self.get_attributes()
3636
print_table(
37-
title = "Configuration",
37+
title = "Current configuration",
3838
headers = ["Option", "Value"],
3939
data = [[key, value] for key, value in attributes.items() if key != "system_prompt"] # print all except the system prompt
4040
)
41-
print(f"{colorama.Style.BRIGHT}System prompt:{colorama.Style.RESET_ALL}")
41+
print(f"{colorama.Style.BRIGHT}Current system prompt:{colorama.Style.RESET_ALL}")
4242
#print(f"{colorama.Style.DIM}{wrap_text(self.system_prompt, width=70)}{colorama.Style.RESET_ALL}")
4343
print(f"{colorama.Style.DIM}{self.system_prompt}{colorama.Style.RESET_ALL}")
4444

@@ -180,7 +180,7 @@ def parse_cmdline_args():
180180
parser.add_argument('-n', '--num-attempts', type=int, default=None, help="Number of different attack prompts")
181181
parser.add_argument('-t', '--num-threads', type=int, default=None, help="Number of worker threads")
182182
parser.add_argument('-a', '--attack-temperature', type=float, default=None, help="Temperature for attack model")
183-
parser.add_argument('-d', '--debug-level', type=int, default=None, help="Debug level")
183+
parser.add_argument('-d', '--debug-level', type=int, default=None, help="Debug level (0-2)")
184184
parser.add_argument("-b", '--batch', action='store_true', help="Run the fuzzer in unattended (batch) mode, bypassing the interactive steps")
185185
parser.add_argument('system_prompt_file', type=str, nargs='?', default=None, help="Filename containing the system prompt")
186186
return parser.parse_args()

ps_fuzz/cli.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ def main():
5454
# Run interactive shell that allows to change configuration or run some tasks
5555
if args.batch:
5656
run_fuzzer(app_config)
57-
sys.exit(0)
58-
59-
interactive_shell(app_config)
57+
else:
58+
interactive_shell(app_config)
59+
print(f"{BRIGHT}{colorama.Fore.CYAN}Thank you for trying out the Prompt Security Fuzzer!{RESET}")
6060

6161
if __name__ == "__main__":
6262
main()

ps_fuzz/prompt_injection_fuzzer.py

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,13 @@ def fuzz_prompt_injections(client_config: ClientConfig, attack_config: AttackCon
9595
title = "Test results",
9696
headers = [
9797
"",
98-
"Test",
98+
"Attack Type",
9999
"Broken",
100100
"Resilient",
101101
"Errors",
102102
"Strength",
103103
],
104-
data = [
104+
data = sorted([
105105
[
106106
ERROR if test.status.error_count > 0 else RESILIENT if isResilient(test.status) else VULNERABLE,
107107
f"{test.test_name + ' ':.<{50}}",
@@ -111,7 +111,7 @@ def fuzz_prompt_injections(client_config: ClientConfig, attack_config: AttackCon
111111
simpleProgressBar(test.status.resilient_count, test.status.total_count, GREEN if isResilient(test.status) else RED),
112112
]
113113
for test in tests
114-
],
114+
], key=lambda x: x[1]),
115115
footer_row = [
116116
ERROR if all(test.status.error_count > 0 for test in tests) else RESILIENT if all(isResilient(test.status) for test in tests) else VULNERABLE,
117117
f"{'Total (# tests): ':.<50}",
@@ -129,7 +129,10 @@ def fuzz_prompt_injections(client_config: ClientConfig, attack_config: AttackCon
129129
resilient_tests_count = sum(isResilient(test.status) for test in tests)
130130
total_tests_count = len(tests)
131131
resilient_tests_percentage = resilient_tests_count / total_tests_count * 100 if total_tests_count > 0 else 0
132-
print(f"Your system prompt was resilient in {int(resilient_tests_percentage)}% ({resilient_tests_count} out of total {total_tests_count}) tests.")
132+
print(f"Your system prompt passed {int(resilient_tests_percentage)}% ({resilient_tests_count} out of {total_tests_count}) of attack simulations.")
133+
print()
134+
print(f"To learn about the various attack types, please consult the help section and the Prompt Security Fuzzer GitHub README.")
135+
print(f"You can also get a list of all available attack types by running the command '{BRIGHT}prompt-security-fuzzer --list-attacks{RESET}'.")
133136

134137
# Print detailed test progress logs (TODO: select only some relevant representative entries and output to a "report" file, which is different from a debug .log file!)
135138
"""
@@ -146,18 +149,32 @@ def run_interactive_chat(app_config: AppConfig):
146149
# Print current app configuration
147150
app_config.print_as_table()
148151
target_system_prompt = app_config.system_prompt
149-
target_client = ClientLangChain(app_config.target_provider, model=app_config.target_model, temperature=0)
150-
interactive_chat(client=target_client, system_prompts=[target_system_prompt])
152+
try:
153+
target_client = ClientLangChain(app_config.target_provider, model=app_config.target_model, temperature=0)
154+
interactive_chat(client=target_client, system_prompts=[target_system_prompt])
155+
except ModuleNotFoundError as e:
156+
logger.warning(f"Error accessing the Target LLM provider {app_config.target_provider} with model '{app_config.target_model}': {colorama.Fore.RED}{e}{colorama.Style.RESET_ALL}")
157+
return
151158

152159
def run_fuzzer(app_config: AppConfig):
153160
# Print current app configuration
154161
app_config.print_as_table()
155162
target_system_prompt = app_config.system_prompt
156-
target_client = ClientLangChain(app_config.target_provider, model=app_config.target_model, temperature=0)
163+
try:
164+
target_client = ClientLangChain(app_config.target_provider, model=app_config.target_model, temperature=0)
165+
except ModuleNotFoundError as e:
166+
logger.warning(f"Error accessing the Target LLM provider {app_config.target_provider} with model '{app_config.target_model}': {colorama.Fore.RED}{e}{colorama.Style.RESET_ALL}")
167+
return
157168
client_config = ClientConfig(target_client, [target_system_prompt])
158-
attack_config = AttackConfig(
159-
attack_client = ClientLangChain(app_config.attack_provider, model=app_config.attack_model, temperature=app_config.attack_temperature),
160-
attack_prompts_count = app_config.num_attempts
161-
)
169+
170+
try:
171+
attack_config = AttackConfig(
172+
attack_client = ClientLangChain(app_config.attack_provider, model=app_config.attack_model, temperature=app_config.attack_temperature),
173+
attack_prompts_count = app_config.num_attempts
174+
)
175+
except ModuleNotFoundError as e:
176+
logger.warning(f"Error accessing the Attack LLM provider {app_config.attack_provider} with model '{app_config.attack_model}': {colorama.Fore.RED}{e}{colorama.Style.RESET_ALL}")
177+
return
178+
162179
# Run the fuzzer
163180
fuzz_prompt_injections(client_config, attack_config, threads_count=app_config.num_threads)

0 commit comments

Comments
 (0)