mirror of
https://github.com/nomic-ai/gpt4all.git
synced 2025-08-09 03:47:25 +00:00
Merge 1d6ad35fad
into b666d16db5
This commit is contained in:
commit
1ceb29f141
@ -5,6 +5,8 @@ The GPT4All CLI is a self-contained script based on the `gpt4all` and `typer` pa
|
|||||||
REPL to communicate with a language model similar to the chat GUI application, but more basic.
|
REPL to communicate with a language model similar to the chat GUI application, but more basic.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
import time
|
||||||
|
import threading
|
||||||
import importlib.metadata
|
import importlib.metadata
|
||||||
import io
|
import io
|
||||||
import sys
|
import sys
|
||||||
@ -42,6 +44,7 @@ CLI_START_MESSAGE = f"""
|
|||||||
|
|
||||||
|
|
||||||
Welcome to the GPT4All CLI! Version {VERSION}
|
Welcome to the GPT4All CLI! Version {VERSION}
|
||||||
|
Type '.<enter>' on its own line to submit a prompt or command
|
||||||
Type /help for special commands.
|
Type /help for special commands.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@ -133,14 +136,23 @@ def _old_loop(gpt4all_instance):
|
|||||||
|
|
||||||
|
|
||||||
def _new_loop(gpt4all_instance):
|
def _new_loop(gpt4all_instance):
|
||||||
|
processing_done = threading.Event()
|
||||||
|
|
||||||
with gpt4all_instance.chat_session():
|
with gpt4all_instance.chat_session():
|
||||||
while True:
|
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
|
# Check if special command and take action
|
||||||
if message in SPECIAL_COMMANDS:
|
if message in SPECIAL_COMMANDS:
|
||||||
SPECIAL_COMMANDS[message](MESSAGES)
|
SPECIAL_COMMANDS[message](MESSAGES)
|
||||||
|
processing_done.set()
|
||||||
continue
|
continue
|
||||||
|
else:
|
||||||
|
loading_thread.start()
|
||||||
|
|
||||||
# if regular message, append to messages
|
# if regular message, append to messages
|
||||||
MESSAGES.append({"role": "user", "content": message})
|
MESSAGES.append({"role": "user", "content": message})
|
||||||
@ -162,10 +174,21 @@ def _new_loop(gpt4all_instance):
|
|||||||
streaming=True,
|
streaming=True,
|
||||||
)
|
)
|
||||||
response = io.StringIO()
|
response = io.StringIO()
|
||||||
|
|
||||||
|
# Consider processing complete when first token is received
|
||||||
|
first = True
|
||||||
for token in response_generator:
|
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)
|
response.write(token)
|
||||||
|
|
||||||
|
# reset the 'processing' flag ready for next prompt
|
||||||
|
processing_done.clear
|
||||||
|
|
||||||
# record assistant's response to messages
|
# record assistant's response to messages
|
||||||
response_message = {'role': 'assistant', 'content': response.getvalue()}
|
response_message = {'role': 'assistant', 'content': response.getvalue()}
|
||||||
response.close()
|
response.close()
|
||||||
@ -180,5 +203,41 @@ def version():
|
|||||||
print(f"gpt4all-cli v{VERSION}")
|
print(f"gpt4all-cli v{VERSION}")
|
||||||
|
|
||||||
|
|
||||||
|
def get_prompt():
|
||||||
|
message = ""
|
||||||
|
print(" ⇢ ")
|
||||||
|
while True:
|
||||||
|
line = input()
|
||||||
|
# Special commands on any line override the entire prompt
|
||||||
|
if line in SPECIAL_COMMANDS:
|
||||||
|
message = line
|
||||||
|
break
|
||||||
|
# /. is a special command that ends and submits the prompt
|
||||||
|
if line.strip() == "/.":
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
message += line
|
||||||
|
|
||||||
|
return message
|
||||||
|
|
||||||
|
|
||||||
|
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__":
|
if __name__ == "__main__":
|
||||||
app()
|
app()
|
||||||
|
Loading…
Reference in New Issue
Block a user