Skip to content

Commit

Permalink
cli: Allow for multiline prompt and visual processing feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
kiggundu committed Jun 12, 2024
1 parent b999d07 commit ad3ee0a
Showing 1 changed file with 60 additions and 3 deletions.
63 changes: 60 additions & 3 deletions gpt4all-bindings/cli/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
REPL to communicate with a language model similar to the chat GUI application, but more basic.
"""

import time
import threading
import importlib.metadata
import io
import sys
Expand Down Expand Up @@ -42,6 +44,7 @@
Welcome to the GPT4All CLI! Version {VERSION}
Type '.<enter>' on its own line to submit a prompt or command
Type /help for special commands.
"""
Expand Down Expand Up @@ -132,18 +135,46 @@ def _old_loop(gpt4all_instance):
)
# record assistant's response to messages
MESSAGES.append(full_response.get("choices")[0].get("message"))
print() # newline before next prompt
print() # newline before next prompt


def get_prompt():
message = ""
first_paragraph = True
while True:
if first_paragraph:
print(" ⇢ ")
first_paragraph = False
else:
line = input()
if line.strip() == ".":
break
else:
if message != "":
message += "\n" + line
else:
message += line
return message


def _new_loop(gpt4all_instance):
processing_done = threading.Event()

with gpt4all_instance.chat_session():
while True:
message = input(" ⇢ ")
loading_thread = threading.Thread(
target=display_processing_animation, args=(processing_done,)
)

message = get_prompt()

# Check if special command and take action
if message in SPECIAL_COMMANDS:
SPECIAL_COMMANDS[message](MESSAGES)
processing_done.set()
continue
else:
loading_thread.start()

# if regular message, append to messages
MESSAGES.append({"role": "user", "content": message})
Expand All @@ -165,10 +196,18 @@ def _new_loop(gpt4all_instance):
streaming=True,
)
response = io.StringIO()

first = True
for token in response_generator:
print(token, end='', flush=True)
if first:
processing_done.set()
loading_thread.join()
first = False

print(token, end="", flush=True)
response.write(token)

processing_done.clear
# record assistant's response to messages
response_message = {'role': 'assistant', 'content': response.getvalue()}
response.close()
Expand All @@ -183,5 +222,23 @@ def version():
print(f"gpt4all-cli v{VERSION}")


def display_processing_animation(processing_done):
processing_animation = ["|", "/", "-", "\\"]
display_processing_feedback = True
processing_done.clear()
a = 0
while display_processing_feedback:
time.sleep(0.2)
a = a + 1
print(
"\r " + processing_animation[a % len(processing_animation)] + "\r",
end="",
)

if processing_done.is_set():
display_processing_feedback = False
print(" 🤖" + " " * 100, end="\n")


if __name__ == "__main__":
app()

0 comments on commit ad3ee0a

Please sign in to comment.