File size: 1,173 Bytes
9314c03
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import asyncio
import json
from contextlib import asynccontextmanager

from fastapi import FastAPI

from .logging import logger

from .config import WARMUP_INIT_RETRIES, WARMUP_INIT_DELAY_S
from .bridge import initialize_once
from .router import router


@asynccontextmanager
async def lifespan(app: FastAPI):
    """应用生命周期管理"""
    # 启动时执行
    try:
        logger.info("[OpenAI Compat] Server starting with direct module integration")
        logger.info("[OpenAI Compat] Endpoints: GET /healthz, GET /v1/models, POST /v1/chat/completions")
    except Exception:
        pass

    # 移除HTTP健康检查,现在使用直接模块调用
    logger.info("[OpenAI Compat] 跳过HTTP健康检查,使用直接模块集成")

    try:
        await initialize_once()
    except Exception as e:
        logger.warning(f"[OpenAI Compat] Warmup initialize_once on startup failed: {e}")

    yield
    # 关闭时执行(如果需要的话)


app = FastAPI(
    title="OpenAI Chat Completions (Warp bridge) - Streaming",
    lifespan=lifespan
)
app.include_router(router)