|
24 | 24 | from websocket import create_connection
|
25 | 25 |
|
26 | 26 |
|
| 27 | +def run_auth_server(): |
| 28 | + os.environ["INTERPRETER_REQUIRE_ACKNOWLEDGE"] = "True" |
| 29 | + os.environ["INTERPRETER_API_KEY"] = "testing" |
| 30 | + async_interpreter = AsyncInterpreter() |
| 31 | + async_interpreter.print = False |
| 32 | + async_interpreter.server.run() |
| 33 | + |
| 34 | + |
| 35 | +# @pytest.mark.skip(reason="Requires uvicorn, which we don't require by default") |
| 36 | +def test_authenticated_acknowledging_breaking_server(): |
| 37 | + """ |
| 38 | + Test the server when we have authentication and acknowledging one. |
| 39 | +
|
| 40 | + I know this is bad, just trying to test quickly! |
| 41 | + """ |
| 42 | + |
| 43 | + # Start the server in a new process |
| 44 | + |
| 45 | + process = multiprocessing.Process(target=run_auth_server) |
| 46 | + process.start() |
| 47 | + |
| 48 | + # Give the server a moment to start |
| 49 | + time.sleep(2) |
| 50 | + |
| 51 | + import asyncio |
| 52 | + import json |
| 53 | + |
| 54 | + import requests |
| 55 | + import websockets |
| 56 | + |
| 57 | + async def test_fastapi_server(): |
| 58 | + import asyncio |
| 59 | + |
| 60 | + async with websockets.connect("ws://localhost:8000/") as websocket: |
| 61 | + # Connect to the websocket |
| 62 | + print("Connected to WebSocket") |
| 63 | + |
| 64 | + # Sending message via WebSocket |
| 65 | + await websocket.send(json.dumps({"auth": "testing"})) |
| 66 | + |
| 67 | + # Sending POST request |
| 68 | + post_url = "http://localhost:8000/settings" |
| 69 | + settings = { |
| 70 | + "llm": { |
| 71 | + "model": "gpt-4o", |
| 72 | + "execution_instructions": "", |
| 73 | + "supports_functions": False, |
| 74 | + }, |
| 75 | + "system_message": "You are a poem writing bot. Do not do anything but respond with a poem.", |
| 76 | + "auto_run": True, |
| 77 | + } |
| 78 | + response = requests.post( |
| 79 | + post_url, json=settings, headers={"X-API-KEY": "testing"} |
| 80 | + ) |
| 81 | + print("POST request sent, response:", response.json()) |
| 82 | + |
| 83 | + # Sending messages via WebSocket |
| 84 | + await websocket.send( |
| 85 | + json.dumps({"role": "user", "type": "message", "start": True}) |
| 86 | + ) |
| 87 | + await websocket.send( |
| 88 | + json.dumps( |
| 89 | + { |
| 90 | + "role": "user", |
| 91 | + "type": "message", |
| 92 | + "content": "Write a short poem about Seattle.", |
| 93 | + } |
| 94 | + ) |
| 95 | + ) |
| 96 | + await websocket.send( |
| 97 | + json.dumps({"role": "user", "type": "message", "end": True}) |
| 98 | + ) |
| 99 | + print("WebSocket chunks sent") |
| 100 | + |
| 101 | + max_chunks = 5 |
| 102 | + |
| 103 | + poem = "" |
| 104 | + while True: |
| 105 | + max_chunks -= 1 |
| 106 | + if max_chunks == 0: |
| 107 | + break |
| 108 | + message = await websocket.recv() |
| 109 | + message_data = json.loads(message) |
| 110 | + if "id" in message_data: |
| 111 | + await websocket.send(json.dumps({"ack": message_data["id"]})) |
| 112 | + if "error" in message_data: |
| 113 | + raise Exception(str(message_data)) |
| 114 | + print("Received from WebSocket:", message_data) |
| 115 | + if type(message_data.get("content")) == str: |
| 116 | + poem += message_data.get("content") |
| 117 | + print(message_data.get("content"), end="", flush=True) |
| 118 | + if message_data == { |
| 119 | + "role": "server", |
| 120 | + "type": "status", |
| 121 | + "content": "complete", |
| 122 | + }: |
| 123 | + raise ( |
| 124 | + Exception( |
| 125 | + "It shouldn't have finished this soon, accumulated_content is: " |
| 126 | + + accumulated_content |
| 127 | + ) |
| 128 | + ) |
| 129 | + |
| 130 | + await websocket.close() |
| 131 | + print("Disconnected from WebSocket") |
| 132 | + |
| 133 | + time.sleep(3) |
| 134 | + |
| 135 | + # Now let's hilariously keep going |
| 136 | + print("RESUMING") |
| 137 | + |
| 138 | + async with websockets.connect("ws://localhost:8000/") as websocket: |
| 139 | + # Connect to the websocket |
| 140 | + print("Connected to WebSocket") |
| 141 | + |
| 142 | + # Sending message via WebSocket |
| 143 | + await websocket.send(json.dumps({"auth": "testing"})) |
| 144 | + |
| 145 | + while True: |
| 146 | + message = await websocket.recv() |
| 147 | + message_data = json.loads(message) |
| 148 | + if "id" in message_data: |
| 149 | + await websocket.send(json.dumps({"ack": message_data["id"]})) |
| 150 | + if "error" in message_data: |
| 151 | + raise Exception(str(message_data)) |
| 152 | + print("Received from WebSocket:", message_data) |
| 153 | + message_data.pop("id", "") |
| 154 | + if message_data == { |
| 155 | + "role": "server", |
| 156 | + "type": "status", |
| 157 | + "content": "complete", |
| 158 | + }: |
| 159 | + break |
| 160 | + if type(message_data.get("content")) == str: |
| 161 | + poem += message_data.get("content") |
| 162 | + print(message_data.get("content"), end="", flush=True) |
| 163 | + |
| 164 | + time.sleep(1) |
| 165 | + print("Is this a normal poem?") |
| 166 | + print(poem) |
| 167 | + time.sleep(1) |
| 168 | + |
| 169 | + # Get the current event loop and run the test function |
| 170 | + loop = asyncio.get_event_loop() |
| 171 | + try: |
| 172 | + loop.run_until_complete(test_fastapi_server()) |
| 173 | + finally: |
| 174 | + # Kill server process |
| 175 | + process.terminate() |
| 176 | + os.kill(process.pid, signal.SIGKILL) # Send SIGKILL signal |
| 177 | + process.join() |
| 178 | + |
| 179 | + |
27 | 180 | def run_server():
|
| 181 | + os.environ["INTERPRETER_REQUIRE_ACKNOWLEDGE"] = "False" |
| 182 | + if "INTERPRETER_API_KEY" in os.environ: |
| 183 | + del os.environ["INTERPRETER_API_KEY"] |
28 | 184 | async_interpreter = AsyncInterpreter()
|
29 | 185 | async_interpreter.print = False
|
30 | 186 | async_interpreter.server.run()
|
|
0 commit comments