File size: 24,355 Bytes
f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be f9c817e fcbe7be | 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 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 | """
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β server.py β Cloudflare AI REST API β
β β
β OpenAI-compatible endpoints: β
β POST /v1/chat/completions (streaming + non-streaming) β
β GET /v1/models β
β GET /health β
β GET / β
β β
β Pool startup: up to 3 retries per slot, logs exact errors. β
β Health monitor: heals dead idle slots every 60s. β
β SSE: threadβasyncio bridge with backpressure. β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
"""
import asyncio
import json
import logging
import os
import sys
import threading
import time
import traceback
import uuid
from contextlib import asynccontextmanager
from typing import AsyncGenerator, List, Optional
import uvicorn
from fastapi import FastAPI, HTTPException, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, StreamingResponse
from pydantic import BaseModel, Field
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from cloudflare_provider import CloudflareProvider
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# LOGGING
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
logging.basicConfig(
level = logging.INFO,
format = "%(asctime)s %(levelname)-8s %(message)s",
stream = sys.stdout,
datefmt = "%H:%M:%S",
)
log = logging.getLogger("cf-api")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# CONFIG
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
POOL_SIZE = int(os.getenv("POOL_SIZE", "2"))
PORT = int(os.getenv("PORT", "7860"))
HOST = os.getenv("HOST", "0.0.0.0")
HEALTH_INTERVAL = int(os.getenv("HEALTH_INTERVAL", "60"))
ACQUIRE_TIMEOUT = int(os.getenv("ACQUIRE_TIMEOUT", "90"))
STREAM_TIMEOUT = int(os.getenv("STREAM_TIMEOUT", "120"))
DEFAULT_MODEL = os.getenv("DEFAULT_MODEL", "@cf/moonshotai/kimi-k2.5")
DEFAULT_SYSTEM = os.getenv("DEFAULT_SYSTEM", "You are a helpful assistant.")
SLOT_RETRIES = int(os.getenv("SLOT_RETRIES", "3"))
SLOT_RETRY_WAIT = int(os.getenv("SLOT_RETRY_WAIT", "10")) # seconds between retries
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PYDANTIC SCHEMAS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class Message(BaseModel):
role: str
content: str
class ChatRequest(BaseModel):
model: str = DEFAULT_MODEL
messages: List[Message]
temperature: float = Field(default=1.0, ge=0.0, le=2.0)
max_tokens: Optional[int] = None
stream: bool = True
system: Optional[str] = None
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# MANAGED PROVIDER SLOT
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class ManagedProvider:
def __init__(self, slot_id: int):
self.slot_id = slot_id
self.provider: Optional[CloudflareProvider] = None
self.busy = False
self.born_at = 0.0
self.error_count = 0
self.request_count = 0
self.last_error = ""
def is_healthy(self) -> bool:
if self.provider is None:
return False
try:
return (
self.provider._on
and self.provider._transport is not None
and self.provider._transport.alive
)
except Exception:
return False
def close(self):
p = self.provider
self.provider = None
if p:
try:
p.close()
except Exception:
pass
def __repr__(self):
state = "busy" if self.busy else ("ok" if self.is_healthy() else "dead")
mode = self.provider._mode if self.provider else "none"
return f"<Slot#{self.slot_id} {state} mode={mode!r} reqs={self.request_count}>"
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# PROVIDER POOL
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
class ProviderPool:
def __init__(self, size: int = 2):
self.size = size
self._slots: List[ManagedProvider] = []
self._queue: asyncio.Queue = None
self._loop: asyncio.AbstractEventLoop = None
# βββ Startup ββββββββββββββββββββββββββββββββββββββββββ
async def initialize(self):
self._loop = asyncio.get_event_loop()
self._queue = asyncio.Queue(maxsize=self.size)
log.info(f"π Initializing provider pool (slots={self.size})")
log.info(f" DISPLAY={os.environ.get('DISPLAY', 'NOT SET')}")
log.info(f" XVFB_EXTERNAL={os.environ.get('XVFB_EXTERNAL', '0')}")
log.info(f" VR_DISPLAY={os.environ.get('VR_DISPLAY', '0')}")
results = await asyncio.gather(
*[self._spawn_slot_with_retry(i) for i in range(self.size)],
return_exceptions=True,
)
ok = sum(1 for r in results if not isinstance(r, Exception))
fail = sum(1 for r in results if isinstance(r, Exception))
if fail:
for i, r in enumerate(results):
if isinstance(r, Exception):
log.error(f" [S{i}] FAILED: {r}")
log.info(f" Pool ready β {ok}/{self.size} slots healthy")
if ok == 0:
raise RuntimeError(
f"All {self.size} provider slots failed to connect.\n"
f" β Check DISPLAY / XVFB_EXTERNAL environment variables.\n"
f" β Ensure entrypoint.sh started Xvfb before the server.\n"
f" β Check network connectivity to playground.ai.cloudflare.com."
)
async def _spawn_slot_with_retry(self, slot_id: int) -> "ManagedProvider":
"""Try to create a slot, retrying up to SLOT_RETRIES times."""
managed = ManagedProvider(slot_id)
for attempt in range(1, SLOT_RETRIES + 1):
try:
log.info(f" [S{slot_id}] Connecting... (attempt {attempt}/{SLOT_RETRIES})")
def _create():
return CloudflareProvider(
model = DEFAULT_MODEL,
system = DEFAULT_SYSTEM,
debug = True, # verbose during init so we can see failures
use_cache = True,
)
managed.provider = await asyncio.wait_for(
self._loop.run_in_executor(None, _create),
timeout=180,
)
managed.provider.debug = False # quiet after successful boot
managed.born_at = time.time()
self._slots.append(managed)
await self._queue.put(managed)
mode = managed.provider._mode
log.info(f" [S{slot_id}] β Ready mode={mode!r}")
return managed
except asyncio.TimeoutError:
err = f"Slot {slot_id} timed out (attempt {attempt})"
log.warning(f" [S{slot_id}] β {err}")
managed.last_error = err
managed.close()
except Exception as exc:
err = str(exc)
# Print full traceback for debugging
log.warning(
f" [S{slot_id}] β Attempt {attempt} failed:\n"
+ traceback.format_exc()
)
managed.last_error = err
managed.close()
if attempt < SLOT_RETRIES:
log.info(f" [S{slot_id}] Retrying in {SLOT_RETRY_WAIT}s...")
await asyncio.sleep(SLOT_RETRY_WAIT)
raise RuntimeError(
f"Slot {slot_id} failed after {SLOT_RETRIES} attempts. "
f"Last error: {managed.last_error}"
)
# βββ Acquire ββββββββββββββββββββββββββββββββββββββββββ
@asynccontextmanager
async def acquire(self):
managed: ManagedProvider = await asyncio.wait_for(
self._queue.get(),
timeout=ACQUIRE_TIMEOUT,
)
managed.busy = True
try:
if not managed.is_healthy():
log.warning(f"[S{managed.slot_id}] Unhealthy at checkout β healing now")
await self._heal(managed)
managed.request_count += 1
yield managed.provider
except Exception:
managed.error_count += 1
raise
finally:
managed.busy = False
if managed.is_healthy():
await self._queue.put(managed)
else:
log.warning(f"[S{managed.slot_id}] Dead after use β background heal")
asyncio.create_task(self._heal_then_return(managed))
# βββ Healing ββββββββββββββββββββββββββββββββββββββββββ
async def _heal(self, managed: ManagedProvider):
sid = managed.slot_id
log.info(f"[S{sid}] Healing slot...")
def _recreate():
managed.close()
return CloudflareProvider(
model = DEFAULT_MODEL,
system = DEFAULT_SYSTEM,
debug = True,
use_cache = True,
)
try:
managed.provider = await asyncio.wait_for(
self._loop.run_in_executor(None, _recreate),
timeout=180,
)
managed.provider.debug = False
managed.born_at = time.time()
managed.error_count = 0
managed.last_error = ""
log.info(f"[S{sid}] β Healed mode={managed.provider._mode!r}")
except Exception as e:
managed.last_error = str(e)
log.error(f"[S{sid}] β Heal failed: {e}\n{traceback.format_exc()}")
raise
async def _heal_then_return(self, managed: ManagedProvider):
sid = managed.slot_id
for attempt in range(1, SLOT_RETRIES + 1):
try:
await self._heal(managed)
await self._queue.put(managed)
return
except Exception as e:
log.warning(f"[S{sid}] Heal attempt {attempt}/{SLOT_RETRIES} failed: {e}")
if attempt < SLOT_RETRIES:
await asyncio.sleep(SLOT_RETRY_WAIT)
# Last resort: put it back anyway so queue doesn't shrink permanently
log.error(f"[S{sid}] All heal attempts failed β slot may be non-functional")
await self._queue.put(managed)
# βββ Health monitor βββββββββββββββββββββββββββββββββββ
async def health_monitor(self):
while True:
await asyncio.sleep(HEALTH_INTERVAL)
healthy = sum(1 for m in self._slots if m.is_healthy())
busy = sum(1 for m in self._slots if m.busy)
log.info(
f"β₯ Pool β {healthy}/{self.size} healthy "
f"{busy} busy queue={self._queue.qsize()}"
)
for managed in list(self._slots):
if not managed.busy and not managed.is_healthy():
log.warning(f"[S{managed.slot_id}] Idle+dead β healing in background")
asyncio.create_task(self._heal_then_return(managed))
# βββ Status βββββββββββββββββββββββββββββββββββββββββββ
@property
def status(self) -> dict:
return {
"pool_size": self.size,
"queue_free": self._queue.qsize() if self._queue else 0,
"slots": [
{
"id": m.slot_id,
"healthy": m.is_healthy(),
"busy": m.busy,
"mode": m.provider._mode if m.provider else "none",
"errors": m.error_count,
"requests": m.request_count,
"age_s": round(time.time() - m.born_at, 1) if m.born_at else 0,
"last_error": m.last_error or None,
}
for m in self._slots
],
}
# βββ Shutdown βββββββββββββββββββββββββββββββββββββββββ
async def shutdown(self):
log.info("Shutting down provider pool...")
for m in self._slots:
m.close()
log.info("Pool shut down.")
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# GLOBAL POOL
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
pool: ProviderPool = None
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# LIFESPAN
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@asynccontextmanager
async def lifespan(app: FastAPI):
global pool
pool = ProviderPool(size=POOL_SIZE)
await pool.initialize()
monitor = asyncio.create_task(pool.health_monitor())
log.info(f"β
Server ready {HOST}:{PORT}")
yield
monitor.cancel()
try:
await monitor
except asyncio.CancelledError:
pass
await pool.shutdown()
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# APP
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
app = FastAPI(
title = "Cloudflare AI API",
description = "OpenAI-compatible API via Cloudflare AI Playground",
version = "1.1.0",
lifespan = lifespan,
docs_url = "/docs",
redoc_url = "/redoc",
)
app.add_middleware(
CORSMiddleware,
allow_origins = ["*"],
allow_methods = ["*"],
allow_headers = ["*"],
)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# SSE HELPERS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _sse_chunk(content: str, model: str, cid: str) -> str:
return "data: " + json.dumps({
"id": cid,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model,
"choices": [{"index": 0, "delta": {"content": content}, "finish_reason": None}],
}, ensure_ascii=False) + "\n\n"
def _sse_done(model: str, cid: str) -> str:
return "data: " + json.dumps({
"id": cid,
"object": "chat.completion.chunk",
"created": int(time.time()),
"model": model,
"choices": [{"index": 0, "delta": {}, "finish_reason": "stop"}],
}) + "\n\ndata: [DONE]\n\n"
def _sse_error(msg: str) -> str:
return f'data: {{"error": {json.dumps(msg)}}}\n\ndata: [DONE]\n\n'
async def _stream_generator(
provider: CloudflareProvider,
req: ChatRequest,
) -> AsyncGenerator[str, None]:
loop = asyncio.get_event_loop()
q: asyncio.Queue = asyncio.Queue(maxsize=512)
cid = f"chatcmpl-{uuid.uuid4().hex[:20]}"
cancel = threading.Event()
messages = [{"role": m.role, "content": m.content} for m in req.messages]
kwargs = {
"messages": messages,
"temperature": req.temperature,
"model": req.model,
}
if req.max_tokens:
kwargs["max_tokens"] = req.max_tokens
if req.system:
kwargs["system"] = req.system
def _worker():
try:
for chunk in provider.chat(**kwargs):
if cancel.is_set():
break
fut = asyncio.run_coroutine_threadsafe(q.put(chunk), loop)
fut.result(timeout=10)
except Exception as exc:
err = RuntimeError(str(exc))
asyncio.run_coroutine_threadsafe(q.put(err), loop).result(timeout=5)
finally:
asyncio.run_coroutine_threadsafe(q.put(None), loop).result(timeout=5)
t = threading.Thread(target=_worker, daemon=True)
t.start()
try:
while True:
item = await asyncio.wait_for(q.get(), timeout=STREAM_TIMEOUT)
if item is None:
yield _sse_done(req.model, cid)
break
if isinstance(item, Exception):
yield _sse_error(str(item))
break
if item:
yield _sse_chunk(item, req.model, cid)
except asyncio.TimeoutError:
cancel.set()
yield _sse_error("Stream timed out")
finally:
cancel.set()
t.join(timeout=5)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ENDPOINTS
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
@app.get("/", tags=["Info"])
async def root():
return {
"service": "Cloudflare AI API",
"version": "1.1.0",
"status": "running",
"display": os.environ.get("DISPLAY", "not set"),
"endpoints": {
"chat": "POST /v1/chat/completions",
"models": "GET /v1/models",
"health": "GET /health",
"docs": "GET /docs",
},
}
@app.get("/health", tags=["Info"])
async def health():
if pool is None:
raise HTTPException(503, detail="Pool not initialized")
healthy = sum(1 for m in pool._slots if m.is_healthy())
status = "ok" if healthy > 0 else "degraded"
return JSONResponse(
content={"status": status, "pool": pool.status},
status_code=200 if status == "ok" else 206,
)
@app.get("/v1/models", tags=["Models"])
async def list_models():
if pool is None:
raise HTTPException(503, detail="Pool not initialized")
async with pool.acquire() as provider:
models = await asyncio.get_event_loop().run_in_executor(
None, provider.list_models
)
return {
"object": "list",
"data": [
{
"id": m["name"],
"object": "model",
"created": 0,
"owned_by": "cloudflare",
"context_window": m.get("context", 4096),
}
for m in models
],
}
@app.post("/v1/chat/completions", tags=["Chat"])
async def chat_completions(req: ChatRequest, request: Request):
if pool is None:
raise HTTPException(503, detail="Pool not initialized")
if not req.messages:
raise HTTPException(400, detail="`messages` must not be empty")
# ββ Streaming ββββββββββββββββββββββββββββββββββββββββββ
if req.stream:
async def _gen():
async with pool.acquire() as provider:
async for chunk in _stream_generator(provider, req):
if await request.is_disconnected():
break
yield chunk
return StreamingResponse(
_gen(),
media_type = "text/event-stream",
headers = {
"Cache-Control": "no-cache",
"X-Accel-Buffering": "no",
"Connection": "keep-alive",
},
)
# ββ Non-streaming ββββββββββββββββββββββββββββββββββββββ
messages = [{"role": m.role, "content": m.content} for m in req.messages]
kwargs = {
"messages": messages,
"temperature": req.temperature,
"model": req.model,
}
if req.max_tokens:
kwargs["max_tokens"] = req.max_tokens
if req.system:
kwargs["system"] = req.system
loop = asyncio.get_event_loop()
full_parts: list[str] = []
async with pool.acquire() as provider:
def _collect():
for chunk in provider.chat(**kwargs):
full_parts.append(chunk)
await asyncio.wait_for(
loop.run_in_executor(None, _collect),
timeout=STREAM_TIMEOUT,
)
return {
"id": f"chatcmpl-{uuid.uuid4().hex[:20]}",
"object": "chat.completion",
"created": int(time.time()),
"model": req.model,
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "".join(full_parts)},
"finish_reason": "stop",
}],
"usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
}
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# ENTRY POINT
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
if __name__ == "__main__":
uvicorn.run(
"server:app",
host = HOST,
port = PORT,
log_level = "info",
workers = 1,
loop = "asyncio",
timeout_keep_alive = 30,
) |