You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
gpt4free/unfinished/gptbz/__init__.py

47 lines
1.5 KiB
Python

from json import dumps, loads
import websockets
2 years ago
# Define the asynchronous function to test the WebSocket connection
async def test():
2 years ago
# Establish a WebSocket connection with the specified URL
async with websockets.connect('wss://chatgpt.func.icu/conversation+ws') as wss:
2 years ago
# 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
2 years ago
# Continuously receive and process messages until the conversation ends
while not ended:
try:
2 years ago
# Receive and parse the JSON response from the server
response = await wss.recv()
json_response = loads(response)
2 years ago
# Print the entire JSON response
2 years ago
print(json_response)
2 years ago
# 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])
2 years ago
# Handle cases when the connection is closed by the server
except websockets.ConnectionClosed:
break