Spaces:
Paused
Paused
File size: 9,348 Bytes
a716923 | 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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | """
OMNI-NEXUS · Worker de cómputo elástico
=======================================
Envuelve llama.cpp en una API compatible con OpenAI para que Odysseus lo
vea como un endpoint local más, sin modificar nada del proyecto.
Endpoints:
GET /health → estado (lo usa el healthcheck de HF)
GET /v1/models → catálogo (lo usa Odysseus al añadir el endpoint)
POST /v1/chat/completions → inferencia, con y sin streaming
GET /warm → despierta el Space sin gastar inferencia
Nota sobre /warm: los Spaces gratuitos se suspenden tras un rato inactivos y
tardan ~60-90 s en volver. El router del gateway llama a /warm para levantar
el siguiente worker mientras el actual trabaja, así el usuario no percibe el
arranque en frío.
"""
from __future__ import annotations
import json
import os
import time
import uuid
from typing import Any, AsyncIterator, Dict, List, Optional
import httpx
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
LLAMA_URL = f"http://127.0.0.1:{os.getenv('LLAMA_PORT', '8080')}"
WORKER_ID = os.getenv("WORKER_ID", "A")
N_CTX = int(os.getenv("N_CTX", "4096"))
TIMEOUT = float(os.getenv("LLAMA_TIMEOUT", "180.0"))
MODEL_NAME = os.getenv("MODEL_NAME", "omni-nexus/worker")
# Plantilla de chat. BitNet b1.58-2B-4T se entrenó con el formato de Llama 3,
# no con ChatML. Usar la plantilla equivocada degrada mucho la calidad sin dar
# ningún error visible — el modelo simplemente responde peor. Por eso es una
# variable explícita y no una suposición.
CHAT_TEMPLATE = os.getenv("CHAT_TEMPLATE", "llama3").lower()
app = FastAPI(title=f"Omni-Nexus Worker {WORKER_ID}", version="1.0.0")
# CORS abierto: el endpoint se consume desde el navegador de Odysseus.
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
_stats = {"requests": 0, "tokens": 0, "started_at": time.time()}
# ---------------------------------------------------------------------------
# Modelos de entrada (subconjunto del esquema OpenAI que Odysseus usa)
# ---------------------------------------------------------------------------
class Message(BaseModel):
role: str
content: Any = ""
class ChatRequest(BaseModel):
model: Optional[str] = None
messages: List[Message]
temperature: float = 0.7
top_p: float = 0.95
max_tokens: Optional[int] = Field(default=1024)
stream: bool = False
stop: Optional[List[str]] = None
# ---------------------------------------------------------------------------
def _flatten(content: Any) -> str:
"""El contenido puede venir como string o como lista de bloques."""
if isinstance(content, str):
return content
if isinstance(content, list):
parts = []
for block in content:
if isinstance(block, dict) and block.get("type") == "text":
parts.append(block.get("text", ""))
elif isinstance(block, str):
parts.append(block)
return "\n".join(parts)
return str(content or "")
def _prompt_llama3(messages: List[Message]) -> str:
"""Formato Llama 3 — el correcto para BitNet b1.58-2B-4T."""
out = ["<|begin_of_text|>"]
for m in messages:
role = m.role if m.role in ("system", "user", "assistant") else "user"
out.append(
f"<|start_header_id|>{role}<|end_header_id|>\n\n"
f"{_flatten(m.content)}<|eot_id|>"
)
out.append("<|start_header_id|>assistant<|end_header_id|>\n\n")
return "".join(out)
def _prompt_chatml(messages: List[Message]) -> str:
"""Formato ChatML — Qwen 2.5 y buena parte de los instruct actuales."""
out = []
for m in messages:
role = m.role if m.role in ("system", "user", "assistant") else "user"
out.append(f"<|im_start|>{role}\n{_flatten(m.content)}<|im_end|>")
out.append("<|im_start|>assistant\n")
return "\n".join(out)
def to_prompt(messages: List[Message]) -> str:
if CHAT_TEMPLATE == "chatml":
return _prompt_chatml(messages)
return _prompt_llama3(messages)
# Se incluyen los stops de ambas plantillas: sobra un par de tokens en la
# lista y evita que un cambio de modelo deje basura al final de la respuesta.
STOP_TOKENS = [
"<|eot_id|>", "<|start_header_id|>", "<|end_header_id|>",
"<|im_end|>", "<|im_start|>", "</s>", "<|end_of_text|>",
]
# ---------------------------------------------------------------------------
@app.get("/health")
async def health():
llama_ok = False
try:
async with httpx.AsyncClient(timeout=5.0) as c:
r = await c.get(f"{LLAMA_URL}/health")
llama_ok = r.status_code == 200
except Exception:
pass
return {
"status": "ok" if llama_ok else "starting",
"worker": WORKER_ID,
"llama": llama_ok,
"ctx": N_CTX,
"template": CHAT_TEMPLATE,
"kind": os.getenv("WORKER_KIND", "chat"),
"uptime_s": round(time.time() - _stats["started_at"]),
"requests": _stats["requests"],
}
@app.get("/warm")
async def warm():
"""Despierta el Space sin gastar inferencia. Responde apenas el proceso vive."""
return {"worker": WORKER_ID, "awake": True, "ts": time.time()}
@app.get("/v1/models")
async def models():
return {
"object": "list",
"data": [
{
"id": MODEL_NAME,
"object": "model",
"created": int(_stats["started_at"]),
"owned_by": f"omni-nexus-worker-{WORKER_ID}",
"context_length": N_CTX,
}
],
}
@app.post("/v1/chat/completions")
async def chat(req: ChatRequest):
prompt = to_prompt(req.messages)
payload = {
"prompt": prompt,
"temperature": req.temperature,
"top_p": req.top_p,
"n_predict": req.max_tokens or 1024,
"stop": STOP_TOKENS + (req.stop or []),
"stream": req.stream,
"cache_prompt": True, # reutiliza el KV cache entre turnos: gran ahorro
}
_stats["requests"] += 1
if req.stream:
return StreamingResponse(
_stream(payload, req.model), media_type="text/event-stream"
)
try:
async with httpx.AsyncClient(timeout=TIMEOUT) as c:
r = await c.post(f"{LLAMA_URL}/completion", json=payload)
r.raise_for_status()
data = r.json()
except httpx.TimeoutException:
raise HTTPException(504, detail=f"Worker {WORKER_ID}: timeout de inferencia")
except Exception as exc:
raise HTTPException(502, detail=f"Worker {WORKER_ID}: {exc}")
text = (data.get("content") or "").strip()
for tok in STOP_TOKENS:
text = text.replace(tok, "")
n_out = data.get("tokens_predicted", 0)
n_in = data.get("tokens_evaluated", 0)
_stats["tokens"] += n_out
return {
"id": f"chatcmpl-{uuid.uuid4().hex[:12]}",
"object": "chat.completion",
"created": int(time.time()),
"model": req.model or MODEL_NAME,
"choices": [
{
"index": 0,
"message": {"role": "assistant", "content": text.strip()},
"finish_reason": "stop",
}
],
"usage": {
"prompt_tokens": n_in,
"completion_tokens": n_out,
"total_tokens": n_in + n_out,
},
"_worker": WORKER_ID,
}
async def _stream(payload: Dict[str, Any], model: Optional[str]) -> AsyncIterator[str]:
cid = f"chatcmpl-{uuid.uuid4().hex[:12]}"
created = int(time.time())
def frame(delta: Dict[str, Any], finish: Optional[str] = None) -> str:
return "data: " + json.dumps(
{
"id": cid,
"object": "chat.completion.chunk",
"created": created,
"model": model or MODEL_NAME,
"choices": [{"index": 0, "delta": delta, "finish_reason": finish}],
}
) + "\n\n"
yield frame({"role": "assistant", "content": ""})
try:
async with httpx.AsyncClient(timeout=TIMEOUT) as c:
async with c.stream("POST", f"{LLAMA_URL}/completion", json=payload) as r:
async for line in r.aiter_lines():
if not line.startswith("data: "):
continue
try:
chunk = json.loads(line[6:])
except json.JSONDecodeError:
continue
piece = chunk.get("content", "")
if piece:
_stats["tokens"] += 1
yield frame({"content": piece})
if chunk.get("stop"):
break
except Exception as exc:
yield frame({"content": f"\n[worker {WORKER_ID}: {exc}]"})
yield frame({}, finish="stop")
yield "data: [DONE]\n\n"
@app.get("/")
async def root():
return {
"name": f"Omni-Nexus Worker {WORKER_ID}",
"usage": "Añádelo en Odysseus → Settings → Add Local Models con la URL /v1",
"endpoints": ["/health", "/warm", "/v1/models", "/v1/chat/completions"],
}
|