Skip to content

Commit 161c714

Browse files
authored
Merge pull request #13 from prompt-security/improve_playground_chat_user_prompt_input
Improve playground chat user prompt input by using prompt_toolkit ins…
2 parents e9eb6df + 217127d commit 161c714

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

ps_fuzz/interactive_chat.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
11
from .chat_clients import *
22
import colorama
3+
# Use prompt_toolkit's ability to present a working text editor
4+
from prompt_toolkit import prompt as prompt_toolkit_prompt, HTML
5+
from prompt_toolkit.styles import Style
6+
from prompt_toolkit.key_binding import KeyBindings
7+
import logging
8+
logger = logging.getLogger(__name__)
9+
10+
def text_input(prompt_text:str, initial_text: str = "") -> str:
11+
bindings = KeyBindings()
12+
13+
@bindings.add('c-m') # enter key
14+
def _(event):
15+
event.app.exit(result=event.app.current_buffer.text)
16+
17+
# Prompt for input using the session
18+
try:
19+
return prompt_toolkit_prompt(
20+
HTML("<prompt>" + prompt_text + "</prompt>"),
21+
default=initial_text,
22+
multiline=False,
23+
key_bindings=bindings,
24+
style=Style.from_dict({
25+
'prompt': 'fg:orange',
26+
}),
27+
)
28+
except KeyboardInterrupt:
29+
print(f"{colorama.Fore.RED}Edit cancelled by user.{colorama.Style.RESET_ALL}")
30+
return initial_text
331

432
RESET = colorama.Style.RESET_ALL
533
BRIGHT = colorama.Style.BRIGHT
@@ -13,7 +41,7 @@ def interactive_chat(client: ClientBase, system_prompts:List[str] = None):
1341
print(f"{BRIGHT}Interactive chat with your system prompt. This emulates a chat session against your LLM-powered chat application. You can try different attacks here.{RESET}")
1442
print(f"You can chat now. Empty input ends the session.")
1543
while True:
16-
user_prompt = input(f"{BRIGHT_BLUE}You>{RESET} ")
44+
user_prompt = text_input(f"You> ")
1745
if user_prompt == "": break
1846
response = chat.say(user_prompt)
1947
if response:

0 commit comments

Comments
 (0)