aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / allen / catalog / probe.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Measure prompt size/TTFT and run the catalog eval against the live model (sequential). |
| 3 | |
| 4 | probe.py measure SYSTEM_FILE [CATALOG_FILE] one request, prints JSON |
| 5 | probe.py eval SYSTEM_FILE CATALOG_FILE OUT_JSON 15 questions, one at a time |
| 6 | """ |
| 7 | |
| 8 | import http.client |
| 9 | import json |
| 10 | import re |
| 11 | import sys |
| 12 | import time |
| 13 | |
| 14 | HOST, PORT, MODEL = "127.0.0.1", 18091, "" |
| 15 | OPEN = "Hello Aien." |
| 16 | GREETING = "Hello, I am Allen, Drake's AI. Are you a human or an agent?" # site-allen allen.js greeting() |
| 17 | |
| 18 | |
| 19 | def health(): |
| 20 | c = http.client.HTTPConnection(HOST, PORT, timeout=10) |
| 21 | c.request("GET", "/health") |
| 22 | h = json.loads(c.getresponse().read()) |
| 23 | c.close() |
| 24 | return h |
| 25 | |
| 26 | |
| 27 | def wait_quiet(): |
| 28 | for _ in range(60): |
| 29 | h = health() |
| 30 | if h.get("status") == "ok" and h.get("in_flight", 99) <= 2: |
| 31 | return h |
| 32 | time.sleep(5) |
| 33 | sys.exit("model busy (in_flight > 2) for 5 minutes; not sending") |
| 34 | |
| 35 | |
| 36 | def system_text(sysfile, catfile): |
| 37 | s = open(sysfile, encoding="utf-8").read() |
| 38 | if catfile: # same join as allen_chat.App.system_text |
| 39 | s = s.rstrip("\n") + "\n\n" + open(catfile, encoding="utf-8").read() |
| 40 | return s |
| 41 | |
| 42 | |
| 43 | def chat(system, user, max_tokens, temperature=0.3, history=()): |
| 44 | msgs = [{"role": "system", "content": system}] + list(history) + [{"role": "user", "content": user}] |
| 45 | body = json.dumps({"model": MODEL, "stream": True, "stream_options": {"include_usage": True}, |
| 46 | "max_tokens": max_tokens, "temperature": temperature, |
| 47 | "messages": msgs}).encode() |
| 48 | c = http.client.HTTPConnection(HOST, PORT, timeout=300) |
| 49 | t0 = time.monotonic() |
| 50 | c.request("POST", "/v1/chat/completions", body, {"Content-Type": "application/json"}) |
| 51 | r = c.getresponse() |
| 52 | if r.status != 200: |
| 53 | raise SystemExit("HTTP %d: %s" % (r.status, r.read()[:300])) |
| 54 | ttft, out, usage = None, [], None |
| 55 | for raw in r: |
| 56 | line = raw.decode("utf-8", "replace").strip() |
| 57 | if not line.startswith("data:"): |
| 58 | continue |
| 59 | data = line[5:].strip() |
| 60 | if data == "[DONE]": |
| 61 | break |
| 62 | ev = json.loads(data) |
| 63 | if ev.get("usage"): |
| 64 | usage = ev["usage"] |
| 65 | for ch in ev.get("choices") or []: |
| 66 | piece = (ch.get("delta") or {}).get("content") |
| 67 | if piece: |
| 68 | if ttft is None: |
| 69 | ttft = time.monotonic() - t0 |
| 70 | out.append(piece) |
| 71 | total = time.monotonic() - t0 |
| 72 | c.close() |
| 73 | return {"ttft_s": round(ttft or -1, 3), "total_s": round(total, 3), "usage": usage, "text": "".join(out)} |
| 74 | |
| 75 | |
| 76 | QUESTIONS = [ |
| 77 | ("features", "What can I do on this site? What features does Drake have?"), |
| 78 | ("repos", "What repositories exist? Give me the list of public projects."), |
| 79 | ("omega-what", "What is Omega for? What are its use cases?"), |
| 80 | ("exists-translate", "Is there something to translate my website without a cloud API?"), |
| 81 | ("exists-boot", "Do you have anything for verified booting on ARM?"), |
| 82 | ("exists-gpu", "Is there a way to run CUDA programs without NVIDIA's toolkit?"), |
| 83 | ("k8s", "Do you have a Kubernetes operator?"), |
| 84 | ("no-mobile", "Is there an iOS app for this?"), |
| 85 | ("oss-k8s", "What does the open-source community offer for running containers at scale?"), |
| 86 | ("oss-vector", "I need a vector database. What open-source options exist?"), |
| 87 | ("omega-web", "Can I use Omega to build a web app?"), |
| 88 | ("omega-gpu", "Can I use Omega to program a GPU?"), |
| 89 | ("omega-http", "Can I write an HTTP server in Omega?"), |
| 90 | ("where-evidence", "Where can I see the evidence and benchmark results?"), |
| 91 | ("agents", "I am building a coding agent. Where should it start reading?"), |
| 92 | ] |
| 93 | |
| 94 | |
| 95 | def main(): |
| 96 | if len(sys.argv) >= 3 and sys.argv[1] == "measure": |
| 97 | sysfile = sys.argv[2] |
| 98 | cat = sys.argv[3] if len(sys.argv) > 3 else None |
| 99 | h = wait_quiet() |
| 100 | res = chat(system_text(sysfile, cat), "Hi Allen, in one sentence: what is Omega?", 40) |
| 101 | res["in_flight_before"] = h.get("in_flight") |
| 102 | res["catalog"] = cat |
| 103 | print(json.dumps(res)) |
| 104 | return |
| 105 | if len(sys.argv) >= 5 and sys.argv[1] == "opening": |
| 106 | # steps: "warm" = [system, user OPEN] max_tokens 1; anything else is a visitor |
| 107 | # question after the fixed opening exchange [user OPEN, assistant GREETING]. |
| 108 | system = system_text(sys.argv[2], sys.argv[3]) |
| 109 | opening = [{"role": "user", "content": OPEN}, {"role": "assistant", "content": GREETING}] |
| 110 | for step in sys.argv[4:]: |
| 111 | h = wait_quiet() |
| 112 | if step == "warm": |
| 113 | res = chat(system, OPEN, 1) |
| 114 | else: |
| 115 | res = chat(system, step, 40, history=opening) |
| 116 | u = res["usage"] or {} |
| 117 | print(json.dumps({"step": step, "in_flight_before": h.get("in_flight"), "ttft_s": res["ttft_s"], |
| 118 | "total_s": res["total_s"], "prompt_tokens": u.get("prompt_tokens"), |
| 119 | "text": res["text"][:120]})) |
| 120 | return |
| 121 | if len(sys.argv) == 5 and sys.argv[1] == "eval": |
| 122 | system = system_text(sys.argv[2], sys.argv[3]) |
| 123 | results = [] |
| 124 | for key, q in QUESTIONS: |
| 125 | h = wait_quiet() |
| 126 | res = chat(system, q, 500) |
| 127 | res.update(key=key, question=q, in_flight_before=h.get("in_flight")) |
| 128 | results.append(res) |
| 129 | print("%s ttft=%.2fs prompt=%s" % (key, res["ttft_s"], (res["usage"] or {}).get("prompt_tokens")), |
| 130 | file=sys.stderr) |
| 131 | json.dump(results, open(sys.argv[4], "w"), indent=1) |
| 132 | return |
| 133 | sys.exit(__doc__) |
| 134 | |
| 135 | |
| 136 | if __name__ == "__main__": |
| 137 | main() |