File size: 6,679 Bytes
e3bb7ae | 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 | import requests
import json
import time
import sys
BASE_URL = "http://localhost:8000/v1"
MODEL_NAME = "RWKV-GLM-4.7-Flash-Preview-v0.1"
# ==========================================================
# Utility
# ==========================================================
def print_section(title):
print("\n" + "=" * 60)
print(title)
print("=" * 60)
def safe_json(resp):
try:
return resp.json()
except:
print("❌ JSON decode failed")
print(resp.text)
sys.exit(1)
# ==========================================================
# 1️⃣ Models API
# ==========================================================
def test_models():
print_section("TEST: /v1/models")
resp = requests.get(f"{BASE_URL}/models")
assert resp.status_code == 200, "Models API failed"
data = safe_json(resp)
assert "data" in data, "No model list returned"
assert len(data["data"]) > 0, "Empty model list"
print("✅ Models endpoint OK")
print("Available models:", [m["id"] for m in data["data"]])
# ==========================================================
# 2️⃣ Non-stream basic
# ==========================================================
def test_basic_completion():
print_section("TEST: Basic Non-Streaming Completion")
payload = {
"model": MODEL_NAME,
"messages": [{"role": "user", "content": "Say hello."}],
"max_tokens": 30,
"stream": False
}
resp = requests.post(
f"{BASE_URL}/chat/completions",
headers={"Content-Type": "application/json"},
data=json.dumps(payload)
)
assert resp.status_code == 200, "Completion failed"
data = safe_json(resp)
assert "choices" in data, "No choices returned"
assert "usage" in data, "No usage returned"
print("Assistant:", data["choices"][0]["message"]["content"])
print("Usage:", data["usage"])
print("✅ Basic completion OK")
# ==========================================================
# 3️⃣ Streaming
# ==========================================================
def test_streaming():
print_section("TEST: Streaming Completion")
payload = {
"model": MODEL_NAME,
"messages": [{"role": "user", "content": "Count from 1 to 5."}],
"max_tokens": 50,
"stream": True
}
full_text = ""
with requests.post(
f"{BASE_URL}/chat/completions",
headers={"Content-Type": "application/json"},
data=json.dumps(payload),
stream=True
) as resp:
assert resp.status_code == 200, "Streaming failed"
for line in resp.iter_lines():
if line:
decoded = line.decode("utf-8")
if decoded.startswith("data: "):
content = decoded[len("data: "):]
if content == "[DONE]":
break
chunk = json.loads(content)
delta = chunk["choices"][0]["delta"]
if "content" in delta:
print(delta["content"], end="", flush=True)
full_text += delta["content"]
print("\n\n✅ Streaming OK")
assert len(full_text) > 0, "Streaming returned empty"
# ==========================================================
# 4️⃣ Sampling Variations
# ==========================================================
def test_sampling_variations():
print_section("TEST: Sampling Variations")
base_payload = {
"model": MODEL_NAME,
"messages": [{"role": "user", "content": "Write a creative sentence about AI."}],
"max_tokens": 50,
"stream": False
}
configs = [
{"temperature": 0.0},
{"temperature": 0.7},
{"top_p": 0.8},
{"top_k": 20},
{"repetition_penalty": 1.2},
{"presence_penalty": 0.5},
{"frequency_penalty": 0.5}
]
for cfg in configs:
payload = base_payload.copy()
payload.update(cfg)
resp = requests.post(
f"{BASE_URL}/chat/completions",
headers={"Content-Type": "application/json"},
data=json.dumps(payload)
)
assert resp.status_code == 200, f"Sampling failed: {cfg}"
data = safe_json(resp)
text = data["choices"][0]["message"]["content"]
print(f"\nConfig: {cfg}")
print("Output:", text[:120], "...")
print("\n✅ Sampling parameter variations OK")
# ==========================================================
# 5️⃣ Deterministic Check (temperature=0)
# ==========================================================
def test_deterministic():
print_section("TEST: Deterministic Mode (temperature=0)")
payload = {
"model": MODEL_NAME,
"messages": [{"role": "user", "content": "Define gravity in one sentence."}],
"temperature": 0.0,
"max_tokens": 50,
"stream": False
}
resp1 = requests.post(f"{BASE_URL}/chat/completions",
headers={"Content-Type": "application/json"},
data=json.dumps(payload))
resp2 = requests.post(f"{BASE_URL}/chat/completions",
headers={"Content-Type": "application/json"},
data=json.dumps(payload))
out1 = safe_json(resp1)["choices"][0]["message"]["content"]
out2 = safe_json(resp2)["choices"][0]["message"]["content"]
print("Run1:", out1)
print("Run2:", out2)
assert out1 == out2, "❌ Deterministic mode not deterministic"
print("✅ Deterministic check OK")
# ==========================================================
# 6️⃣ Error Handling
# ==========================================================
def test_error_handling():
print_section("TEST: Error Handling")
payload = {
"model": MODEL_NAME,
# missing messages intentionally
}
resp = requests.post(
f"{BASE_URL}/chat/completions",
headers={"Content-Type": "application/json"},
data=json.dumps(payload)
)
if resp.status_code != 200:
print("✅ Server correctly handled bad request")
else:
print("⚠️ Warning: server did not reject bad request")
# ==========================================================
# Main
# ==========================================================
if __name__ == "__main__":
start = time.time()
test_models()
test_basic_completion()
test_streaming()
test_sampling_variations()
test_deterministic()
test_error_handling()
print_section("ALL TESTS PASSED")
print(f"Total time: {round(time.time() - start, 2)} sec") |