File size: 1,680 Bytes
62c1330 |
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 |
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import PlainTextResponse
class OpenAIStub(FastAPI):
def __init__(self) -> None:
super().__init__()
self.models = {}
self.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"]
)
@self.get('/v1/billing/usage')
@self.get('/v1/dashboard/billing/usage')
async def handle_billing_usage():
return { 'total_usage': 0 }
@self.get("/", response_class=PlainTextResponse)
@self.head("/", response_class=PlainTextResponse)
@self.options("/", response_class=PlainTextResponse)
async def root():
return PlainTextResponse(content="", status_code=200 if self.models else 503)
@self.get("/health")
async def health():
return {"status": "ok" if self.models else "unk" }
@self.get("/v1/models")
async def get_model_list():
return self.model_list()
@self.get("/v1/models/{model}")
async def get_model_info(model_id: str):
return self.model_info(model_id)
def register_model(self, name: str, model: str = None) -> None:
self.models[name] = model if model else name
def deregister_model(self, name: str) -> None:
if name in self.models:
del self.models[name]
def model_info(self, model: str) -> dict:
result = {
"id": model,
"object": "model",
"created": 0,
"owned_by": "user"
}
return result
def model_list(self) -> dict:
if not self.models:
return {}
result = {
"object": "list",
"data": [ self.model_info(model) for model in list(set(self.models.keys() | self.models.values())) if model ]
}
return result
|