File size: 2,917 Bytes
1f355ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""The demo server against a fake model: no GPU, no key needed.   python -m tests.test_demo"""
import asyncio
import json
import os

from aiohttp import web
from aiohttp.test_utils import TestClient, TestServer


def fake_model(seen):
    async def chat(request):
        body = await request.json()
        seen.append({"auth": request.headers.get("Authorization", ""), "model": body["model"], "max_tokens": body["max_tokens"]})
        resp = web.StreamResponse(headers={"Content-Type": "text/event-stream"})
        await resp.prepare(request)
        for piece in ("def ", "merge", "():"):
            await resp.write(("data: " + json.dumps({"choices": [{"delta": {"content": piece}}]}) + "\n\n").encode())
        await resp.write(("data: " + json.dumps({"choices": [], "usage": {"completion_tokens": 3}}) + "\n\ndata: [DONE]\n\n").encode())
        return resp
    app = web.Application()
    app.router.add_post("/v1/chat/completions", chat)
    return app


async def run():
    seen = []
    up = TestServer(fake_model(seen))
    await up.start_server()
    url = str(up.make_url("")).rstrip("/")
    os.environ.update(MINI_URL=url, MINI_MODEL="rightnow-mini", OTHER_URL=url, OTHER_MODEL="other/model", OTHER_KEY_ENV="DEMO_TEST_KEY",
                      DEMO_TEST_KEY="not-a-real-key")
    from demo import server as demo
    import argparse
    client = TestClient(TestServer(await demo.make_app(argparse.Namespace(max_tokens=64, daily_cap=3))))
    await client.start_server()
    page = await client.get("/")
    info = await (await client.get("/info")).json()
    texts = {}
    for side in ("mini", "other"):
        r = await client.post("/stream/" + side, json={"task": "merge intervals"})
        texts[side] = await r.text()
    empty = await client.post("/stream/mini", json={"task": "  "})
    capped = await client.post("/stream/mini", json={"task": "again"})
    capped2 = await client.post("/stream/mini", json={"task": "again"})
    html = await page.text()
    await client.close()
    await up.close()
    return html, info, texts, seen, empty.status, capped.status, capped2.status


def main():
    html, info, texts, seen, empty, c1, c2 = asyncio.run(run())
    assert "RightNow Mini, side by side" in html and info == {"mini": "rightnow-mini", "other": "other/model", "max_tokens": 64}
    assert all("merge" in t and "[DONE]" in t for t in texts.values())
    assert seen[0]["auth"] == "" and seen[0]["model"] == "rightnow-mini" and seen[1]["auth"] == "Bearer not-a-real-key" and seen[1]["max_tokens"] == 64
    assert "not-a-real-key" not in html and "not-a-real-key" not in json.dumps(info) and all("not-a-real-key" not in t for t in texts.values())
    assert empty == 400 and c1 == 200 and c2 == 429
    print("ok  the page loads, both sides stream, the other model's key goes upstream only, an empty task is refused, the daily cap holds")


if __name__ == "__main__":
    main()