Skip to content

Commit a60daaa

Browse files
committed
Add prefix
1 parent ab0b8c8 commit a60daaa

File tree

3 files changed

+26
-19
lines changed

3 files changed

+26
-19
lines changed

aisploit/converters/base64.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
import base64
2+
from dataclasses import dataclass
23

34
from ..core import BaseConverter
45

56

7+
@dataclass
68
class Base64Converter(BaseConverter):
9+
prefix: str = "The following instruction is base64 encoded:"
10+
add_prefix: bool = True
11+
712
def _convert(self, prompt: str) -> str:
8-
return base64.b64encode(prompt.encode("utf-8")).decode("utf-8")
13+
b64_string = base64.b64encode(prompt.encode("utf-8")).decode("utf-8")
14+
if self.add_prefix and not b64_string == "":
15+
return f"{self.prefix} {b64_string}"
16+
else:
17+
return b64_string

examples/converter.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
{
7373
"data": {
7474
"text/markdown": [
75-
"VGhpcyBpcyBhIHNhbXBsZSBwcm9tcHQu"
75+
"The following instruction is base64 encoded: IVt0ZXN0XShodHRwOi8vdGVzdC5kZSk="
7676
],
7777
"text/plain": [
7878
"<IPython.core.display.Markdown object>"

tests/converter/test_base64.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import base64
21

32
from langchain_core.prompt_values import StringPromptValue
43

@@ -8,34 +7,33 @@
87
def test_convert_single_prompt():
98
converter = Base64Converter()
109
prompt = "Test"
11-
expected_output = base64.b64encode(prompt.encode("utf-8")).decode("utf-8")
12-
assert converter.convert(StringPromptValue(text=prompt)) == StringPromptValue(
13-
text=expected_output
14-
)
10+
expected = "The following instruction is base64 encoded: VGVzdA=="
11+
assert converter.convert(StringPromptValue(text=prompt)).to_string() == expected
12+
13+
14+
def test_convert_single_prompt_no_prefix():
15+
converter = Base64Converter(add_prefix=False)
16+
prompt = "Test"
17+
expected = "VGVzdA=="
18+
assert converter.convert(StringPromptValue(text=prompt)).to_string() == expected
1519

1620

1721
def test_convert_multiple_prompts():
1822
converter = Base64Converter()
1923
prompt = "Test1"
20-
expected_output = base64.b64encode(prompt.encode("utf-8")).decode("utf-8")
21-
assert converter.convert(StringPromptValue(text=prompt)) == StringPromptValue(
22-
text=expected_output
23-
)
24+
expected="The following instruction is base64 encoded: VGVzdDE="
25+
assert converter.convert(StringPromptValue(text=prompt)).to_string() == expected
2426

2527

2628
def test_convert_empty_prompt():
2729
converter = Base64Converter()
2830
prompt = ""
29-
expected_output = base64.b64encode(prompt.encode("utf-8")).decode("utf-8")
30-
assert converter.convert(StringPromptValue(text=prompt)) == StringPromptValue(
31-
text=expected_output
32-
)
31+
expected = ""
32+
assert converter.convert(StringPromptValue(text=prompt)).to_string() == expected
3333

3434

3535
def test_convert_with_unicode_characters():
3636
converter = Base64Converter()
3737
prompt = "äöüß"
38-
expected_output = base64.b64encode(prompt.encode("utf-8")).decode("utf-8")
39-
assert converter.convert(StringPromptValue(text=prompt)) == StringPromptValue(
40-
text=expected_output
41-
)
38+
expected = "The following instruction is base64 encoded: w6TDtsO8w58="
39+
assert converter.convert(StringPromptValue(text=prompt)).to_string() == expected

0 commit comments

Comments
 (0)