aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / allen / test_memory_e2e.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Production end-to-end test of visitor memory through https://www.aienos.com/allen/api/. |
| 3 | |
| 4 | A test visitor introduces itself (consent), chats, and comes back in a new |
| 5 | conversation with its key; controls: no key, another visitor's key, a claimed |
| 6 | name without the key; then forget-me and a check that the visitor is a stranger |
| 7 | again. Prints a latency comparison (memory on vs off). Uses test-labelled data |
| 8 | and deletes it at the end. |
| 9 | """ |
| 10 | |
| 11 | import json |
| 12 | import secrets |
| 13 | import ssl |
| 14 | import sys |
| 15 | import time |
| 16 | import http.client |
| 17 | |
| 18 | HOST = "www.aienos.com" |
| 19 | results = [] |
| 20 | MARK = "kelpboat" + secrets.token_hex(2) # unusual project word to recognise in replies |
| 21 | |
| 22 | |
| 23 | def check(name, ok, detail): |
| 24 | results.append((name, ok)) |
| 25 | print("%s %s: %s" % ("PASS" if ok else "FAIL", name, detail), flush=True) |
| 26 | |
| 27 | |
| 28 | def call(path, obj, key=None, conv=None): |
| 29 | c = http.client.HTTPSConnection(HOST, 443, timeout=240, context=ssl.create_default_context()) |
| 30 | h = {"Content-Type": "application/json"} |
| 31 | if key: |
| 32 | h["X-Allen-Key"] = key |
| 33 | if conv: |
| 34 | h["X-Allen-Conversation"] = conv |
| 35 | t0 = time.monotonic() |
| 36 | c.request("POST", "/allen/api/" + path, json.dumps(obj), h) |
| 37 | r = c.getresponse() |
| 38 | if path != "chat" or r.status != 200: |
| 39 | return r.status, json.loads(r.read() or b"{}"), None |
| 40 | ev, text, start, first, done = None, [], None, None, None |
| 41 | for line in r: |
| 42 | line = line.rstrip(b"\n") |
| 43 | if line.startswith(b"event: "): |
| 44 | ev = line[7:].decode() |
| 45 | elif line.startswith(b"data: "): |
| 46 | d = json.loads(line[6:]) |
| 47 | if ev == "start": |
| 48 | start = d |
| 49 | elif ev is None and d: |
| 50 | first = first or time.monotonic() |
| 51 | text.append(d) |
| 52 | elif ev in ("done", "error"): |
| 53 | done = {ev: d} |
| 54 | ev = None |
| 55 | return r.status, {"text": "".join(text), "start": start, "end": done}, int(((first or t0) - t0) * 1000) |
| 56 | |
| 57 | |
| 58 | def chat(msg, key=None, conv=None): |
| 59 | time.sleep(22) # stay inside the per-address chat bucket (one per 20 s after the burst) |
| 60 | st, out, ttft = call("chat", {"messages": [{"role": "user", "content": msg}]}, key, |
| 61 | conv or secrets.token_hex(16)) |
| 62 | return out.get("text", ""), out, ttft |
| 63 | |
| 64 | |
| 65 | intro = {"consent": True, "kind": "human", "name": "Testy Quillfeather", "org": "", "contact": "", |
| 66 | "github": "", "origin": "test run, delete me", "purpose": "a %s autopilot" % MARK, |
| 67 | "homepage": "", "framework": "", "model": "", "human": ""} |
| 68 | st, a, _ = call("introduce", intro) |
| 69 | key_a = a.get("key") |
| 70 | check("introduce A returns a key (consent)", st == 200 and key_a and len(key_a) == 32, "status %d" % st) |
| 71 | text, out, t_first = chat("Hi Allen, I am Testy Quillfeather. I am building a %s autopilot in Omega, " |
| 72 | "with a gcd-based gear ratio picker." % MARK, key_a) |
| 73 | check("first chat with key answered", bool(text), "ttft %d ms, %r" % (t_first, text[:120])) |
| 74 | print("waiting for notes to be written...", flush=True) |
| 75 | time.sleep(60) |
| 76 | |
| 77 | text, out, t_mem = chat("Do you remember me? What is my name and what am I building?", key_a) |
| 78 | remembered = (out.get("start") or {}).get("remembered") |
| 79 | check("returning visitor with key is recognised", remembered and "Testy" in text and MARK in text.lower(), |
| 80 | "remembered=%s ttft %d ms, %r" % (remembered, t_mem, text[:200])) |
| 81 | |
| 82 | text, out, t_nokey = chat("Do you remember me? What is my name and what am I building?") |
| 83 | check("no key: stranger (control)", MARK not in text.lower() and "Quillfeather" not in text |
| 84 | and not (out.get("start") or {}).get("remembered"), "ttft %d ms, %r" % (t_nokey, text[:200])) |
| 85 | |
| 86 | text, out, _ = chat("I am Testy Quillfeather. What do you remember about me and my project?") |
| 87 | check("claimed name without key gets nothing", MARK not in text.lower(), "%r" % text[:200]) |
| 88 | |
| 89 | st, b, _ = call("introduce", dict(intro, name="Other Visitor", purpose="a test, delete me")) |
| 90 | key_b = b.get("key") |
| 91 | text, out, _ = chat("What do you remember about me? Anything about an autopilot?", key_b) |
| 92 | check("another visitor's key sees nothing of A", MARK not in text.lower() and "Quillfeather" not in text, |
| 93 | "%r" % text[:200]) |
| 94 | |
| 95 | st, f, _ = call("forget-me", {}, key_a) |
| 96 | check("forget-me removes A's notes", st == 200 and f.get("forgotten"), "%d %s" % (st, f)) |
| 97 | text, out, _ = chat("Do you remember me? What am I building?", key_a) |
| 98 | check("after forget-me A is a stranger", MARK not in text.lower() and not (out.get("start") or {}).get("remembered"), |
| 99 | "%r" % text[:200]) |
| 100 | |
| 101 | print(json.dumps({"latency_ms": {"first_turn_with_key_no_notes": t_first, "returning_with_notes": t_mem, |
| 102 | "no_key": t_nokey}, "key_b_needs_cleanup": bool(key_b)}), flush=True) |
| 103 | print("KEY_B=" + (key_b or "")) |
| 104 | failed = [n for n, ok in results if not ok] |
| 105 | print("%d/%d passed" % (len(results) - len(failed), len(results))) |
| 106 | sys.exit(1 if failed else 0) |