SPB Git

Repository Public

fake_engine.py 5.7 KB 157 lines python

1"""Faux moteur pour les TESTS et le développement du frontend uniquement.23Il reproduit le protocole du serveur mlx_lm (mêmes champs, mêmes événements SSE, mêmes erreurs) sans modèle :4le texte produit est un écho déterministe. Il n'est jamais déployé ni importé par le code de production.56Comportements pilotables par le contenu du dernier message utilisateur :7  « [slow] »      → 150 ms entre les tokens        « [tool] »  → émet un appel d'outil (si tools fournis)8  « [json] »      → émet un objet JSON valide       « [badjson] » → émet du texte non JSON9  « [think] »     → émet d'abord du raisonnement    « [boom] »  → coupe la connexion en cours de flux10"""1112from __future__ import annotations1314import asyncio15import json16import time17import uuid18from typing import Any1920from fastapi import FastAPI, Request21from fastapi.responses import JSONResponse, StreamingResponse2223app = FastAPI()24STATE: dict[str, Any] = {"active": 0, "max_active": 0, "requests": 0, "cancelled": 0, "bodies": []}252627@app.get("/health")28async def health() -> dict[str, str]:29    return {"status": "ok"}303132@app.get("/internal/stats")33async def stats() -> dict[str, Any]:34    return {35        "ready": True,36        "healthy": True,37        "requests": STATE["requests"],38        "model": "fake-engine",39        "pid": 0,40        "uptime_s": 1,41        "warmup_s": 0.0,42        "mlx": {"active_bytes": 0, "peak_bytes": 0, "cache_bytes": 0},43        "prompt_cache": {"entries": 0, "bytes": 0},44        "decode_concurrency": 8,45        "prompt_concurrency": 4,46        "versions": {"mlx": "fake", "mlx_lm": "fake"},47    }484950def _plan(body: dict[str, Any]) -> tuple[list[dict[str, Any]], float, str]:51    """Retourne (deltas, délai, finish_reason)."""52    if "prompt" in body:53        words = [w + " " for w in f"echo: {body['prompt']}".split(" ")]54        return [{"text": w} for w in words], 0.0, "stop"55    last = next((m for m in reversed(body["messages"]) if m["role"] == "user"), {"content": ""})56    text = str(last.get("content") or "")57    delay = 0.15 if "[slow]" in text else 0.058    deltas: list[dict[str, Any]] = []59    finish = "stop"60    if "[think]" in text:61        deltas += [{"reasoning": "je "}, {"reasoning": "réfléchis"}]62    if "[tool]" in text and body.get("tools"):63        name = body["tools"][0]["function"]["name"]64        deltas.append(65            {66                "tool_calls": [67                    {68                        "function": {"name": name, "arguments": json.dumps({"city": "Québec"})},69                        "type": "function",70                        "id": str(uuid.uuid4()),71                        "index": 0,72                    }73                ]74            }75        )76        finish = "tool_calls"77    elif "[badjson]" in text and "rejetée" not in text:78        deltas += [{"content": "voici "}, {"content": "du texte"}]79    elif "[json]" in text or "rejetée" in text:80        deltas += [{"content": '{"answer": '}, {"content": "42}"}]81    else:82        deltas += [{"content": w + " "} for w in f"echo: {text}".split(" ")]83    limit = int(body.get("max_tokens", 512))84    if len(deltas) > limit:85        deltas, finish = deltas[:limit], "length"86    return deltas, delay, finish878889@app.post("/v1/chat/completions")90@app.post("/v1/completions")91async def completions(request: Request) -> Any:92    body = await request.json()93    STATE["requests"] += 194    STATE["bodies"].append(body)95    del STATE["bodies"][:-50]96    if body.get("model") != "default_model":97        return JSONResponse({"error": "model must be 'default_model'"}, status_code=400)98    is_chat = "messages" in body99    deltas, delay, finish = _plan(body)100    boom = is_chat and "[boom]" in json.dumps(body["messages"])101    cid = f"chatcmpl-{uuid.uuid4()}"102    prompt_tokens = len(json.dumps(body.get("messages") or body.get("prompt"))) // 4103104    def evt(choices: list[dict[str, Any]], **extra: Any) -> bytes:105        return (106            b"data: "107            + json.dumps(108                {109                    "id": cid,110                    "object": "chat.completion.chunk",111                    "model": "default_model",112                    "created": int(time.time()),113                    "choices": choices,114                    **extra,115                }116            ).encode()117            + b"\n\n"118        )119120    async def gen() -> Any:121        STATE["active"] += 1122        STATE["max_active"] = max(STATE["max_active"], STATE["active"])123        sent = 0124        try:125            yield b": keepalive 1/1\n\n"126            for d in deltas:127                if delay:128                    await asyncio.sleep(delay)129                if boom and sent == 1:130                    raise RuntimeError("boom")131                if is_chat:132                    yield evt([{"index": 0, "finish_reason": None, "delta": {"role": "assistant", **d}}])133                else:134                    yield evt([{"index": 0, "finish_reason": None, "text": d["text"]}])135                sent += 1136            if is_chat:137                yield evt([{"index": 0, "finish_reason": finish, "delta": {"role": "assistant"}}])138            else:139                yield evt([{"index": 0, "finish_reason": finish, "text": ""}])140            yield evt(141                [],142                usage={143                    "prompt_tokens": prompt_tokens,144                    "completion_tokens": sent,145                    "total_tokens": prompt_tokens + sent,146                    "prompt_tokens_details": {"cached_tokens": 2},147                },148            )149            yield b"data: [DONE]\n\n"150        except asyncio.CancelledError:151            STATE["cancelled"] += 1152            raise153        finally:154            STATE["active"] -= 1155156    return StreamingResponse(gen(), media_type="text/event-stream")157
Files main
Menu

Theme