TokenTrace / scripts /smoke_branch_next.py
cccmmd
init: TokenTrace - LLM interpretability toolbox
76b5743
Raw
History Blame Contribute Delete
3.19 kB
#!/usr/bin/env python
"""
ๅˆ†ๅ‰ๆ ‘ branch-next ๅ†’็ƒŸ่„šๆœฌใ€‚
็”จๆณ•:
python scripts/smoke_branch_next.py
python scripts/smoke_branch_next.py --base-url http://localhost:7860
"""
import argparse
import sys
try:
import requests
except ImportError:
print("requests not installed; run: pip install requests")
sys.exit(1)
_DEFAULT_URL = "http://localhost:7860"
def post(base_url, payload):
return requests.post(f"{base_url}/api/branch-next", json=payload, timeout=120)
def run(base_url):
failures = 0
print("\n[1] ๆญฃๅธธ top-10 โ€” ไธญๅ›ฝ็š„้ฆ–้ƒฝ")
r = post(base_url, {"prefix": "ไธญๅ›ฝ็š„้ฆ–้ƒฝ", "model": "base", "source_page": "causal_flow"})
if r.status_code != 200:
print(f" โœ— {r.status_code}: {r.text[:200]}")
failures += 1
else:
body = r.json()
print(f" โœ“ prefix_tokens={body['prefix_tokens']}, is_context_full={body['is_context_full']}")
print(f" top-3: {[(c['token'], c['prob']) for c in body['candidates'][:3]]}")
# ๆฆ‚็އ้™ๅบ
probs = [c["prob"] for c in body["candidates"]]
assert probs == sorted(probs, reverse=True), "candidates not sorted by prob"
print(" โœ“ ๅ€™้€‰ๆŒ‰ๆฆ‚็އ้™ๅบ")
top1 = body["candidates"][0]["token"]
print(f" top-1 token: {top1!r}")
print("\n[2] ่‡ชๅฎšไน‰ top_k=3")
r = post(base_url, {"prefix": "The capital of France is", "model": "base", "source_page": "causal_flow", "top_k": 3})
if r.status_code != 200:
print(f" โœ— {r.status_code}: {r.text[:200]}")
failures += 1
else:
body = r.json()
assert len(body["candidates"]) == 3
print(f" โœ“ 3 candidates: {[c['token'] for c in body['candidates']]}")
print("\n[3] top_k ่ถ…ไธŠ้™่‡ชๅŠจ clamp")
r = post(base_url, {"prefix": "hello", "model": "base", "source_page": "causal_flow", "top_k": 999})
if r.status_code != 200:
print(f" โœ— {r.status_code}: {r.text[:200]}")
failures += 1
else:
from backend.core.branch_next import BRANCH_NEXT_TOP_K_MAX
body = r.json()
assert len(body["candidates"]) <= BRANCH_NEXT_TOP_K_MAX
print(f" โœ“ clamped to {len(body['candidates'])} candidates")
print("\n[4] ็ผบ prefix โ€” ้ข„ๆœŸ 400")
r = post(base_url, {"model": "base", "source_page": "causal_flow"})
if r.status_code != 400:
print(f" โœ— ้ข„ๆœŸ 400๏ผŒๅพ—ๅˆฐ {r.status_code}")
failures += 1
else:
print(f" โœ“ 400: {r.json()['message']}")
print("\n[5] ้žๆณ• model โ€” ้ข„ๆœŸ 400")
r = post(base_url, {"prefix": "hello", "model": "gpt4", "source_page": "causal_flow"})
if r.status_code != 400:
print(f" โœ— ้ข„ๆœŸ 400๏ผŒๅพ—ๅˆฐ {r.status_code}")
failures += 1
else:
print(f" โœ“ 400: {r.json()['message']}")
print(f"\n{'='*40}")
print("โœ… All smoke tests passed." if failures == 0 else f"โŒ {failures} test(s) failed.")
return failures
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--base-url", default=_DEFAULT_URL)
sys.exit(run(parser.parse_args().base_url))
if __name__ == "__main__":
main()