-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmistral_cooldown_probe.py
More file actions
executable file
·172 lines (152 loc) · 5 KB
/
mistral_cooldown_probe.py
File metadata and controls
executable file
·172 lines (152 loc) · 5 KB
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
#!/usr/bin/env python3
import json
import subprocess
import time
from typing import Tuple, List
MISTRAL_API_KEY = "zsEegAJFadHH4uooe2lW0HVNmy1rpqGT"
MISTRAL_MODEL = "mistral-large-latest"
MISTRAL_ENDPOINT = "https://api.mistral.ai/v1/chat/completions"
def now_ms() -> int:
return int(time.time() * 1000)
def curl_chat(payload: dict, stream: bool) -> Tuple[int, int, int]:
"""
Returns: (http_code, request_started_ms, last_token_ms_or_response_end_ms)
For non-stream requests, 3rd value is response-end timestamp.
"""
request_started = now_ms()
cmd = [
"curl",
"-sS",
"-X",
"POST",
MISTRAL_ENDPOINT,
"-H",
"Content-Type: application/json",
"-H",
f"Authorization: Bearer {MISTRAL_API_KEY}",
"--data-binary",
json.dumps(payload),
"-w",
"\nHTTP_STATUS:%{http_code}\n",
]
if stream:
cmd.insert(1, "-N")
proc = subprocess.Popen(
cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
)
last_token_ms = request_started
http_code = 0
assert proc.stdout is not None
for line in proc.stdout:
line = line.rstrip("\n")
if line.startswith("data:"):
data = line[5:].strip()
if data and data != "[DONE]":
last_token_ms = now_ms()
elif line.startswith("HTTP_STATUS:"):
try:
http_code = int(line.split(":", 1)[1].strip())
except ValueError:
http_code = 0
exit_code = proc.wait()
if exit_code != 0:
raise RuntimeError(f"curl failed with exit code {exit_code}")
if not stream:
last_token_ms = now_ms()
return http_code, request_started, last_token_ms
def sleep_until(target_ms: int) -> None:
remaining = target_ms - now_ms()
if remaining > 0:
time.sleep(remaining / 1000.0)
def probe_last_token_mode(delays: List[int]) -> None:
print("=== PROBE: ab_letztem_token ===")
min_success = None
for delay in delays:
stream_payload = {
"model": MISTRAL_MODEL,
"messages": [{"role": "user", "content": "Sag nur OK."}],
"max_tokens": 32,
"stream": True,
}
code, _, last_token = curl_chat(stream_payload, stream=True)
if code != 200:
print(f"baseline_stream_failed http={code}")
continue
sleep_until(last_token + delay)
probe_payload = {
"model": MISTRAL_MODEL,
"messages": [{"role": "user", "content": "OK?"}],
"max_tokens": 1,
"stream": False,
}
probe_code, _, _ = curl_chat(probe_payload, stream=False)
print(f"delay={delay}ms http={probe_code}")
if min_success is None and probe_code == 200:
min_success = delay
print(f"min_success_delay_ms={min_success}")
print()
def probe_request_start_mode(delays: List[int]) -> None:
print("=== PROBE: ab_request_start ===")
min_success = None
for delay in delays:
baseline_payload = {
"model": MISTRAL_MODEL,
"messages": [{"role": "user", "content": "Sag nur OK."}],
"max_tokens": 32,
"stream": True,
}
request_started = now_ms()
baseline_cmd = [
"curl",
"-sS",
"-N",
"-X",
"POST",
MISTRAL_ENDPOINT,
"-H",
"Content-Type: application/json",
"-H",
f"Authorization: Bearer {MISTRAL_API_KEY}",
"--data-binary",
json.dumps(baseline_payload),
"-w",
"\nHTTP_STATUS:%{http_code}\n",
]
baseline_proc = subprocess.Popen(
baseline_cmd,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
text=True,
bufsize=1,
)
sleep_until(request_started + delay)
probe_payload = {
"model": MISTRAL_MODEL,
"messages": [{"role": "user", "content": "OK?"}],
"max_tokens": 1,
"stream": False,
}
probe_code, _, _ = curl_chat(probe_payload, stream=False)
print(f"delay={delay}ms http={probe_code}")
if min_success is None and probe_code == 200:
min_success = delay
baseline_output, _ = baseline_proc.communicate()
baseline_status = 0
for line in baseline_output.splitlines():
if line.startswith("HTTP_STATUS:"):
try:
baseline_status = int(line.split(":", 1)[1].strip())
except ValueError:
baseline_status = 0
if baseline_status != 200:
print(f"baseline_stream_failed http={baseline_status}")
print(f"min_success_delay_ms={min_success}")
print()
if __name__ == "__main__":
step_delays = list(range(100, 3001, 100))
probe_last_token_mode(step_delays)
probe_request_start_mode(step_delays)