Skip to content
This repository has been archived by the owner on Aug 10, 2023. It is now read-only.

Print response as it streams #23

Merged
merged 2 commits into from
Dec 5, 2022
Merged
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
21 changes: 13 additions & 8 deletions src/revChatGPT/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def get_input(prompt):
import sys

while True:
prompt = get_input("You: ")
prompt = get_input("\nYou:\n")
if prompt.startswith("!"):
if prompt == "!help":
print("""
Expand All @@ -56,19 +56,24 @@ def get_input(prompt):
continue
elif prompt == "!exit":
break
import textwrap

messages = []
lines_printed=0

try:
print("Please wait for ChatGPT to formulate its full response...")
response = chatbot.get_chat_response(prompt)
print("Chatbot: ")
for message in chatbot.get_chat_response(prompt, output="stream"):
formatted_messages = textwrap.wrap(message, width=80)
if (len(formatted_messages) > lines_printed+1):
print(formatted_messages[lines_printed])
lines_printed+=1
print(formatted_messages[lines_printed])
except Exception as e:
print("Something went wrong!")
print(e)
continue
# Erase the "Please wait" line when done waiting
sys.stdout.write("\033[F\033[K")

print("\n")
print("Chatbot:", response['message'])
print("\n")

arguments=list(sys.argv)
del arguments[0]
Expand Down
3 changes: 3 additions & 0 deletions src/revChatGPT/revChatGPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def get_chat_response(self, prompt, output="text"):
"parent_message_id":self.parent_id,
"model":"text-davinci-002-render"
}
#print("bar")
if output == "text":
response = requests.post("https://chat.openai.com/backend-api/conversation", headers=self.headers, data=json.dumps(data))
try:
Expand All @@ -62,6 +63,7 @@ def get_chat_response(self, prompt, output="text"):
continue
line = line[6:]
line = json.loads(line)
#print(line)
try:
message = line["message"]["content"]["parts"][0]
except:
Expand All @@ -70,6 +72,7 @@ def get_chat_response(self, prompt, output="text"):
except:
continue
else:
print("foo")
return ValueError("Output must be either 'text' or 'response'")

def refresh_session(self):
Expand Down