1
0
Fork 0
GPT4FREE/unfinished/gptbz/__init__.py

47 lines
1.5 KiB
Python
Raw Normal View History

2023-04-20 14:34:19 +00:00
from json import dumps, loads
2023-04-27 14:43:59 +00:00
import websockets
2023-04-27 16:17:07 +00:00
# Define the asynchronous function to test the WebSocket connection
2023-04-20 14:34:19 +00:00
async def test():
2023-04-27 16:17:07 +00:00
# Establish a WebSocket connection with the specified URL
2023-04-20 14:34:19 +00:00
async with websockets.connect('wss://chatgpt.func.icu/conversation+ws') as wss:
2023-04-27 16:17:07 +00:00
# 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
2023-04-20 14:34:19 +00:00
ended = None
2023-04-27 16:17:07 +00:00
# Continuously receive and process messages until the conversation ends
2023-04-20 14:34:19 +00:00
while not ended:
try:
2023-04-27 16:17:07 +00:00
# Receive and parse the JSON response from the server
response = await wss.recv()
2023-04-20 14:34:19 +00:00
json_response = loads(response)
2023-04-27 16:17:07 +00:00
# Print the entire JSON response
2023-04-25 11:31:12 +00:00
print(json_response)
2023-04-27 16:17:07 +00:00
# Check for the end of the conversation
ended = json_response.get('eof')
# If the conversation has not ended, print the received message
2023-04-20 14:34:19 +00:00
if not ended:
print(json_response['content']['parts'][0])
2023-04-27 16:17:07 +00:00
# Handle cases when the connection is closed by the server
2023-04-20 14:34:19 +00:00
except websockets.ConnectionClosed:
break