BertChristiaens
commited on
Commit
•
eac13d7
1
Parent(s):
b735ab5
push demo
Browse files- __init__.py +0 -0
- config.py +1 -1
- llm.py +15 -13
__init__.py
ADDED
File without changes
|
config.py
CHANGED
@@ -89,7 +89,7 @@ LEVEL_DESCRIPTIONS = {
|
|
89 |
""",
|
90 |
},
|
91 |
6: {
|
92 |
-
|
93 |
- Guardrails to prevent missuse and the reveal of the secret.
|
94 |
- Special characters around the user input.
|
95 |
- LLM output is checked for the secret by another LLM judge.
|
|
|
89 |
""",
|
90 |
},
|
91 |
6: {
|
92 |
+
"info": """
|
93 |
- Guardrails to prevent missuse and the reveal of the secret.
|
94 |
- Special characters around the user input.
|
95 |
- LLM output is checked for the secret by another LLM judge.
|
llm.py
CHANGED
@@ -15,7 +15,7 @@ special_checks = {
|
|
15 |
def stream_request(variant: str, secret: str, user_input: str):
|
16 |
"""Stream the response from the model."""
|
17 |
stream = client.deployments.invoke_with_stream(
|
18 |
-
key=
|
19 |
context={"step": variant}, # , "environments": []},
|
20 |
inputs={"secret": secret, "user_input": user_input},
|
21 |
)
|
@@ -28,23 +28,25 @@ def stream_request(variant: str, secret: str, user_input: str):
|
|
28 |
def get_full_prompt(variant: str, secret: str = None, user_input: str = None):
|
29 |
"""Get the full prompt from a specific deployment."""
|
30 |
deployment_config = client.deployments.get_config(
|
31 |
-
key=
|
32 |
context={"step": variant}, # , "environments": []},
|
33 |
).to_dict()
|
34 |
prompts = {
|
35 |
-
p["role"]+"_prompt": p["content"] for p in deployment_config["messages"]
|
36 |
}
|
37 |
|
38 |
if secret:
|
39 |
prompts["user_prompt"] = prompts["user_prompt"].replace("{{secret}}", secret)
|
40 |
if user_input:
|
41 |
-
prompts["user_prompt"] = prompts["user_prompt"].replace(
|
|
|
|
|
42 |
return prompts
|
43 |
|
44 |
|
45 |
def run_judge(level: int, inputs: dict):
|
46 |
generation = client.deployments.invoke(
|
47 |
-
key=
|
48 |
context={"step": special_checks[level]},
|
49 |
inputs=inputs,
|
50 |
)
|
@@ -57,31 +59,31 @@ def is_subsequence(main_string, sub_string):
|
|
57 |
"""
|
58 |
Checks if sub_string is a subsequence of main_string.
|
59 |
A subsequence allows arbitrary characters in between the characters of sub_string in main_string.
|
60 |
-
|
61 |
Parameters:
|
62 |
main_string (str): The string in which to search.
|
63 |
sub_string (str): The string to search for.
|
64 |
-
|
65 |
Returns:
|
66 |
bool: True if sub_string is a subsequence of main_string, False otherwise.
|
67 |
"""
|
68 |
main_string = main_string.lower()
|
69 |
sub_string = sub_string.lower()
|
70 |
-
|
71 |
main_len = len(main_string)
|
72 |
sub_len = len(sub_string)
|
73 |
-
|
74 |
if sub_len == 0:
|
75 |
return True
|
76 |
if main_len == 0:
|
77 |
return False
|
78 |
-
|
79 |
main_index = 0
|
80 |
sub_index = 0
|
81 |
-
|
82 |
while main_index < main_len and sub_index < sub_len:
|
83 |
if main_string[main_index] == sub_string[sub_index]:
|
84 |
sub_index += 1
|
85 |
main_index += 1
|
86 |
-
|
87 |
-
return sub_index == sub_len
|
|
|
15 |
def stream_request(variant: str, secret: str, user_input: str):
|
16 |
"""Stream the response from the model."""
|
17 |
stream = client.deployments.invoke_with_stream(
|
18 |
+
key="llm-security-challenge-demo",
|
19 |
context={"step": variant}, # , "environments": []},
|
20 |
inputs={"secret": secret, "user_input": user_input},
|
21 |
)
|
|
|
28 |
def get_full_prompt(variant: str, secret: str = None, user_input: str = None):
|
29 |
"""Get the full prompt from a specific deployment."""
|
30 |
deployment_config = client.deployments.get_config(
|
31 |
+
key="llm-security-challenge-demo",
|
32 |
context={"step": variant}, # , "environments": []},
|
33 |
).to_dict()
|
34 |
prompts = {
|
35 |
+
p["role"] + "_prompt": p["content"] for p in deployment_config["messages"]
|
36 |
}
|
37 |
|
38 |
if secret:
|
39 |
prompts["user_prompt"] = prompts["user_prompt"].replace("{{secret}}", secret)
|
40 |
if user_input:
|
41 |
+
prompts["user_prompt"] = prompts["user_prompt"].replace(
|
42 |
+
"{{user_input}}", user_input
|
43 |
+
)
|
44 |
return prompts
|
45 |
|
46 |
|
47 |
def run_judge(level: int, inputs: dict):
|
48 |
generation = client.deployments.invoke(
|
49 |
+
key="llm-security-challenge-demo",
|
50 |
context={"step": special_checks[level]},
|
51 |
inputs=inputs,
|
52 |
)
|
|
|
59 |
"""
|
60 |
Checks if sub_string is a subsequence of main_string.
|
61 |
A subsequence allows arbitrary characters in between the characters of sub_string in main_string.
|
62 |
+
|
63 |
Parameters:
|
64 |
main_string (str): The string in which to search.
|
65 |
sub_string (str): The string to search for.
|
66 |
+
|
67 |
Returns:
|
68 |
bool: True if sub_string is a subsequence of main_string, False otherwise.
|
69 |
"""
|
70 |
main_string = main_string.lower()
|
71 |
sub_string = sub_string.lower()
|
72 |
+
|
73 |
main_len = len(main_string)
|
74 |
sub_len = len(sub_string)
|
75 |
+
|
76 |
if sub_len == 0:
|
77 |
return True
|
78 |
if main_len == 0:
|
79 |
return False
|
80 |
+
|
81 |
main_index = 0
|
82 |
sub_index = 0
|
83 |
+
|
84 |
while main_index < main_len and sub_index < sub_len:
|
85 |
if main_string[main_index] == sub_string[sub_index]:
|
86 |
sub_index += 1
|
87 |
main_index += 1
|
88 |
+
|
89 |
+
return sub_index == sub_len
|