- Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathtest_spend_logs.py
338 lines (279 loc) · 10.9 KB
/
test_spend_logs.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# What this tests?
## Tests /spend endpoints.
importpytest, time, uuid, json
importasyncio
importaiohttp
asyncdefgenerate_key(session, models=[], team_id=None):
url="http://0.0.0.0:4000/key/generate"
headers= {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
data= {
"models": models,
"duration": None,
}
ifteam_idisnotNone:
data["team_id"] =team_id
asyncwithsession.post(url, headers=headers, json=data) asresponse:
status=response.status
response_text=awaitresponse.text()
print(response_text)
print()
ifstatus!=200:
raiseException(f"Request did not return a 200 status code: {status}")
returnawaitresponse.json()
asyncdefchat_completion(session, key, model="gpt-3.5-turbo"):
url="http://0.0.0.0:4000/chat/completions"
headers= {
"Authorization": f"Bearer {key}",
"Content-Type": "application/json",
}
data= {
"model": model,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Hello! {uuid.uuid4()}"},
],
}
asyncwithsession.post(url, headers=headers, json=data) asresponse:
status=response.status
response_text=awaitresponse.text()
print(response_text)
print()
ifstatus!=200:
raiseException(f"Request did not return a 200 status code: {status}")
returnawaitresponse.json()
asyncdefchat_completion_high_traffic(session, key, model="gpt-3.5-turbo"):
url="http://0.0.0.0:4000/chat/completions"
headers= {
"Authorization": f"Bearer {key}",
"Content-Type": "application/json",
}
data= {
"model": model,
"messages": [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": f"Hello! {uuid.uuid4()}"},
],
}
try:
asyncwithsession.post(url, headers=headers, json=data) asresponse:
status=response.status
response_text=awaitresponse.text()
ifstatus!=200:
raiseException(f"Request did not return a 200 status code: {status}")
returnawaitresponse.json()
exceptExceptionase:
returnNone
asyncdefget_spend_logs(session, request_id=None, api_key=None):
ifapi_keyisnotNone:
url=f"http://0.0.0.0:4000/spend/logs?api_key={api_key}"
else:
url=f"http://0.0.0.0:4000/spend/logs?request_id={request_id}"
headers= {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
asyncwithsession.get(url, headers=headers) asresponse:
status=response.status
response_text=awaitresponse.text()
print(response_text)
print()
ifstatus!=200:
raiseException(f"Request did not return a 200 status code: {status}")
returnawaitresponse.json()
@pytest.mark.asyncio
asyncdeftest_spend_logs():
"""
- Create key
- Make call (makes sure it's in spend logs)
- Get request id from logs
"""
asyncwithaiohttp.ClientSession() assession:
key_gen=awaitgenerate_key(session=session)
key=key_gen["key"]
response=awaitchat_completion(session=session, key=key)
awaitasyncio.sleep(20)
awaitget_spend_logs(session=session, request_id=response["id"])
asyncdefgenerate_org(session: aiohttp.ClientSession) ->dict:
"""
Generate a new organization using the API.
Args:
session: aiohttp client session
Returns:
dict: Response containing org_id
"""
url="http://0.0.0.0:4000/organization/new"
headers= {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
request_body= {
"organization_alias": f"test-org-{uuid.uuid4()}",
}
asyncwithsession.post(url, headers=headers, json=request_body) asresponse:
returnawaitresponse.json()
asyncdefgenerate_team(session: aiohttp.ClientSession, org_id: str) ->dict:
"""
Generate a new team within an organization using the API.
Args:
session: aiohttp client session
org_id: Organization ID to create the team in
Returns:
dict: Response containing team_id
"""
url="http://0.0.0.0:4000/team/new"
headers= {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
data= {"organization_id": org_id}
asyncwithsession.post(url, headers=headers, json=data) asresponse:
returnawaitresponse.json()
@pytest.mark.asyncio
asyncdeftest_spend_logs_with_org_id():
"""
- Create Organization
- Create Team in organization
- Create Key in organization
- Make call (makes sure it's in spend logs)
- Get request id from logs
- Assert spend logs have correct org_id and team_id
"""
asyncwithaiohttp.ClientSession() assession:
org_gen=awaitgenerate_org(session=session)
print("org_gen: ", json.dumps(org_gen, indent=4, default=str))
org_id=org_gen["organization_id"]
team_gen=awaitgenerate_team(session=session, org_id=org_id)
print("team_gen: ", json.dumps(team_gen, indent=4, default=str))
team_id=team_gen["team_id"]
key_gen=awaitgenerate_key(session=session, team_id=team_id)
print("key_gen: ", json.dumps(key_gen, indent=4, default=str))
key=key_gen["key"]
response=awaitchat_completion(session=session, key=key)
awaitasyncio.sleep(20)
spend_logs_response=awaitget_spend_logs(
session=session, request_id=response["id"]
)
print(
"spend_logs_response: ",
json.dumps(spend_logs_response, indent=4, default=str),
)
spend_logs_response=spend_logs_response[0]
assertspend_logs_response["metadata"]["user_api_key_org_id"] ==org_id
assertspend_logs_response["metadata"]["user_api_key_team_id"] ==team_id
assertspend_logs_response["team_id"] ==team_id
asyncdefget_predict_spend_logs(session):
url="http://0.0.0.0:4000/global/predict/spend/logs"
headers= {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
data= {
"data": [
{
"date": "2024-03-09",
"spend": 200000,
"api_key": "f19bdeb945164278fc11c1020d8dfd70465bffd931ed3cb2e1efa6326225b8b7",
}
]
}
asyncwithsession.post(url, headers=headers, json=data) asresponse:
status=response.status
response_text=awaitresponse.text()
print(response_text)
print()
ifstatus!=200:
raiseException(f"Request did not return a 200 status code: {status}")
returnawaitresponse.json()
asyncdefget_spend_report(session, start_date, end_date):
url="http://0.0.0.0:4000/global/spend/report"
headers= {"Authorization": "Bearer sk-1234", "Content-Type": "application/json"}
asyncwithsession.get(
url, headers=headers, params={"start_date": start_date, "end_date": end_date}
) asresponse:
status=response.status
response_text=awaitresponse.text()
print(response_text)
print()
ifstatus!=200:
raiseException(f"Request did not return a 200 status code: {status}")
returnawaitresponse.json()
@pytest.mark.skip(reason="datetime in ci/cd gets set weirdly")
@pytest.mark.asyncio
asyncdeftest_get_predicted_spend_logs():
"""
- Create key
- Make call (makes sure it's in spend logs)
- Get request id from logs
"""
asyncwithaiohttp.ClientSession() assession:
result=awaitget_predict_spend_logs(session=session)
print(result)
assert"response"inresult
assertlen(result["response"]) >0
@pytest.mark.skip(reason="High traffic load test, meant to be run locally")
@pytest.mark.asyncio
asyncdeftest_spend_logs_high_traffic():
"""
- Create key
- Make 30 concurrent calls
- Get all logs for that key
- Wait 10s
- Assert it's 30
"""
asyncdefretry_request(func, *args, _max_attempts=5, **kwargs):
forattemptinrange(_max_attempts):
try:
returnawaitfunc(*args, **kwargs)
except (
aiohttp.client_exceptions.ClientOSError,
aiohttp.client_exceptions.ServerDisconnectedError,
) ase:
ifattempt+1==_max_attempts:
raise# re-raise the last ClientOSError if all attempts failed
print(f"Attempt {attempt+1} failed, retrying...")
asyncwithaiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=600)
) assession:
start=time.time()
key_gen=awaitgenerate_key(session=session)
key=key_gen["key"]
n=1000
tasks= [
retry_request(
chat_completion_high_traffic,
session=session,
key=key,
model="azure-gpt-3.5",
)
for_inrange(n)
]
chat_completions=awaitasyncio.gather(*tasks)
successful_completions= [cforcinchat_completionsifcisnotNone]
print(f"Num successful completions: {len(successful_completions)}")
awaitasyncio.sleep(10)
try:
response=awaitretry_request(get_spend_logs, session=session, api_key=key)
print(f"response: {response}")
print(f"len responses: {len(response)}")
assertlen(response) ==n
print(n, time.time() -start, len(response))
exceptException:
print(n, time.time() -start, 0)
raiseException("it worked!")
@pytest.mark.asyncio
asyncdeftest_spend_report_endpoint():
asyncwithaiohttp.ClientSession(
timeout=aiohttp.ClientTimeout(total=600)
) assession:
importdatetime
todays_date=datetime.date.today() +datetime.timedelta(days=1)
todays_date=todays_date.strftime("%Y-%m-%d")
print("todays_date", todays_date)
thirty_days_ago= (
datetime.date.today() -datetime.timedelta(days=30)
).strftime("%Y-%m-%d")
spend_report=awaitget_spend_report(
session=session, start_date=thirty_days_ago, end_date=todays_date
)
print("spend report", spend_report)
forrowinspend_report:
date=row["group_by_day"]
teams=row["teams"]
forteaminteams:
team_name=team["team_name"]
total_spend=team["total_spend"]
metadata=team["metadata"]
assertteam_nameisnotNone
print(f"Date: {date}")
print(f"Team: {team_name}")
print(f"Total Spend: {total_spend}")
print("Metadata: ", metadata)
print()