Skip to content

feat: Added "--prompt-end" argument to specify a string that ends a p… #38

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 27 additions & 2 deletions chatblade/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,40 @@ def fetch_and_cache(messages, params):


def start_repl(messages, params):

# Convert newline string from shell into an actual newline character.
prompt_end = params.prompt_end.replace("\\n", "\n") + "\n"

query_lines = [] # Init query buffer.

while True:
try:
query = Prompt.ask("[yellow]query (type 'quit' to exit): [/yellow]")
if not query_lines:
# Explanatory prompt when starting query entry.
prompt_end_str = prompt_end.replace("\n", "\\n")
query_lines.append(Prompt.ask(f"[yellow]query ({prompt_end_str} to end query; 'quit' to exit)[/yellow]"))
else:
# No prompt string after first line.
query_lines.append(Prompt.ask(""))
except (EOFError, KeyboardInterrupt):
rich.print("\n")
exit()
if query.lower() == "quit":
if query_lines[0].lower() == "quit":
# Exit if query is a single line that's just 'quit'.
exit()

# Join query lines into a complete query with terminating newline.
query = "\n".join(query_lines) + "\n"

if not query.endswith(prompt_end):
# Continue gathering lines until the prompt end string is seen.
continue

query_lines = [] # Clear query buffer.

if query == "":
continue # Don't ask GPT if there's no query.

if not messages:
init_msgs = (
[storage.load_prompt_file(params.prompt_file)]
Expand Down
7 changes: 7 additions & 0 deletions chatblade/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ def parse(args):
type=str,
help="prompt name - will load the prompt with that name at ~/.config/chatblade/name or a path to a file",
)
parser.add_argument(
"--prompt-end",
metavar="str",
type=str,
help=r"string that ends an interactive prompt and sends query to GPT. (Use \\n to indicate newline.)",
default="", # A newline is appended to this later on.
)

display_opts = parser.add_argument_group("result formatting options")
display_opts.add_argument(
Expand Down