File size: 2,538 Bytes
a1621b5 | 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | """Tests for the FastAPI server (``examples.api_server``).
These tests use FastAPI's TestClient and don't require a running server.
Skipped if fastapi is not installed.
"""
from __future__ import annotations
import pytest
try:
from fastapi.testclient import TestClient
HAS_FASTAPI = True
except ImportError:
HAS_FASTAPI = False
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "examples"))
from palimseste.hf import HFPalimpsesteLM
from palimseste.lm import PalimpsesteConfig
import numpy as np
pytestmark = pytest.mark.skipif(not HAS_FASTAPI,
reason="fastapi not installed")
@pytest.fixture
def app_and_lm():
cfg = PalimpsesteConfig(D=2000, context_window=16, kernel_radius=100,
temperature=0.3)
lm = HFPalimpsesteLM(config=cfg, rng=np.random.default_rng(0))
text = "bonjour salut je suis palimpseste. qui es-tu je suis un cortex. merci je vous en prie."
lm.build_tokenizer(text)
lm.train_on_text(text)
from api_server import create_app
app = create_app(lm)
return app, lm
def test_health(app_and_lm):
app, _ = app_and_lm
client = TestClient(app)
r = client.get("/health")
assert r.status_code == 200
data = r.json()
assert data["status"] == "ok"
assert "D" in data
assert "|M|" in data
def test_stats(app_and_lm):
app, _ = app_and_lm
client = TestClient(app)
r = client.get("/stats")
assert r.status_code == 200
data = r.json()
assert "D" in data
assert "n_traces" in data
assert "vocab_size" in data
assert "turns" in data
def test_chat(app_and_lm):
app, _ = app_and_lm
client = TestClient(app)
r = client.post("/chat", json={"message": "bonjour"})
assert r.status_code == 200
data = r.json()
assert "response" in data
assert "elapsed_ms" in data
assert isinstance(data["response"], str)
def test_teach(app_and_lm):
app, lm = app_and_lm
client = TestClient(app)
n_before = len(lm.mem)
r = client.post("/teach", json={"question": "what color is the sky",
"answer": "blue"})
assert r.status_code == 200
data = r.json()
assert data["success"] is True
assert data["tokens_written"] > 0
assert len(lm.mem) > n_before
def test_root_html(app_and_lm):
app, _ = app_and_lm
client = TestClient(app)
r = client.get("/")
assert r.status_code == 200
assert "PALIMPSESTE" in r.text
|