-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmock_test.py
49 lines (38 loc) · 1.34 KB
/
mock_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import asyncio
from openai_multi_client import OpenAIMultiClient, Payload, OpenAIMultiOrderedClient
def test(ordered):
async def mock(payload: Payload):
import random
rand_wait_time = random.random()
await asyncio.sleep(rand_wait_time)
rand_fail = random.random()
if rand_fail < 0.3:
raise Exception("Mocked exception")
return {"response": f"mocked {payload.metadata['id']}"}
if ordered:
api = OpenAIMultiOrderedClient(custom_api=mock, max_retries=3, retry_multiplier=2)
else:
api = OpenAIMultiClient(custom_api=mock, max_retries=3, retry_multiplier=2)
def put_data():
for pid in range(100):
pid = pid + 1
print(f"Requesting {pid}")
api.request({"prompt": f"This is test {pid}"}, metadata={'id': pid},
endpoint="completions")
api.run_request_function(put_data)
print('*' * 20)
i = 0
failed = 0
for response in api:
i += 1
if response.failed:
failed += 1
print(f"Failed {response.metadata['id']}: {i}/100")
else:
print(f"Got response {response.metadata['id']}: {i}/100")
print('*' * 20)
print(f"Total failed: {failed}/100")
print('*' * 20)
if __name__ == '__main__':
test(ordered=False)
test(ordered=True)