From b2037302e984a214014e32b00b2453a69adb8ef7 Mon Sep 17 00:00:00 2001 From: Andrew Tsegaye Date: Thu, 27 Apr 2023 09:17:07 -0700 Subject: well updated --- unfinished/gptbz/__init__.py | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) (limited to 'unfinished/gptbz') diff --git a/unfinished/gptbz/__init__.py b/unfinished/gptbz/__init__.py index ba326cf6..44bfcd19 100644 --- a/unfinished/gptbz/__init__.py +++ b/unfinished/gptbz/__init__.py @@ -1,30 +1,44 @@ import websockets from json import dumps, loads +# Define the asynchronous function to test the WebSocket connection + + async def test(): + # Establish a WebSocket connection with the specified URL async with websockets.connect('wss://chatgpt.func.icu/conversation+ws') as wss: - - await wss.send(dumps(separators=(',', ':'), obj = { - 'content_type':'text', - 'engine':'chat-gpt', - 'parts':['hello world'], - 'options':{} - } - )) - + + # Prepare the message payload as a JSON object + payload = { + 'content_type': 'text', + 'engine': 'chat-gpt', + 'parts': ['hello world'], + 'options': {} + } + + # Send the payload to the WebSocket server + await wss.send(dumps(obj=payload, separators=(',', ':'))) + + # Initialize a variable to track the end of the conversation ended = None + # Continuously receive and process messages until the conversation ends while not ended: try: - response = await wss.recv() + # Receive and parse the JSON response from the server + response = await wss.recv() json_response = loads(response) + + # Print the entire JSON response print(json_response) - - ended = json_response.get('eof') - + + # Check for the end of the conversation + ended = json_response.get('eof') + + # If the conversation has not ended, print the received message if not ended: print(json_response['content']['parts'][0]) - + + # Handle cases when the connection is closed by the server except websockets.ConnectionClosed: break - -- cgit v1.2.3