Spaces:
Running
Running
| #!/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() | |